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