| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML | # | ||
| 3 | # | Core Module for TeX conversion | # | ||
| 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 | package LaTeXML::Core; | ||
| 14 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 4 | ▶ | use LaTeXML::Global; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | #use LaTeXML::Common::Object; | ||
| 18 | 4 | ▶ | use LaTeXML::Common::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 4 | ▶ | use LaTeXML::Core::State; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 4 | ▶ | use LaTeXML::Core::Token; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 4 | ▶ | use LaTeXML::Core::Tokens; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | 4 | ▶ | use LaTeXML::Core::Stomach; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 23 | use LaTeXML::Core::Document; | ||
| 24 | use LaTeXML::Common::Model; | ||
| 25 | use LaTeXML::MathParser; | ||
| 26 | use LaTeXML::Util::Pathname; | ||
| 27 | use LaTeXML::Pre::BibTeX; | ||
| 28 | use LaTeXML::Package; # !!!! | ||
| 29 | use LaTeXML::Version; | ||
| 30 | use Encode; | ||
| 31 | use FindBin; | ||
| 32 | use base qw(LaTeXML::Common::Object); | ||
| 33 | |||
| 34 | #********************************************************************** | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my ($class, %options) = @_; | ||
| 38 | my $state = LaTeXML::Core::State->new(catcodes => 'standard', | ||
| 39 | stomach => LaTeXML::Core::Stomach->new(), | ||
| 40 | model => $options{model} || LaTeXML::Common::Model->new()); | ||
| 41 | $state->assignValue(VERBOSITY => (defined $options{verbosity} ? $options{verbosity} : 0), | ||
| 42 | 'global'); | ||
| 43 | $state->assignValue(STRICT => (defined $options{strict} ? $options{strict} : 0), | ||
| 44 | 'global'); | ||
| 45 | $state->assignValue(INCLUDE_COMMENTS => (defined $options{includecomments} ? $options{includecomments} : 1), | ||
| 46 | 'global'); | ||
| 47 | $state->assignValue(DOCUMENTID => (defined $options{documentid} ? $options{documentid} : ''), | ||
| 48 | 'global'); | ||
| 49 | $state->assignValue(SEARCHPATHS => [map { pathname_absolute(pathname_canonical($_)) } | ||
| 50 | '.', @{ $options{searchpaths} || [] }], | ||
| 51 | 'global'); | ||
| 52 | $state->assignValue(GRAPHICSPATHS => [map { pathname_absolute(pathname_canonical($_)) } | ||
| 53 | @{ $options{graphicspaths} || [] }], 'global'); | ||
| 54 | $state->assignValue(INCLUDE_STYLES => $options{includestyles} || 0, 'global'); | ||
| 55 | # Core has to ensure a default input encoding, and we default towards modern utf-8 documents | ||
| 56 | # This can be removed when all executables rely on LaTeXML::Common::Config | ||
| 57 | $options{inputencoding} = "utf-8" unless $options{inputencoding}; | ||
| 58 | $state->assignValue(PERL_INPUT_ENCODING => $options{inputencoding}); | ||
| 59 | $state->assignValue(NOMATHPARSE => $options{nomathparse} || 0, 'global'); | ||
| 60 | return bless { state => $state, | ||
| 61 | nomathparse => $options{nomathparse} || 0, | ||
| 62 | preload => $options{preload}, | ||
| 63 | }, $class; } | ||
| 64 | |||
| 65 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 66 | # High-level API. | ||
| 67 | |||
| 68 | sub convertAndWriteFile { | ||
| 69 | my ($self, $file) = @_; | ||
| 70 | $file =~ s/\.tex$//; | ||
| 71 | my $doc = $self->convertFile($file); | ||
| 72 | $doc->getDocument->toFile("$file.xml", 1) if $doc; | ||
| 73 | return $doc; } | ||
| 74 | |||
| 75 | sub convertFile { | ||
| 76 | my ($self, $file) = @_; | ||
| 77 | my $digested = $self->digestFile($file); | ||
| 78 | return unless $digested; | ||
| 79 | return $self->convertDocument($digested); } | ||
| 80 | |||
| 81 | sub getStatusMessage { | ||
| 82 | my ($self) = @_; | ||
| 83 | return $$self{state}->getStatusMessage; } | ||
| 84 | |||
| 85 | sub getStatusCode { | ||
| 86 | my ($self) = @_; | ||
| 87 | return $$self{state}->getStatusCode; } | ||
| 88 | |||
| 89 | # You'd typically do this after both digestion AND conversion... | ||
| 90 | sub showProfile { | ||
| 91 | my ($self, $digested) = @_; | ||
| 92 | return | ||
| 93 | $self->withState(sub { | ||
| 94 | LaTeXML::Core::Definition::showProfile(); # Show profile (if any) | ||
| 95 | }); } | ||
| 96 | |||
| 97 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
| 98 | # Mid-level API. | ||
| 99 | |||
| 100 | # options are currently being evolved to accomodate the Daemon: | ||
| 101 | # mode : the processing mode, ie the pool to preload: TeX or BibTeX | ||
| 102 | # noinitialize : if defined, it does not initialize State. | ||
| 103 | # preamble = names a tex file (or standard_preamble.tex) | ||
| 104 | # postamble = names a tex file (or standard_postamble.tex) | ||
| 105 | |||
| 106 | our %MODE_EXTENSION = ( # CONFIGURATION? | ||
| 107 | TeX => 'tex', LaTeX => 'tex', AmSTeX => 'tex', BibTeX => 'bib'); | ||
| 108 | |||
| 109 | sub digestFile { | ||
| 110 | my ($self, $request, %options) = @_; | ||
| 111 | my ($dir, $name, $ext); | ||
| 112 | my $mode = $options{mode} || 'TeX'; | ||
| 113 | if (pathname_is_literaldata($request)) { | ||
| 114 | $dir = undef; $ext = $MODE_EXTENSION{$mode}; | ||
| 115 | $name = "Anonymous String"; } | ||
| 116 | elsif (pathname_is_url($request)) { | ||
| 117 | $dir = undef; $ext = $MODE_EXTENSION{$mode}; | ||
| 118 | $name = $request; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | $request =~ s/\.\Q$MODE_EXTENSION{$mode}\E$//; | ||
| 122 | if (my $pathname = pathname_find($request, types => [$MODE_EXTENSION{$mode}, ''], | ||
| 123 | paths => $$self{state}->lookupValue('SEARCHPATHS'))) { | ||
| 124 | $request = $pathname; | ||
| 125 | ($dir, $name, $ext) = pathname_split($request); } | ||
| 126 | else { | ||
| 127 | $self->withState(sub { | ||
| 128 | Fatal('missing_file', $request, undef, "Can't find $mode file $request", | ||
| 129 | LaTeXML::Package::maybeReportSearchPaths() | ||
| 130 | ); }); } } | ||
| 131 | return | ||
| 132 | $self->withState(sub { | ||
| 133 | my ($state) = @_; | ||
| 134 | NoteBegin("Digesting $mode $name"); | ||
| 135 | $self->initializeState($mode . ".pool", @{ $$self{preload} || [] }) unless $options{noinitialize}; | ||
| 136 | $state->assignValue(SOURCEFILE => $request) if (!pathname_is_literaldata($request)); | ||
| 137 | $state->assignValue(SOURCEDIRECTORY => $dir) if defined $dir; | ||
| 138 | $state->unshiftValue(SEARCHPATHS => $dir) | ||
| 139 | if defined $dir && !grep { $_ eq $dir } @{ $state->lookupValue('SEARCHPATHS') }; | ||
| 140 | $state->unshiftValue(GRAPHICSPATHS => $dir) | ||
| 141 | |||
| 142 | if defined $dir && !grep { $_ eq $dir } @{ $state->lookupValue('GRAPHICSPATHS') }; | ||
| 143 | |||
| 144 | $state->installDefinition(LaTeXML::Core::Definition::Expandable->new(T_CS('\jobname'), undef, | ||
| 145 | Tokens(Explode($name)))); | ||
| 146 | # Reverse order, since last opened is first read! | ||
| 147 | $self->loadPostamble($options{postamble}) if $options{postamble}; | ||
| 148 | LaTeXML::Package::InputContent($request); | ||
| 149 | $self->loadPreamble($options{preamble}) if $options{preamble}; | ||
| 150 | |||
| 151 | # Now for the Hacky part for BibTeX!!! | ||
| 152 | if ($mode eq 'BibTeX') { | ||
| 153 | my $bib = LaTeXML::Pre::BibTeX->newFromGullet($name, $state->getStomach->getGullet); | ||
| 154 | LaTeXML::Package::InputContent("literal:" . $bib->toTeX); } | ||
| 155 | my $list = $self->finishDigestion; | ||
| 156 | NoteEnd("Digesting $mode $name"); | ||
| 157 | return $list; }); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub finishDigestion { | ||
| 161 | my ($self) = @_; | ||
| 162 | my $state = $$self{state}; | ||
| 163 | my $stomach = $state->getStomach; | ||
| 164 | my @stuff = (); | ||
| 165 | while ($stomach->getGullet->getMouth->hasMoreInput) { | ||
| 166 | push(@stuff, $stomach->digestNextBody); } | ||
| 167 | # Note that \end{document} will generally handle these cases as a Warning | ||
| 168 | if (my $env = $state->lookupValue('current_environment')) { | ||
| 169 | Error('expected', "\\end{$env}", $stomach, | ||
| 170 | "Input ended while environment $env was open"); } | ||
| 171 | my $ifstack = $state->lookupValue('if_stack'); | ||
| 172 | if ($ifstack && $$ifstack[0]) { | ||
| 173 | Error('expected', '\fi', $stomach, | ||
| 174 | "Input ended while conditional " . ToString($$ifstack[0]{token}) . " was incomplete", | ||
| 175 | "started at " . ToString($$ifstack[0]{start})); } | ||
| 176 | $stomach->getGullet->flush; | ||
| 177 | return List(@stuff); } | ||
| 178 | |||
| 179 | sub loadPreamble { | ||
| 180 | my ($self, $preamble) = @_; | ||
| 181 | my $gullet = $$self{state}->getStomach->getGullet; | ||
| 182 | if ($preamble eq 'standard_preamble.tex') { | ||
| 183 | $preamble = 'literal:\documentclass{article}\begin{document}'; } | ||
| 184 | return LaTeXML::Package::InputContent($preamble); } | ||
| 185 | |||
| 186 | sub loadPostamble { | ||
| 187 | my ($self, $postamble) = @_; | ||
| 188 | my $gullet = $$self{state}->getStomach->getGullet; | ||
| 189 | if ($postamble eq 'standard_postamble.tex') { | ||
| 190 | $postamble = 'literal:\end{document}'; } | ||
| 191 | return LaTeXML::Package::InputContent($postamble); } | ||
| 192 | |||
| 193 | sub convertDocument { | ||
| 194 | my ($self, $digested) = @_; | ||
| 195 | return | ||
| 196 | $self->withState(sub { | ||
| 197 | my ($state) = @_; | ||
| 198 | my $model = $state->getModel; # The document model. | ||
| 199 | my $document = LaTeXML::Core::Document->new($model); | ||
| 200 | local $LaTeXML::DOCUMENT = $document; | ||
| 201 | NoteBegin("Building"); | ||
| 202 | $model->loadSchema(); # If needed? | ||
| 203 | if (my $paths = $state->lookupValue('SEARCHPATHS')) { | ||
| 204 | if ($state->lookupValue('INCLUDE_COMMENTS')) { | ||
| 205 | $document->insertPI('latexml', searchpaths => join(',', @$paths)); } } | ||
| 206 | foreach my $preload_by_reference (@{ $$self{preload} }) { | ||
| 207 | my $preload = $preload_by_reference; # copy preload value, as we want to preserve the hash as-is, for (potential) future daemon calls | ||
| 208 | next if $preload =~ /\.pool$/; | ||
| 209 | my $options = undef; # Stupid perlcritic policy | ||
| 210 | if ($preload =~ s/^\[([^\]]*)\]//) { $options = $1; } | ||
| 211 | if ($preload =~ s/\.cls$//) { | ||
| 212 | $document->insertPI('latexml', class => $preload, ($options ? (options => $options) : ())); } | ||
| 213 | else { | ||
| 214 | $preload =~ s/\.sty$//; | ||
| 215 | $document->insertPI('latexml', package => $preload, ($options ? (options => $options) : ())); } } | ||
| 216 | { no warnings 'recursion'; | ||
| 217 | $document->absorb($digested); } | ||
| 218 | NoteEnd("Building"); | ||
| 219 | |||
| 220 | if (my $rules = $state->lookupValue('DOCUMENT_REWRITE_RULES')) { | ||
| 221 | NoteBegin("Rewriting"); | ||
| 222 | $document->markXMNodeVisibility; | ||
| 223 | foreach my $rule (@$rules) { | ||
| 224 | $rule->rewrite($document, $document->getDocument->documentElement); } | ||
| 225 | NoteEnd("Rewriting"); } | ||
| 226 | |||
| 227 | LaTeXML::MathParser->new(lexematize => $state->lookupValue('LEXEMATIZE_MATH'))->parseMath($document) unless $$self{nomathparse}; | ||
| 228 | NoteBegin("Finalizing"); | ||
| 229 | my $xmldoc = $document->finalize(); | ||
| 230 | NoteEnd("Finalizing"); | ||
| 231 | return $xmldoc; }); } | ||
| 232 | |||
| 233 | sub withState { | ||
| 234 | my ($self, $closure) = @_; | ||
| 235 | local $STATE = $$self{state}; | ||
| 236 | # And, set fancy error handler for ANY die! | ||
| 237 | local $SIG{__DIE__} = \&LaTeXML::Common::Error::perl_die_handler; | ||
| 238 | local $SIG{INT} = \&LaTeXML::Common::Error::perl_interrupt_handler; | ||
| 239 | local $SIG{__WARN__} = \&LaTeXML::Common::Error::perl_warn_handler; | ||
| 240 | local $SIG{'ALRM'} = \&LaTeXML::Common::Error::perl_timeout_handler; | ||
| 241 | local $SIG{'TERM'} = \&LaTeXML::Common::Error::perl_terminate_handler; | ||
| 242 | |||
| 243 | local $LaTeXML::DUAL_BRANCH = ''; | ||
| 244 | |||
| 245 | return &$closure($STATE); } | ||
| 246 | |||
| 247 | sub initializeState { | ||
| 248 | my ($self, @files) = @_; | ||
| 249 | my $state = $$self{state}; | ||
| 250 | my $stomach = $state->getStomach; # The current Stomach; | ||
| 251 | my $gullet = $stomach->getGullet; | ||
| 252 | $stomach->initialize; | ||
| 253 | my $paths = $state->lookupValue('SEARCHPATHS'); | ||
| 254 | $state->assignValue('InitialPreloads' => 1, 'global'); | ||
| 255 | foreach my $preload (@files) { | ||
| 256 | my ($options, $type); | ||
| 257 | $options = $1 if $preload =~ s/^\[([^\]]*)\]//; | ||
| 258 | $type = ($preload =~ s/\.(\w+)$// ? $1 : 'sty'); | ||
| 259 | my $handleoptions = ($type eq 'sty') || ($type eq 'cls'); | ||
| 260 | if ($options) { | ||
| 261 | if ($handleoptions) { | ||
| 262 | $options = [split(/,/, $options)]; } | ||
| 263 | else { | ||
| 264 | Warn('unexpected', 'options', | ||
| 265 | "Attempting to pass options to $preload.$type (not style or class)", | ||
| 266 | "The options were [$options]"); } } | ||
| 267 | # Attach extension back if HTTP protocol: | ||
| 268 | if (pathname_is_url($preload)) { | ||
| 269 | $preload .= '.' . $type; | ||
| 270 | } | ||
| 271 | LaTeXML::Package::InputDefinitions($preload, type => $type, | ||
| 272 | handleoptions => $handleoptions, options => $options); | ||
| 273 | } | ||
| 274 | $state->assignValue('InitialPreloads' => undef, 'global'); | ||
| 275 | return; } | ||
| 276 | |||
| 277 | sub writeDOM { | ||
| 278 | my ($self, $dom, $name) = @_; | ||
| 279 | $dom->toFile("$name.xml", 1); | ||
| 280 | return 1; } | ||
| 281 | |||
| 282 | #********************************************************************** | ||
| 283 | # Should post processing be managed from here too? | ||
| 284 | # Problem: with current DOM setup, I pretty much have to write the | ||
| 285 | # file and reread it anyway... | ||
| 286 | # Also, want to inhibit loading an extreme number of classes if not needed. | ||
| 287 | #********************************************************************** | ||
| 288 | 1; | ||
| 289 | |||
| 290 | __END__ | ||