| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Definition::Constructor | # | ||
| 3 | # | Representation of definitions of Control Sequences | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Core::Definition::Constructor; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Global; | ||
| 16 | use LaTeXML::Common::Object; | ||
| 17 | use LaTeXML::Common::Error; | ||
| 18 | use LaTeXML::Core::Whatsit; | ||
| 19 | use base qw(LaTeXML::Core::Definition::Primitive); | ||
| 20 | use LaTeXML::Core::Definition::Constructor::Compiler; | ||
| 21 | |||
| 22 | #********************************************************************** | ||
| 23 | # Constructor control sequences. | ||
| 24 | # They are first converted to a Whatsit in the Stomach, and that Whatsit's | ||
| 25 | # contruction is carried out to form parts of the document. | ||
| 26 | # In particular, beforeDigest, reading args and afterDigest are executed | ||
| 27 | # in the Stomach. | ||
| 28 | #********************************************************************** | ||
| 29 | |||
| 30 | # Known traits: | ||
| 31 | # beforeDigest, afterDigest : code for before/after digestion daemons | ||
| 32 | # afterDigestBody : similar, but applies only the \begin{environment} commands. | ||
| 33 | # reversion : CODE or TOKENS for reverting to TeX form | ||
| 34 | # captureBody : whether to capture the following List as a `body` | ||
| 35 | # (for environments, math modes) | ||
| 36 | # If this is a token, it is the token that will be matched to end the body. | ||
| 37 | # properties : a hash of default values for properties to store in the Whatsit. | ||
| 38 | sub new { | ||
| 39 | my ($class, $cs, $parameters, $replacement, %traits) = @_; | ||
| 40 | my $source = $STATE->getStomach->getGullet->getMouth; | ||
| 41 | Fatal('misdefined', $cs, $source, | ||
| 42 | "Constructor replacement for '" . ToString($cs) . "' is not a string or CODE", | ||
| 43 | "Replacement is $replacement") | ||
| 44 | if !(defined $replacement) || ((ref $replacement) && !(ref $replacement eq 'CODE')); | ||
| 45 | return bless { cs => $cs, parameters => $parameters, replacement => $replacement, | ||
| 46 | locator => $source->getLocator, %traits, | ||
| 47 | ## nargs => (defined $traits{nargs} ? $traits{nargs} | ||
| 48 | ## : ($parameters ? $parameters->getNumArgs : 0)) | ||
| 49 | nargs => $traits{nargs} | ||
| 50 | }, $class; } | ||
| 51 | |||
| 52 | sub getReversionSpec { | ||
| 53 | my ($self) = @_; | ||
| 54 | my $spec = $$self{reversion}; | ||
| 55 | if ($spec && !ref $spec) { | ||
| 56 | $spec = $$self{reversion} = LaTeXML::Package::TokenizeInternal($spec)->packParameters; } | ||
| 57 | return $spec; } | ||
| 58 | |||
| 59 | sub getSizer { | ||
| 60 | my ($self) = @_; | ||
| 61 | return $$self{sizer}; } | ||
| 62 | |||
| 63 | sub getAlias { | ||
| 64 | my ($self) = @_; | ||
| 65 | return $$self{alias}; } | ||
| 66 | |||
| 67 | sub getNumArgs { | ||
| 68 | my ($self) = @_; | ||
| 69 | return $$self{nargs} if defined $$self{nargs}; | ||
| 70 | my $params = $self->getParameters; | ||
| 71 | $$self{nargs} = ($params ? $params->getNumArgs : 0); | ||
| 72 | return $$self{nargs}; } | ||
| 73 | |||
| 74 | # Digest the constructor; This should occur in the Stomach to create a Whatsit. | ||
| 75 | # The whatsit which will be further processed to create the document. | ||
| 76 | sub invoke { | ||
| 77 | no warnings 'recursion'; | ||
| 78 | my ($self, $stomach) = @_; | ||
| 79 | # Call any `Before' code. | ||
| 80 | my $_tracing = $STATE->lookupValue('TRACING') || 0; | ||
| 81 | my $tracing = ($_tracing & TRACE_COMMANDS); | ||
| 82 | my $profiled = ($_tracing & TRACE_PROFILE) && ($LaTeXML::CURRENT_TOKEN || $$self{cs}); | ||
| 83 | |||
| 84 | LaTeXML::Core::Definition::startProfiling($profiled, 'digest') if $profiled; | ||
| 85 | |||
| 86 | my @pre = $self->executeBeforeDigest($stomach); | ||
| 87 | |||
| 88 | Debug('{' . $self->tracingCSName . '}') if $tracing; | ||
| 89 | # Get some info before we process arguments... | ||
| 90 | my $font = $STATE->lookupValue('font'); | ||
| 91 | my $ismath = $STATE->lookupValue('IN_MATH'); | ||
| 92 | # Parse AND digest the arguments to the Constructor | ||
| 93 | my $parms = $$self{parameters}; | ||
| 94 | my @args = ($parms ? $parms->readArgumentsAndDigest($stomach, $self) : ()); | ||
| 95 | Debug($self->tracingArgs(@args)) if $tracing && @args; | ||
| 96 | my $nargs = $self->getNumArgs; | ||
| 97 | @args = @args[0 .. $nargs - 1]; | ||
| 98 | |||
| 99 | # Compute any extra Whatsit properties (many end up as element attributes) | ||
| 100 | my $properties = $$self{properties}; | ||
| 101 | my %props = (!defined $properties ? () | ||
| 102 | : (ref $properties eq 'CODE' ? &$properties($stomach, @args) | ||
| 103 | : %$properties)); | ||
| 104 | foreach my $key (keys %props) { | ||
| 105 | my $value = $props{$key}; | ||
| 106 | if (ref $value eq 'CODE') { | ||
| 107 | $props{$key} = &$value($stomach, @args); } } | ||
| 108 | $props{font} = $font unless defined $props{font}; | ||
| 109 | $props{locator} = $stomach->getGullet->getLocator unless defined $props{locator}; | ||
| 110 | $props{isMath} = $ismath unless defined $props{isMath}; | ||
| 111 | $props{level} = $stomach->getBoxingLevel; | ||
| 112 | $props{scriptlevel} = $stomach->getScriptLevel if $ismath; | ||
| 113 | # Now create the Whatsit, itself. | ||
| 114 | my $whatsit = LaTeXML::Core::Whatsit->new($self, [@args], %props); | ||
| 115 | # Call any 'After' code. | ||
| 116 | my @post = $self->executeAfterDigest($stomach, $whatsit); | ||
| 117 | if (my $cap = $$self{captureBody}) { | ||
| 118 | $whatsit->setBody(@post, $stomach->digestNextBody((ref $cap ? $cap : undef))); @post = (); } | ||
| 119 | |||
| 120 | my @postpost = $self->executeAfterDigestBody($stomach, $whatsit); | ||
| 121 | LaTeXML::Core::Definition::stopProfiling($profiled, 'digest') if $profiled; | ||
| 122 | return (@pre, $whatsit, @post, @postpost); } | ||
| 123 | |||
| 124 | # Similar to executeAfterDigest | ||
| 125 | sub executeAfterDigestBody { | ||
| 126 | my ($self, $stomach, @whatever) = @_; | ||
| 127 | local $LaTeXML::Core::State::UNLOCKED = 1; | ||
| 128 | my @post = grep { defined } @{ $$self{afterDigestBody} || [] }; | ||
| 129 | return (map { &$_($stomach, @whatever) } @post); } | ||
| 130 | |||
| 131 | sub doAbsorbtion { | ||
| 132 | my ($self, $document, $whatsit) = @_; | ||
| 133 | # First, compile the constructor pattern, if needed. | ||
| 134 | my $replacement = $$self{replacement}; | ||
| 135 | if (!ref $replacement) { | ||
| 136 | $$self{replacement} = $replacement = LaTeXML::Core::Definition::Constructor::Compiler::compileConstructor($self); } | ||
| 137 | # Now do the absorbtion. | ||
| 138 | if (my $pre = $$self{beforeConstruct}) { | ||
| 139 | map { &$_($document, $whatsit) } @$pre; } | ||
| 140 | &{$replacement}($document, $whatsit->getArgs, $whatsit->getProperties); | ||
| 141 | if (my $post = $$self{afterConstruct}) { | ||
| 142 | map { &$_($document, $whatsit) } @$post; } | ||
| 143 | return; } | ||
| 144 | |||
| 145 | #=============================================================================== | ||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ |