lib/LaTeXML/Post/OpenMath.pm untested

filter: Statementstmt 0.0% 0 / 166 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 277 CC 22 SCAR 37.6
CC
22
Cov
0%
om_expr_aux
132.0
combineParallel
20.0
om_unparsed
20.0
CRAP 43.0
1 # /=====================================================================\ #
2 # | LaTeXML::Post::OpenMath | #
3 # | OpenMath generator for LaTeXML | #
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  
13 # ================================================================================
14 # TODO
15 # Scheme for mapping from LaTeXML's "meaning" => cd+name
16 # Unfortunately, OpenMath's naming scheme is unweildy, unless you are
17 # completely immersed in the OpenMath world. It is also incomplete in
18 # the sense that it doesn't cover nearly enough symbols that LaTeXML encounters.
19 # And, of course, many of those symbols are ambiguous!
20 #
21 # So, I think LaTeXML's naming scheme is a worthwhile one to pursue,
22 # but we DO need to provide a rich but customizable mapping, starting with stuff like:
23 # equals => arith1:eq
24 # ================================================================================
25 package LaTeXML::Post::OpenMath;
26 use strict;
27 use warnings;
28 use LaTeXML::Common::XML;
29 use LaTeXML::Post::MathML;
30 use LaTeXML::Post;
31 use base qw(LaTeXML::Post::MathProcessor);
32 use base qw(Exporter);
33 our @EXPORT = (
34 qw( &DefOpenMath ),
35 qw( &om_expr ),
36 );
37  
38 my $omURI = "http://www.openmath.org/OpenMath"; # CONSTANT
39 my $omMimeType = 'application/openmath+xml'; # CONSTANT
40  
41 sub preprocess {
42 my ($self, $doc, @nodes) = @_;
43 $$self{hackplane1} = 0 unless $$self{hackplane1};
44 $$self{plane1} = 1 if $$self{hackplane1} || !defined $$self{plane1};
45 $doc->adjust_latexml_doctype('OpenMath'); # Add OpenMath if LaTeXML dtd.
46 $doc->addNamespace($omURI, 'om');
47 return; }
48  
49 sub outerWrapper {
50 my ($self, $doc, $xmath, $om) = @_;
51 my $wrapped = ['om:OMOBJ', {}, $om];
52 # Associate the generated node with the source XMath node; but don't cross-reference
53 $self->associateNode($wrapped, $xmath, 1);
54 return $wrapped; }
55  
56 sub convertNode {
57 my ($self, $doc, $xmath) = @_;
58 my ($item, @rest) = element_nodes($xmath);
59 return { processor => $self,
60 encoding => 'OpenMath', mimetype => $omMimeType,
61 xml => (!$item || @rest ? om_unparsed($item, @rest) : om_expr($item)) }; }
62  
63 sub combineParallel {
64 my ($self, $doc, $xmath, $primary, @secondaries) = @_;
65 my $id = $xmath->getAttribute('fragid');
66 my @attr = ();
67 foreach my $secondary (@secondaries) {
68 my $mimetype = $$secondary{mimetype} || 'unknown';
69 if ($mimetype eq $omMimeType) { # Another OpenMath object?
70 push(@attr,
71 ['om:OMS', { cd => "Alternate", name => $mimetype }],
72 $$secondary{xml}); }
73 elsif (my $xml = $$secondary{xml}) { # Or some other XML object?
74 # ORRRR should this be in other order?
75 push(@attr,
76 ['om:OMS', { cd => "Alternate", name => $mimetype }],
77 ['om:OMFOREIGN', {}, $$secondary{processor}->outerWrapper($doc, $xmath, $xml)]); }
78 # What do do with src?
79 ## elsif (my $src = $$secondary{src}) { # something referred to by a file? Image, maybe?
80 ## push(@wsecondaries, ['m:annotation', { encoding => $mimetype, src => $src }]); }
81 elsif (my $string = $$secondary{string}) { # simple string data?
82 push(@attr,
83 ['om:OMS', { cd => "Alternate", name => $mimetype }],
84 ['om:OMSTR', {}, $string]); }
85 # anything else ignore?
86 }
87 return { processor => $self,
88 mimetype => $omMimeType,
89 xml => ['om:OMATTR', {}, @attr, $$primary{xml}] }; }
90  
91 sub getQName {
92 my ($node) = @_;
93 return $LaTeXML::Post::DOCUMENT->getQName($node); }
94  
95 sub rawIDSuffix {
96 return '.om'; }
97  
98 # ================================================================================
99  
100 # DANGER!!! Ths accumulate all the DefMathML declarations.
101 # They're fixed after the module has been loaded, so are Daemon Safe,
102 # but probably should be going into (post) STATE, so that they are extensible.
103 our $OMTable = {};
104  
105 sub DefOpenMath {
106 my ($key, $sub) = @_;
107 $$OMTable{$key} = $sub;
108 return; }
109  
110 sub om_expr {
111 my ($node) = @_;
112 # Get the real node, first.
113 $node = $LaTeXML::Post::DOCUMENT->realizeXMNode($node);
114 my $result = om_expr_aux($node);
115 # Associate the generated node with the source XMath node.
116 $LaTeXML::Post::MATHPROCESSOR->associateNode($result, $node);
117 return $result; }
118  
119 # Is it clear that we should just getAttribute('role'),
120 # instead of the getOperatorRole like in MML?
121 sub om_expr_aux {
122 my ($node) = @_;
123 return OMError("Missing Subexpression") unless $node;
124 my $tag = getQName($node);
125 if (($tag eq 'ltx:XMWrap') || ($tag eq 'ltx:XMArg')) { # Unparsed
126 my ($item, @rest) = element_nodes($node);
127 return (!$item || @rest ? om_unparsed($item, @rest) : om_expr($item)); }
128 elsif ($tag eq 'ltx:XMDual') {
129 my ($content, $presentation) = element_nodes($node);
130 return om_expr($content); }
131 elsif ($tag eq 'ltx:XMApp') {
132 my ($op, @args) = element_nodes($node);
133 return OMError("Missing Operator") unless $op;
134 my $sub = lookupConverter('Apply', $op->getAttribute('role'), $op->getAttribute('meaning'));
135 return &$sub($op, @args); }
136 elsif ($tag eq 'ltx:XMTok') {
137 my $sub = lookupConverter('Token', $node->getAttribute('role'), $node->getAttribute('meaning'));
138 return &$sub($node); }
139 elsif ($tag eq 'ltx:XMHint') {
140 return (); }
141 elsif ($tag eq 'ltx:XMText') {
142 if (scalar(element_nodes($node))) { # If it has markup?
143 return ['om:OMATTR', {},
144 ['om:OMATP', {},
145 ['om:OMS', { cd => 'OMDoc', name => 'verbalizes' }],
146 ['om:OMFOREIGN', { encoding => 'mtext' },
147 $LaTeXML::Post::MATHPROCESSOR->convertXMTextContent($LaTeXML::Post::DOCUMENT, 0,
148 $node->childNodes)]],
149 ['om:OMS', { cd => 'OMDoc', name => 'infObj' }]] }
150 else {
151 return ['om:OMSTR', {}, $node->textContent]; } }
152 else {
153 return ['om:OMSTR', {}, $node->textContent]; } }
154  
155 sub om_unparsed {
156 my (@nodes) = @_;
157 if (!@nodes) {
158 return ['om:OME', {},
159 ['om:OMS', { name => 'unexpected', cd => 'moreerrors' }],
160 ['om:OMSTR', {}, "Missing Subexpression"]]; }
161 else {
162 my @om = ();
163 foreach my $node (@nodes) {
164 $node = $LaTeXML::Post::DOCUMENT->realizeXMNode($node);
165 my $tag = getQName($node);
166 if ($tag eq 'ltx:XMHint') { }
167 elsif (($tag eq 'ltx:XMTok') && (($node->getAttribute('role') || 'UNKNOWN') eq 'UNKNOWN')) {
168 push(@om, ['om:OMS', { cd => 'unknown', name => $node->textContent }]); }
169 else {
170 push(@om, om_expr_aux($node)); } }
171 return ['om:OME', {}, ['om:OMS', { cd => 'ambiguous', name => 'fragments' }], @om]; } }
172  
173 sub lookupConverter {
174 my ($mode, $role, $name) = @_;
175 $name = '?' unless $name;
176 $role = '?' unless $role;
177 return $$OMTable{"$mode:$role:$name"} || $$OMTable{"$mode:?:$name"}
178 || $$OMTable{"$mode:$role:?"} || $$OMTable{"$mode:?:?"}; }
179  
180 # ================================================================================
181 # Helpers
182 sub OMError {
183 my ($msg) = @_;
184 return ['om:OME', {},
185 ['om:OMS', { name => 'unexpected', cd => 'moreerrors' }],
186 ['om:OMS', {}, $msg]]; }
187 # ================================================================================
188 # Tokens
189  
190 # Note: In general, there needs to be a lot more support/analysis.
191 # Here, we simply assume that the token is a variable if there's no CD!!!
192 # See comments above about meaning.
193 # With the gradual refinement of meaning, in the lack of a mapping,
194 # we'll just presume that the cd defaults to latexml...
195 DefOpenMath('Token:?:?', sub {
196 my ($token) = @_;
197 if (my $meaning = $token->getAttribute('meaning')) {
198 my $cd = $token->getAttribute('omcd') || 'latexml';
199 return ['om:OMS', { name => $meaning, cd => $cd }]; }
200 else {
201 my ($name, %mmlattr) = LaTeXML::Post::MathML::stylizeContent($token, 'om:OMV');
202 if (my $mv = $mmlattr{mathvariant}) {
203 $name = $mv . "-" . $name; }
204 return ['om:OMV', { name => $name }]; } });
205  
206 # NOTE: Presence of '.' distinguishes float from int !?!?
207 DefOpenMath('Token:NUMBER:?', sub {
208 my ($node) = @_;
209 my $value = $node->getAttribute('meaning'); # name attribute (may) holds actual value.
210 $value = $node->textContent unless defined $value;
211 if ($value =~ /\./) {
212 return ['om:OMF', { dec => $value }]; }
213 else {
214 return ['om:OMI', {}, $value]; } });
215  
216 DefOpenMath('Token:SUPERSCRIPTOP:?', sub {
217 return ['om:OMS', { name => 'superscript', cd => 'ambiguous' }]; });
218 DefOpenMath('Token:SUBSCRIPTOP:?', sub {
219 return ['om:OMS', { name => 'subscript', cd => 'ambiguous' }]; });
220  
221 DefOpenMath("Token:?:\x{2062}", sub {
222 return ['om:OMS', { name => 'times', cd => 'arith1' }]; });
223  
224 # ================================================================================
225 # Applications.
226  
227 # Generic
228  
229 DefOpenMath('Apply:?:?', sub {
230 my ($op, @args) = @_;
231 return ['om:OMA', {}, map { om_expr($_) } $op, @args]; });
232  
233 # NOTE: No support for OMATTR here...
234  
235 # NOTE: Sketch of what OMBIND support might look like.
236 # Currently, no such construct is created in LaTeXML...
237 DefOpenMath('Apply:LambdaBinding:?', sub {
238 my ($op, $expr, @vars) = @_;
239 return ['om:OMBIND', {},
240 ['om:OMS', { name => "lambda", cd => 'fns1' },
241 ['om:OMBVAR', {}, map { om_expr($_) } @vars], # Presumably, these yield OMV
242 om_expr($expr)]]; });
243  
244 # ================================================================================
245 1;