| 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 | sub canConvert { | ||
| 99 | my ($sefl, $doc, $math) = @_; | ||
| 100 | return LaTeXML::Post::MathProcessor::mathIsParsed($doc, $math); } | ||
| 101 | |||
| 102 | # ================================================================================ | ||
| 103 | |||
| 104 | # DANGER!!! Ths accumulate all the DefMathML declarations. | ||
| 105 | # They're fixed after the module has been loaded, so are Daemon Safe, | ||
| 106 | # but probably should be going into (post) STATE, so that they are extensible. | ||
| 107 | our $OMTable = {}; | ||
| 108 | |||
| 109 | sub DefOpenMath { | ||
| 110 | my ($key, $sub) = @_; | ||
| 111 | $$OMTable{$key} = $sub; | ||
| 112 | return; } | ||
| 113 | |||
| 114 | sub om_expr { | ||
| 115 | my ($node) = @_; | ||
| 116 | # Get the real node, first. | ||
| 117 | $node = $LaTeXML::Post::DOCUMENT->realizeXMNode($node); | ||
| 118 | my $result = om_expr_aux($node); | ||
| 119 | # Associate the generated node with the source XMath node. | ||
| 120 | $LaTeXML::Post::MATHPROCESSOR->associateNode($result, $node); | ||
| 121 | return $result; } | ||
| 122 | |||
| 123 | # Is it clear that we should just getAttribute('role'), | ||
| 124 | # instead of the getOperatorRole like in MML? | ||
| 125 | sub om_expr_aux { | ||
| 126 | my ($node) = @_; | ||
| 127 | return OMError("Missing Subexpression") unless $node; | ||
| 128 | my $tag = getQName($node); | ||
| 129 | if (($tag eq 'ltx:XMWrap') || ($tag eq 'ltx:XMArg')) { # Unparsed | ||
| 130 | my ($item, @rest) = element_nodes($node); | ||
| 131 | return (!$item || @rest ? om_unparsed($item, @rest) : om_expr($item)); } | ||
| 132 | elsif ($tag eq 'ltx:XMDual') { | ||
| 133 | my ($content, $presentation) = element_nodes($node); | ||
| 134 | return om_expr($content); } | ||
| 135 | elsif ($tag eq 'ltx:XMApp') { | ||
| 136 | if (my $meaning = $node->getAttribute('meaning')) { | ||
| 137 | my $sub = lookupConverter('Token', $node->getAttribute('role'), $meaning); | ||
| 138 | return &$sub($node); } | ||
| 139 | my ($op, @args) = element_nodes($node); | ||
| 140 | return OMError("Missing Operator") unless $op; | ||
| 141 | my $sub = lookupConverter('Apply', $op->getAttribute('role'), $op->getAttribute('meaning')); | ||
| 142 | return &$sub($op, @args); } | ||
| 143 | elsif ($tag eq 'ltx:XMTok') { | ||
| 144 | my $sub = lookupConverter('Token', $node->getAttribute('role'), $node->getAttribute('meaning')); | ||
| 145 | return &$sub($node); } | ||
| 146 | elsif ($tag eq 'ltx:XMHint') { | ||
| 147 | return (); } | ||
| 148 | elsif ($tag eq 'ltx:XMText') { | ||
| 149 | if (scalar(element_nodes($node))) { # If it has markup? | ||
| 150 | return ['om:OMATTR', {}, | ||
| 151 | ['om:OMATP', {}, | ||
| 152 | ['om:OMS', { cd => 'OMDoc', name => 'verbalizes' }], | ||
| 153 | ['om:OMFOREIGN', { encoding => 'mtext' }, | ||
| 154 | $LaTeXML::Post::MATHPROCESSOR->convertXMTextContent($LaTeXML::Post::DOCUMENT, 0, | ||
| 155 | $node->childNodes)]], | ||
| 156 | ['om:OMS', { cd => 'OMDoc', name => 'infObj' }]] } | ||
| 157 | else { | ||
| 158 | return ['om:OMSTR', {}, $node->textContent]; } } | ||
| 159 | else { | ||
| 160 | return ['om:OMSTR', {}, $node->textContent]; } } | ||
| 161 | |||
| 162 | sub om_unparsed { | ||
| 163 | my (@nodes) = @_; | ||
| 164 | if (!@nodes) { | ||
| 165 | return ['om:OME', {}, | ||
| 166 | ['om:OMS', { name => 'unexpected', cd => 'moreerrors' }], | ||
| 167 | ['om:OMSTR', {}, "Missing Subexpression"]]; } | ||
| 168 | else { | ||
| 169 | my @om = (); | ||
| 170 | foreach my $node (@nodes) { | ||
| 171 | $node = $LaTeXML::Post::DOCUMENT->realizeXMNode($node); | ||
| 172 | my $tag = getQName($node); | ||
| 173 | if ($tag eq 'ltx:XMHint') { } | ||
| 174 | elsif (($tag eq 'ltx:XMTok') && (($node->getAttribute('role') || 'UNKNOWN') eq 'UNKNOWN')) { | ||
| 175 | push(@om, ['om:OMS', { cd => 'unknown', name => $node->textContent }]); } | ||
| 176 | else { | ||
| 177 | push(@om, om_expr_aux($node)); } } | ||
| 178 | return ['om:OME', {}, ['om:OMS', { cd => 'ambiguous', name => 'fragments' }], @om]; } } | ||
| 179 | |||
| 180 | sub lookupConverter { | ||
| 181 | my ($mode, $role, $name) = @_; | ||
| 182 | $name = '?' unless $name; | ||
| 183 | $role = '?' unless $role; | ||
| 184 | return $$OMTable{"$mode:$role:$name"} || $$OMTable{"$mode:?:$name"} | ||
| 185 | || $$OMTable{"$mode:$role:?"} || $$OMTable{"$mode:?:?"}; } | ||
| 186 | |||
| 187 | # ================================================================================ | ||
| 188 | # Helpers | ||
| 189 | sub OMError { | ||
| 190 | my ($msg) = @_; | ||
| 191 | return ['om:OME', {}, | ||
| 192 | ['om:OMS', { name => 'unexpected', cd => 'moreerrors' }], | ||
| 193 | ['om:OMS', {}, $msg]]; } | ||
| 194 | # ================================================================================ | ||
| 195 | # Tokens | ||
| 196 | |||
| 197 | # Note: In general, there needs to be a lot more support/analysis. | ||
| 198 | # Here, we simply assume that the token is a variable if there's no CD!!! | ||
| 199 | # See comments above about meaning. | ||
| 200 | # With the gradual refinement of meaning, in the lack of a mapping, | ||
| 201 | # we'll just presume that the cd defaults to latexml... | ||
| 202 | DefOpenMath('Token:?:?', sub { | ||
| 203 | my ($token) = @_; | ||
| 204 | if (my $meaning = $token->getAttribute('meaning')) { | ||
| 205 | my $cd = $token->getAttribute('omcd') || 'latexml'; | ||
| 206 | return ['om:OMS', { name => $meaning, cd => $cd }]; } | ||
| 207 | else { | ||
| 208 | my ($name, %mmlattr) = LaTeXML::Post::MathML::stylizeContent($token, 'om:OMV'); | ||
| 209 | if (my $mv = $mmlattr{mathvariant}) { | ||
| 210 | $name = $mv . "-" . $name; } | ||
| 211 | return ['om:OMV', { name => $name }]; } }); | ||
| 212 | |||
| 213 | # NOTE: Presence of '.' distinguishes float from int !?!? | ||
| 214 | DefOpenMath('Token:NUMBER:?', sub { | ||
| 215 | my ($node) = @_; | ||
| 216 | my $value = $node->getAttribute('meaning'); # name attribute (may) holds actual value. | ||
| 217 | $value = $node->textContent unless defined $value; | ||
| 218 | if ($value =~ /\./) { | ||
| 219 | return ['om:OMF', { dec => $value }]; } | ||
| 220 | else { | ||
| 221 | return ['om:OMI', {}, $value]; } }); | ||
| 222 | |||
| 223 | DefOpenMath('Token:SUPERSCRIPTOP:?', sub { | ||
| 224 | return ['om:OMS', { name => 'superscript', cd => 'ambiguous' }]; }); | ||
| 225 | DefOpenMath('Token:SUBSCRIPTOP:?', sub { | ||
| 226 | return ['om:OMS', { name => 'subscript', cd => 'ambiguous' }]; }); | ||
| 227 | |||
| 228 | DefOpenMath("Token:?:\x{2062}", sub { | ||
| 229 | return ['om:OMS', { name => 'times', cd => 'arith1' }]; }); | ||
| 230 | |||
| 231 | # ================================================================================ | ||
| 232 | # Applications. | ||
| 233 | |||
| 234 | # Generic | ||
| 235 | |||
| 236 | DefOpenMath('Apply:?:?', sub { | ||
| 237 | my ($op, @args) = @_; | ||
| 238 | return ['om:OMA', {}, map { om_expr($_) } $op, @args]; }); | ||
| 239 | |||
| 240 | # NOTE: No support for OMATTR here... | ||
| 241 | |||
| 242 | # NOTE: Sketch of what OMBIND support might look like. | ||
| 243 | # Currently, no such construct is created in LaTeXML... | ||
| 244 | DefOpenMath('Apply:LambdaBinding:?', sub { | ||
| 245 | my ($op, $expr, @vars) = @_; | ||
| 246 | return ['om:OMBIND', {}, | ||
| 247 | ['om:OMS', { name => "lambda", cd => 'fns1' }, | ||
| 248 | ['om:OMBVAR', {}, map { om_expr($_) } @vars], # Presumably, these yield OMV | ||
| 249 | om_expr($expr)]]; }); | ||
| 250 | |||
| 251 | # ================================================================================ | ||
| 252 | 1; |