lib/LaTeXML/Core/Definition/Constructor.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/LaTeXML/Core.pm ↑ Summary lib/LaTeXML/Util/Test.pm »
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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use LaTeXML::Core::Whatsit;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use base qw(LaTeXML::Core::Definition::Primitive);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use LaTeXML::Core::Definition::Constructor::Compiler;
Subroutine BEGIN: called CC=1 SCAR=2.6
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); }
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 my ($self, $stomach) = @_;
78 # Call any `Before' code.
79 my $profiled = $STATE->lookupValue('PROFILING') && ($LaTeXML::CURRENT_TOKEN || $$self{cs});
80 my $tracing = $STATE->lookupValue('TRACINGCOMMANDS');
81 LaTeXML::Core::Definition::startProfiling($profiled, 'digest') if $profiled;
82  
83 my @pre = $self->executeBeforeDigest($stomach);
84  
85 print STDERR '{' . $self->tracingCSName . "}\n" if $tracing;
86 # Get some info before we process arguments...
87 my $font = $STATE->lookupValue('font');
88 my $ismath = $STATE->lookupValue('IN_MATH');
89 # Parse AND digest the arguments to the Constructor
90 my $params = $self->getParameters;
91 my @args = ($params ? $params->readArgumentsAndDigest($stomach, $self) : ());
92 print STDERR $self->tracingArgs(@args) . "\n" if $tracing && @args;
93 my $nargs = $self->getNumArgs;
94 @args = @args[0 .. $nargs - 1];
95  
96 # Compute any extra Whatsit properties (many end up as element attributes)
97 my $properties = $$self{properties};
98 my %props = (!defined $properties ? ()
99 : (ref $properties eq 'CODE' ? &$properties($stomach, @args)
100 : %$properties));
101 foreach my $key (keys %props) {
102 my $value = $props{$key};
103 if (ref $value eq 'CODE') {
104 $props{$key} = &$value($stomach, @args); } }
105 $props{font} = $font unless defined $props{font};
106 $props{locator} = $stomach->getGullet->getLocator unless defined $props{locator};
107 $props{isMath} = $ismath unless defined $props{isMath};
108 $props{level} = $stomach->getBoxingLevel;
109  
110 # Now create the Whatsit, itself.
111 my $whatsit = LaTeXML::Core::Whatsit->new($self, [@args], %props);
112 # Call any 'After' code.
113 my @post = $self->executeAfterDigest($stomach, $whatsit);
114 if (my $cap = $$self{captureBody}) {
115 $whatsit->setBody(@post, $stomach->digestNextBody((ref $cap ? $cap : undef))); @post = (); }
116  
117 my @postpost = $self->executeAfterDigestBody($stomach, $whatsit);
118 LaTeXML::Core::Definition::stopProfiling($profiled, 'digest') if $profiled;
119 return (@pre, $whatsit, @post, @postpost); }
120  
121 # Similar to executeAfterDigest
122 sub executeAfterDigestBody {
123 my ($self, $stomach, @whatever) = @_;
124 local $LaTeXML::Core::State::UNLOCKED = 1;
125 my $post = $$self{afterDigestBody};
126 return ($post ? map { &$_($stomach, @whatever) } @$post : ()); }
127  
128 sub doAbsorbtion {
129 my ($self, $document, $whatsit) = @_;
130 # First, compile the constructor pattern, if needed.
131 my $replacement = $$self{replacement};
132 if (!ref $replacement) {
133 $$self{replacement} = $replacement = LaTeXML::Core::Definition::Constructor::Compiler::compileConstructor($self); }
134 # Now do the absorbtion.
135 if (my $pre = $$self{beforeConstruct}) {
136 map { &$_($document, $whatsit) } @$pre; }
137 &{$replacement}($document, $whatsit->getArgs, $whatsit->getProperties);
138 if (my $post = $$self{afterConstruct}) {
139 map { &$_($document, $whatsit) } @$post; }
140 return; }
141  
142 #===============================================================================
143 1;
144  
145 __END__
« lib/LaTeXML/Core.pm ↑ Summary lib/LaTeXML/Util/Test.pm »