| 1 | # /=====================================================================\ # | ||||||||||||||||||||||||||||||
| 2 | # | LaTeXML::Post | # | ||||||||||||||||||||||||||||||
| 3 | # | PostProcessing driver | # | ||||||||||||||||||||||||||||||
| 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::Post; | ||||||||||||||||||||||||||||||
| 14 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 28 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | 28 | ▶ | use LaTeXML::Util::Radix; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 18 | 28 | ▶ | use Encode; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 28 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 20 | 28 | ▶ | use base qw(LaTeXML::Common::Object); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 21 | 28 | ▶ | use LaTeXML::Global; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | 28 | ▶ | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 23 | 28 | ▶ | use LaTeXML::Core::State; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | our @EXPORT = (@LaTeXML::Common::Error::EXPORT); | ||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 27 | 0 | my ($class, %options) = @_; | |||||||||||||||||||||||||||||
| 28 | 0 | my $self = bless { status => {}, %options }, $class; | |||||||||||||||||||||||||||||
| 29 | # TEMPORARY HACK!!!! | ||||||||||||||||||||||||||||||
| 30 | # Create a State object, for config & recursive conversions; | ||||||||||||||||||||||||||||||
| 31 | # Eventually should be managed higher up. | ||||||||||||||||||||||||||||||
| 32 | 0 | $$self{state} = LaTeXML::Core::State->new(); | |||||||||||||||||||||||||||||
| 33 | 0 | return $self; } | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | #====================================================================== | ||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub ProcessChain { | ||||||||||||||||||||||||||||
|
Subroutine
ProcessChain: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 37 | 0 | my ($self, $doc, @postprocessors) = @_; | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return $self->withState(sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 39 | 0 | return $self->ProcessChain_internal($doc, @postprocessors); }); } | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub ProcessChain_internal { | ||||||||||||||||||||||||||||
|
Subroutine
ProcessChain_internal: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||
| 42 | 0 | my ($self, $doc, @postprocessors) = @_; | |||||||||||||||||||||||||||||
| 43 | 0 | local $LaTeXML::POST = $self; | |||||||||||||||||||||||||||||
| 44 | 0 | local $LaTeXML::Post::NOTEINFO = undef; | |||||||||||||||||||||||||||||
| 45 | 0 | local $LaTeXML::Post::DOCUMENT = $doc; | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | 0 | my @docs = ($doc); | |||||||||||||||||||||||||||||
| 48 | 0 | ProgressSpinup("post-processing"); | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | 0 | foreach my $processor (@postprocessors) { | |||||||||||||||||||||||||||||
| 51 | 0 | local $LaTeXML::Post::PROCESSOR = $processor; | |||||||||||||||||||||||||||||
| 52 | 0 | my @newdocs = (); | |||||||||||||||||||||||||||||
| 53 | 0 | foreach my $doc (@docs) { | |||||||||||||||||||||||||||||
| 54 | 0 | local $LaTeXML::Post::DOCUMENT = $doc; | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (my @nodes = grep { $_ } $processor->toProcess($doc)) { # If there are nodes to process | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | 0 | my $n = scalar(@nodes); | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $msg = join(' ', $processor->getName || '', | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$processor->getName || ''
$doc->siteRelativeDestination || ''
MC/DC0%0/1
$processor->getName || ''
$processor->getName
MC/DC0%0/1
$doc->siteRelativeDestination || ''
$doc->siteRelativeDestination
Truth table0%0/2
A
A= $processor->getName
Truth table0%0/2
A
A= $doc->siteRelativeDestination
| |||||||||||||||||||||||||||||||
| 58 | $doc->siteRelativeDestination || '', | ||||||||||||||||||||||||||||||
| 59 | ($n > 1 ? "$n to process" : 'processing')); | ||||||||||||||||||||||||||||||
| 60 | 0 | ProgressSpinup($msg); | |||||||||||||||||||||||||||||
| 61 | 0 | push(@newdocs, $processor->process($doc, @nodes)); | |||||||||||||||||||||||||||||
| 62 | 0 | ProgressSpindown($msg); } | |||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||||
| 64 | 0 | push(@newdocs, $doc); } } | |||||||||||||||||||||||||||||
| 65 | 0 | @docs = @newdocs; } | |||||||||||||||||||||||||||||
| 66 | 0 | ProgressSpindown("post-processing"); | |||||||||||||||||||||||||||||
| 67 | 0 | return @docs; } | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | ## HACK!!! | ||||||||||||||||||||||||||||||
| 70 | ## This is a copy of withState from LaTeXML::Core.pm | ||||||||||||||||||||||||||||||
| 71 | ## This should eventually be in a higher level, common class | ||||||||||||||||||||||||||||||
| 72 | ## using a common State or configuration object | ||||||||||||||||||||||||||||||
| 73 | ## in order to wrap ALL processing. | ||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub withState { | ||||||||||||||||||||||||||||
|
Subroutine
withState: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 75 | 0 | my ($self, $closure) = @_; | |||||||||||||||||||||||||||||
| 76 | 0 | local $STATE = $$self{state}; | |||||||||||||||||||||||||||||
| 77 | # And, set fancy error handler for ANY die! | ||||||||||||||||||||||||||||||
| 78 | 0 | local $SIG{__DIE__} = \&LaTeXML::Common::Error::perl_die_handler; | |||||||||||||||||||||||||||||
| 79 | 0 | local $SIG{INT} = \&LaTeXML::Common::Error::perl_interrupt_handler; | |||||||||||||||||||||||||||||
| 80 | 0 | local $SIG{__WARN__} = \&LaTeXML::Common::Error::perl_warn_handler; | |||||||||||||||||||||||||||||
| 81 | 0 | local $SIG{'ALRM'} = \&LaTeXML::Common::Error::perl_timeout_handler; | |||||||||||||||||||||||||||||
| 82 | 0 | local $SIG{'TERM'} = \&LaTeXML::Common::Error::perl_terminate_handler; | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 0 | local $LaTeXML::DUAL_BRANCH = ''; | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 0 | return &$closure($STATE); } | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub getStatusCode { | ||||||||||||||||||||||||||||
|
Subroutine
getStatusCode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 89 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 90 | 0 | return $$self{state}->getStatusCode; } | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub getStatusMessage { | ||||||||||||||||||||||||||||
|
Subroutine
getStatusMessage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||
| 94 | 0 | return $$self{state}->getStatusMessage; } | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | #====================================================================== | ||||||||||||||||||||||||||||||
| 97 | # "Global" Post processing services | ||||||||||||||||||||||||||||||
| 98 | #====================================================================== | ||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | # Return a sorter appropriate for lang (if Unicode::Collate::Locale available), | ||||||||||||||||||||||||||||||
| 101 | # or an undifferentiated Unicode sorter (if only Unicode::Collate is available), | ||||||||||||||||||||||||||||||
| 102 | # or just a dumb stand-in for perl's sort | ||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub getsorter { | ||||||||||||||||||||||||||||
|
Subroutine
getsorter: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 104 | 0 | my ($self, $lang) = @_; | |||||||||||||||||||||||||||||
| 105 | 0 | my $collator; | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($collator = $$self{collatorcache}{$lang}) { } | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 107 | elsif ($collator = eval { | ||||||||||||||||||||||||||||||
| 108 | 0 | local $LaTeXML::IGNORE_ERRORS = 1; | |||||||||||||||||||||||||||||
| 109 | 0 | require 'Unicode/Collate/Locale.pm'; | |||||||||||||||||||||||||||||
| 110 | 0 | Unicode::Collate::Locale->new( | |||||||||||||||||||||||||||||
| 111 | locale => $lang, | ||||||||||||||||||||||||||||||
| 112 | variable => 'non-ignorable', # I think; at least space shouldn't be ignored | ||||||||||||||||||||||||||||||
| 113 | upper_before_lower => 1); }) { } | ||||||||||||||||||||||||||||||
| 114 | elsif ($collator = eval { | ||||||||||||||||||||||||||||||
| 115 | 0 | local $LaTeXML::IGNORE_ERRORS = 1; | |||||||||||||||||||||||||||||
| 116 | 0 | require 'Unicode/Collate.pm'; | |||||||||||||||||||||||||||||
| 117 | 0 | Unicode::Collate->new( | |||||||||||||||||||||||||||||
| 118 | variable => 'non-ignorable', # I think; at least space shouldn't be ignored | ||||||||||||||||||||||||||||||
| 119 | upper_before_lower => 1); }) { | ||||||||||||||||||||||||||||||
| 120 | 0 | Info('expected', 'Unicode::Collate::Locale', undef, | |||||||||||||||||||||||||||||
| 121 | "No Unicode::Collate::Locale found;", | ||||||||||||||||||||||||||||||
| 122 | "using Unicode::Collate; ignoring language='$lang'"); } | ||||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||||
| 124 | # Otherwise, just use primitive codepoint ordering. | ||||||||||||||||||||||||||||||
| 125 | 0 | $collator = LaTeXML::Post::DumbCollator->new(); | |||||||||||||||||||||||||||||
| 126 | 0 | Info('expected', 'Unicode::Collate::Locale', undef, | |||||||||||||||||||||||||||||
| 127 | "No Unicode::Collate::Locale or Unicode::Collate", | ||||||||||||||||||||||||||||||
| 128 | "using perl's sort; ignoring language='$lang'"); } | ||||||||||||||||||||||||||||||
| 129 | 0 | $$self{collatorcache}{$lang} = $collator; | |||||||||||||||||||||||||||||
| 130 | 0 | return $collator; } | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||||
| 133 | package LaTeXML::Post::DumbCollator; | ||||||||||||||||||||||||||||||
| 134 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 137 | 0 | my ($class) = @_; | |||||||||||||||||||||||||||||
| 138 | 0 | return bless {}, $class; } | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub sort { | ||||||||||||||||||||||||||||
|
Subroutine
sort: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 141 | 0 | my ($self, @things) = @_; | |||||||||||||||||||||||||||||
| 142 | 0 | return (sort @things); } | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||||
| 145 | package LaTeXML::Post::Processor; | ||||||||||||||||||||||||||||||
| 146 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 147 | 28 | ▶ | use LaTeXML::Post; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 148 | 28 | ▶ | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | use LaTeXML::Common::XML; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||
| 150 | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||||||
| 151 | use base qw(LaTeXML::Common::Object); | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | # An Abstract Post Processor | ||||||||||||||||||||||||||||||
| 154 | sub new { | ||||||||||||||||||||||||||||||
| 155 | my ($class, %options) = @_; | ||||||||||||||||||||||||||||||
| 156 | my $self = bless {%options}, $class; | ||||||||||||||||||||||||||||||
| 157 | $$self{resource_directory} = $options{resource_directory}; | ||||||||||||||||||||||||||||||
| 158 | $$self{resource_prefix} = $options{resource_prefix}; | ||||||||||||||||||||||||||||||
| 159 | my $name = $class; $name =~ s/^LaTeXML::Post:://; | ||||||||||||||||||||||||||||||
| 160 | $$self{name} = $name; | ||||||||||||||||||||||||||||||
| 161 | return $self; } | ||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | sub getName { | ||||||||||||||||||||||||||||||
| 164 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 165 | return $$self{name}; } | ||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | # Return the nodes to be processed; by default the document element. | ||||||||||||||||||||||||||||||
| 168 | # This allows processors to focus on specific kinds of nodes, | ||||||||||||||||||||||||||||||
| 169 | # or to skip processing if there are none to process. | ||||||||||||||||||||||||||||||
| 170 | sub toProcess { | ||||||||||||||||||||||||||||||
| 171 | my ($self, $doc) = @_; | ||||||||||||||||||||||||||||||
| 172 | return $doc->getDocumentElement; } | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | # This must be defined to do whatever processing is needed to @toprocess nodes. | ||||||||||||||||||||||||||||||
| 175 | sub process { | ||||||||||||||||||||||||||||||
| 176 | my ($self, $doc, @toprocess) = @_; | ||||||||||||||||||||||||||||||
| 177 | Fatal("misdefined", $self, $doc, "This post-processor is abstract; does not implement ->process"); | ||||||||||||||||||||||||||||||
| 178 | return $doc; } | ||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | #====================================================================== | ||||||||||||||||||||||||||||||
| 181 | # Some postprocessors will want to create a bunch of "resource"s, | ||||||||||||||||||||||||||||||
| 182 | # such as generated or transformed image files, or other data files. | ||||||||||||||||||||||||||||||
| 183 | # These should return a pathname, relative to the document's destination, | ||||||||||||||||||||||||||||||
| 184 | # for storing a resource associated with $node. | ||||||||||||||||||||||||||||||
| 185 | # Will use the Post option resource_directory | ||||||||||||||||||||||||||||||
| 186 | sub desiredResourcePathname { | ||||||||||||||||||||||||||||||
| 187 | my ($self, $doc, $node, $source, $type) = @_; | ||||||||||||||||||||||||||||||
| 188 | return; } | ||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | # Ideally this would return a pathname relative to the document | ||||||||||||||||||||||||||||||
| 191 | # but I think we've accommodated absolute ones. | ||||||||||||||||||||||||||||||
| 192 | sub generateResourcePathname { | ||||||||||||||||||||||||||||||
| 193 | my ($self, $doc, $node, $source, $type) = @_; | ||||||||||||||||||||||||||||||
| 194 | my $subdir = $$self{resource_directory} || ''; | ||||||||||||||||||||||||||||||
| 195 | my $prefix = $$self{resource_prefix} || "x"; | ||||||||||||||||||||||||||||||
| 196 | my $counter = join('_', "_max", $subdir, $prefix, "counter_"); | ||||||||||||||||||||||||||||||
| 197 | my $n = $doc->cacheLookup($counter) || 0; | ||||||||||||||||||||||||||||||
| 198 | my $name = $prefix . ++$n; | ||||||||||||||||||||||||||||||
| 199 | $doc->cacheStore($counter, $n); | ||||||||||||||||||||||||||||||
| 200 | return pathname_make(dir => $subdir, name => $name, type => $type); } | ||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||
| 202 | # Returns a two-part list of the form: | ||||||||||||||||||||||||||||||
| 203 | # | ||||||||||||||||||||||||||||||
| 204 | # [class, classoptions, oldstyle], [package1,package1options], [package2,package2options], ... | ||||||||||||||||||||||||||||||
| 205 | # | ||||||||||||||||||||||||||||||
| 206 | # Where there first element is always the *class* (if none, "article" returned as a default) | ||||||||||||||||||||||||||||||
| 207 | # And all following elements are *packages* | ||||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||||
| 209 | sub find_documentclass_and_packages { | ||||||||||||||||||||||||||||||
| 210 | my ($self, $doc) = @_; | ||||||||||||||||||||||||||||||
| 211 | my ($class, $classoptions, $oldstyle, @packages); | ||||||||||||||||||||||||||||||
| 212 | foreach my $pi ($doc->findnodes(".//processing-instruction('latexml')")) { | ||||||||||||||||||||||||||||||
| 213 | my $data = $pi->textContent; | ||||||||||||||||||||||||||||||
| 214 | my $entry = {}; | ||||||||||||||||||||||||||||||
| 215 | while ($data =~ s/\s*([\w\-\_]*)=([\"\'])(.*?)\2//) { | ||||||||||||||||||||||||||||||
| 216 | $$entry{$1} = $3; } | ||||||||||||||||||||||||||||||
| 217 | if ($$entry{class}) { | ||||||||||||||||||||||||||||||
| 218 | $class = $$entry{class}; | ||||||||||||||||||||||||||||||
| 219 | $classoptions = $$entry{options} || 'onecolumn'; | ||||||||||||||||||||||||||||||
| 220 | $oldstyle = $$entry{oldstyle}; } | ||||||||||||||||||||||||||||||
| 221 | elsif ($$entry{package}) { | ||||||||||||||||||||||||||||||
| 222 | my @p = grep { $_; } split(/\s*,\s*/, $$entry{package}); | ||||||||||||||||||||||||||||||
| 223 | foreach my $package (@p) { | ||||||||||||||||||||||||||||||
| 224 | push(@packages, [$package, $$entry{options} || '']); } } } | ||||||||||||||||||||||||||||||
| 225 | if (!$class) { | ||||||||||||||||||||||||||||||
| 226 | Warn('expected', 'class', undef, "No document class found; using article"); | ||||||||||||||||||||||||||||||
| 227 | $class = 'article'; } | ||||||||||||||||||||||||||||||
| 228 | return ([$class, $classoptions, $oldstyle], @packages); } | ||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | sub find_preambles { | ||||||||||||||||||||||||||||||
| 231 | my ($self, $doc) = @_; | ||||||||||||||||||||||||||||||
| 232 | my @preambles = (); | ||||||||||||||||||||||||||||||
| 233 | foreach my $pi ($doc->findnodes(".//processing-instruction('latexml')")) { | ||||||||||||||||||||||||||||||
| 234 | my $data = $pi->textContent; | ||||||||||||||||||||||||||||||
| 235 | while ($data =~ s/\s*([\w\-\_]*)=([\"\'])(.*?)\2//) { | ||||||||||||||||||||||||||||||
| 236 | if ($1 eq 'preamble') { | ||||||||||||||||||||||||||||||
| 237 | push(@preambles, $3); } } } | ||||||||||||||||||||||||||||||
| 238 | return join("\n", @preambles); } | ||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | # Utility to Copy any foreign attributes from $node onto the $newnode | ||||||||||||||||||||||||||||||
| 241 | # Allow both $node & $newnode to be XML Elements ore array representation | ||||||||||||||||||||||||||||||
| 242 | sub copy_foreign_attributes { | ||||||||||||||||||||||||||||||
| 243 | my ($self, $newnode, $node) = @_; | ||||||||||||||||||||||||||||||
| 244 | my %attr = (); | ||||||||||||||||||||||||||||||
| 245 | if (ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 246 | %attr = %{ $$node[1] }; } | ||||||||||||||||||||||||||||||
| 247 | else { | ||||||||||||||||||||||||||||||
| 248 | foreach my $attr ($node->attributes()) { | ||||||||||||||||||||||||||||||
| 249 | if ($attr->nodeType == XML_ATTRIBUTE_NODE) { | ||||||||||||||||||||||||||||||
| 250 | $attr{ $attr->nodeName } = $attr->getValue; } } } | ||||||||||||||||||||||||||||||
| 251 | foreach my $key (keys %attr) { | ||||||||||||||||||||||||||||||
| 252 | next if $key =~ /^xml:/; | ||||||||||||||||||||||||||||||
| 253 | next unless $key =~ /:/; | ||||||||||||||||||||||||||||||
| 254 | my $value = $attr{$key}; | ||||||||||||||||||||||||||||||
| 255 | if (ref $newnode eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 256 | $$newnode[1]{$key} = $value unless defined $$newnode[1]{$key}; } | ||||||||||||||||||||||||||||||
| 257 | else { | ||||||||||||||||||||||||||||||
| 258 | $newnode->setAttribute($key => $value) unless $newnode->hasAttribute($key); } } | ||||||||||||||||||||||||||||||
| 259 | return; } | ||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||
| 261 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||||
| 262 | package LaTeXML::Post::MathProcessor; | ||||||||||||||||||||||||||||||
| 263 | use strict; | ||||||||||||||||||||||||||||||
| 264 | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||||||
| 265 | use LaTeXML::Post; | ||||||||||||||||||||||||||||||
| 266 | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||||||
| 267 | use base qw(LaTeXML::Post::Processor); | ||||||||||||||||||||||||||||||
| 268 | use LaTeXML::Common::XML; | ||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||
| 270 | # This is an abstract class; A complete MathProcessor will need to define: | ||||||||||||||||||||||||||||||
| 271 | # $self->convertNode($doc,$xmath) | ||||||||||||||||||||||||||||||
| 272 | # to generate the converted math node | ||||||||||||||||||||||||||||||
| 273 | # $self->combineParallel($doc,$math,$primary,@secondaries) | ||||||||||||||||||||||||||||||
| 274 | # to combine the $primary (the result of $self's conversion of $math) | ||||||||||||||||||||||||||||||
| 275 | # with the results of other math processors to create the | ||||||||||||||||||||||||||||||
| 276 | # parallel markup appropriate for this processor's markup. | ||||||||||||||||||||||||||||||
| 277 | # $self->rawIDSuffix returns a short string to append to id's for nodes | ||||||||||||||||||||||||||||||
| 278 | # using this markup. | ||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||
| 280 | # Top level processing finds and converts all top-level math nodes. | ||||||||||||||||||||||||||||||
| 281 | # Any nested mathnodes would only appear within ltx:XMText; | ||||||||||||||||||||||||||||||
| 282 | # postprocessors must handle nested math as appropriate (but see convertXMTextContent) | ||||||||||||||||||||||||||||||
| 283 | # Invokes preprocess on each before doing the conversion in case | ||||||||||||||||||||||||||||||
| 284 | # analysis is needed. | ||||||||||||||||||||||||||||||
| 285 | sub toProcess { | ||||||||||||||||||||||||||||||
| 286 | my ($self, $doc) = @_; | ||||||||||||||||||||||||||||||
| 287 | return $doc->findnodes('//ltx:Math[not(ancestor::ltx:Math)]'); } | ||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||
| 289 | sub process { | ||||||||||||||||||||||||||||||
| 290 | my ($self, $doc, @maths) = @_; | ||||||||||||||||||||||||||||||
| 291 | local $LaTeXML::Post::MATHPROCESSOR = $self; | ||||||||||||||||||||||||||||||
| 292 | $doc->markXMNodeVisibility; | ||||||||||||||||||||||||||||||
| 293 | $self->preprocess($doc, @maths); | ||||||||||||||||||||||||||||||
| 294 | if ($$self{parallel}) { | ||||||||||||||||||||||||||||||
| 295 | my @secondaries = @{ $$self{secondary_processors} }; | ||||||||||||||||||||||||||||||
| 296 | # What's the right test for when cross-referencing should be done? | ||||||||||||||||||||||||||||||
| 297 | # For now: only when the primary and some secondary can cross-ref | ||||||||||||||||||||||||||||||
| 298 | # (otherwise, end up with peculiar structures?) | ||||||||||||||||||||||||||||||
| 299 | my ($proc1, @ignore) = grep { $_->can('addCrossref') } @secondaries; | ||||||||||||||||||||||||||||||
| 300 | if ($self->can('addCrossref') && $proc1) { | ||||||||||||||||||||||||||||||
| 301 | $$self{crossreferencing} = 1; # We'll need ID's! | ||||||||||||||||||||||||||||||
| 302 | $$proc1{crossreferencing} = 1; } | ||||||||||||||||||||||||||||||
| 303 | foreach my $proc (@secondaries) { | ||||||||||||||||||||||||||||||
| 304 | local $LaTeXML::Post::MATHPROCESSOR = $proc; | ||||||||||||||||||||||||||||||
| 305 | $proc->preprocess($doc, @maths); } } | ||||||||||||||||||||||||||||||
| 306 | # Re-Fetch the math nodes, in case preprocessing has messed them up!!! | ||||||||||||||||||||||||||||||
| 307 | @maths = $self->toProcess($doc); | ||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||
| 309 | ## Do in reverse, since (in LaTeXML) we allow math nested within text within math. | ||||||||||||||||||||||||||||||
| 310 | ## So, we want to converted any nested expressions first, so they get carried along | ||||||||||||||||||||||||||||||
| 311 | ## with the outer ones. | ||||||||||||||||||||||||||||||
| 312 | my $n = 0; | ||||||||||||||||||||||||||||||
| 313 | foreach my $math (reverse(@maths)) { | ||||||||||||||||||||||||||||||
| 314 | # If parent is MathBranch, which branch number is it? | ||||||||||||||||||||||||||||||
| 315 | # (note: the MathBranch will be in a ltx:MathFork, with a ltx:Math being 1st child) | ||||||||||||||||||||||||||||||
| 316 | my @preceding = $doc->findnodes("parent::ltx:MathBranch/preceding-sibling::*", $math); | ||||||||||||||||||||||||||||||
| 317 | local $LaTeXML::Post::MathProcessor::FORK = scalar(@preceding); | ||||||||||||||||||||||||||||||
| 318 | $self->processNode($doc, $math); | ||||||||||||||||||||||||||||||
| 319 | $n++; } | ||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||
| 321 | # Experimentally, cross reference ??? (or clearer name?) | ||||||||||||||||||||||||||||||
| 322 | if ($$self{parallel}) { | ||||||||||||||||||||||||||||||
| 323 | # There could be various strategies when there are more than 2 parallel conversions, | ||||||||||||||||||||||||||||||
| 324 | # eg a cycle or something.... | ||||||||||||||||||||||||||||||
| 325 | # Here, we simply take the first two processors that know how to addCrossref | ||||||||||||||||||||||||||||||
| 326 | # and connect their nodes to each other. | ||||||||||||||||||||||||||||||
| 327 | my ($proc1, $proc2, @ignore) | ||||||||||||||||||||||||||||||
| 328 | = grep { $_->can('addCrossref') } $self, @{ $$self{secondary_processors} }; | ||||||||||||||||||||||||||||||
| 329 | if ($proc1 && $proc2) { | ||||||||||||||||||||||||||||||
| 330 | # First, prepare a list of all Math id's, in document order, to simplify crossreferencing | ||||||||||||||||||||||||||||||
| 331 | my $ids = {}; | ||||||||||||||||||||||||||||||
| 332 | my $pos = 0; | ||||||||||||||||||||||||||||||
| 333 | foreach my $n ($doc->findnodes('descendant-or-self::ltx:Math/descendant::*[@xml:id]')) { | ||||||||||||||||||||||||||||||
| 334 | $$ids{ $n->getAttribute('xml:id') } = $pos++; } | ||||||||||||||||||||||||||||||
| 335 | $$proc1{crossreferencing_ids} = $ids; | ||||||||||||||||||||||||||||||
| 336 | $$proc2{crossreferencing_ids} = $ids; | ||||||||||||||||||||||||||||||
| 337 | # Now do cross referencing | ||||||||||||||||||||||||||||||
| 338 | $proc1->addCrossrefs($doc, $proc2); | ||||||||||||||||||||||||||||||
| 339 | $proc2->addCrossrefs($doc, $proc1); } } | ||||||||||||||||||||||||||||||
| 340 | NoteLog("converted $n Maths"); | ||||||||||||||||||||||||||||||
| 341 | return $doc; } | ||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||
| 343 | sub canConvert { return 1; } | ||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||
| 345 | # Make THIS MathProcessor the primary branch (of whatever parallel markup it supports), | ||||||||||||||||||||||||||||||
| 346 | # and make all of the @moreprocessors be secondary ones. | ||||||||||||||||||||||||||||||
| 347 | sub setParallel { | ||||||||||||||||||||||||||||||
| 348 | my ($self, @moreprocessors) = @_; | ||||||||||||||||||||||||||||||
| 349 | if (@moreprocessors) { | ||||||||||||||||||||||||||||||
| 350 | $$self{parallel} = 1; | ||||||||||||||||||||||||||||||
| 351 | map { $$_{is_secondary} = 1 } @moreprocessors; # Mark the others as secondary | ||||||||||||||||||||||||||||||
| 352 | $$self{secondary_processors} = [@moreprocessors]; | ||||||||||||||||||||||||||||||
| 353 | $$self{name} .= '[w/' . join('+', map { $_->getName } @moreprocessors) . ']'; } | ||||||||||||||||||||||||||||||
| 354 | else { | ||||||||||||||||||||||||||||||
| 355 | $$self{parallel} = 0; } | ||||||||||||||||||||||||||||||
| 356 | return; } | ||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||
| 358 | # Optional; if you want to do anything before translation | ||||||||||||||||||||||||||||||
| 359 | sub preprocess { | ||||||||||||||||||||||||||||||
| 360 | my ($self, $doc, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 361 | return; } | ||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||
| 363 | # $self->processNode($doc,$mathnode) is the top-level conversion | ||||||||||||||||||||||||||||||
| 364 | # It converts the XMath within $mathnode, and adds it to the $mathnode, | ||||||||||||||||||||||||||||||
| 365 | # This invokes $self->convertNode($doc,$xmath) to get the conversion. | ||||||||||||||||||||||||||||||
| 366 | sub processNode { | ||||||||||||||||||||||||||||||
| 367 | my ($self, $doc, $math) = @_; | ||||||||||||||||||||||||||||||
| 368 | my $xmath = $doc->findnode('ltx:XMath', $math); | ||||||||||||||||||||||||||||||
| 369 | return unless $xmath; # Nothing to convert if there's no XMath ... ! | ||||||||||||||||||||||||||||||
| 370 | local $LaTeXML::Post::MATHPROCESSOR = $self; | ||||||||||||||||||||||||||||||
| 371 | my $conversion; | ||||||||||||||||||||||||||||||
| 372 | # XMath will be removed (LATER!), but mark its ids as reusable. | ||||||||||||||||||||||||||||||
| 373 | $doc->preremoveNodes($xmath); | ||||||||||||||||||||||||||||||
| 374 | if ($$self{parallel}) { | ||||||||||||||||||||||||||||||
| 375 | my $primary = $self->convertNode($doc, $xmath); | ||||||||||||||||||||||||||||||
| 376 | my @secondaries = (); | ||||||||||||||||||||||||||||||
| 377 | foreach my $proc (@{ $$self{secondary_processors} }) { | ||||||||||||||||||||||||||||||
| 378 | # Exception: Do not generate Content MathML for kludge parses | ||||||||||||||||||||||||||||||
| 379 | next unless $proc->canConvert($doc, $math); | ||||||||||||||||||||||||||||||
| 380 | local $LaTeXML::Post::MATHPROCESSOR = $proc; | ||||||||||||||||||||||||||||||
| 381 | my $secondary = $proc->convertNode($doc, $xmath); | ||||||||||||||||||||||||||||||
| 382 | # IF it is (first) image, copy image attributes to ltx:Math ??? | ||||||||||||||||||||||||||||||
| 383 | $self->maybeSetMathImage($math, $secondary); | ||||||||||||||||||||||||||||||
| 384 | push(@secondaries, $secondary); } | ||||||||||||||||||||||||||||||
| 385 | $conversion = $self->combineParallel($doc, $xmath, $primary, @secondaries); } | ||||||||||||||||||||||||||||||
| 386 | else { | ||||||||||||||||||||||||||||||
| 387 | $conversion = $self->convertNode($doc, $xmath); | ||||||||||||||||||||||||||||||
| 388 | $self->maybeSetMathImage($math, $conversion); } | ||||||||||||||||||||||||||||||
| 389 | # we now REMOVE the ltx:XMath from the ltx:Math, and whitespace | ||||||||||||||||||||||||||||||
| 390 | # (if there's an XMath PostProcessing module, it will add it back, with appropriate id's) | ||||||||||||||||||||||||||||||
| 391 | if (my $xml = $$conversion{xml}) { | ||||||||||||||||||||||||||||||
| 392 | $$conversion{xml} = $self->outerWrapper($doc, $xmath, $xml); } | ||||||||||||||||||||||||||||||
| 393 | elsif (my $string = $$conversion{string}) { | ||||||||||||||||||||||||||||||
| 394 | my $mimetype = $$conversion{mimetype} || 'unknown'; | ||||||||||||||||||||||||||||||
| 395 | $$conversion{xml} = ['ltx:text', { class => 'ltx_math_' . $mimetype }, $string]; } | ||||||||||||||||||||||||||||||
| 396 | $doc->removeNodes($xmath); | ||||||||||||||||||||||||||||||
| 397 | # NOTE: Unless XMath is the primary, (preserving the XMath, w/no IDSuffix) | ||||||||||||||||||||||||||||||
| 398 | # we've got to remove the id's from the XMath, since the primary will get same id's | ||||||||||||||||||||||||||||||
| 399 | # and (some versions) of libxml2 complain! | ||||||||||||||||||||||||||||||
| 400 | if ($$conversion{mimetype} && ($$conversion{mimetype} ne 'application/x-latexml')) { | ||||||||||||||||||||||||||||||
| 401 | map { $_->removeAttribute('xml:id') } | ||||||||||||||||||||||||||||||
| 402 | $doc->findnodes('descendant-or-self::*[@xml:id]', $xmath); } | ||||||||||||||||||||||||||||||
| 403 | $doc->removeBlankNodes($math); | ||||||||||||||||||||||||||||||
| 404 | if (my $new = $$conversion{xml}) { | ||||||||||||||||||||||||||||||
| 405 | $doc->addNodes($math, $new); } | ||||||||||||||||||||||||||||||
| 406 | # else ? | ||||||||||||||||||||||||||||||
| 407 | return; } | ||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||
| 409 | sub maybeSetMathImage { | ||||||||||||||||||||||||||||||
| 410 | my ($self, $math, $conversion) = @_; | ||||||||||||||||||||||||||||||
| 411 | if ((($$conversion{mimetype} || '') =~ /^image\//) # Got an image? | ||||||||||||||||||||||||||||||
| 412 | && !$math->getAttribute('imagesrc')) { # and it's the first one | ||||||||||||||||||||||||||||||
| 413 | if (my $src = $$conversion{src}) { | ||||||||||||||||||||||||||||||
| 414 | $math->setAttribute(imagesrc => pathname_to_url($src)); | ||||||||||||||||||||||||||||||
| 415 | $math->setAttribute(imagewidth => $$conversion{width}); | ||||||||||||||||||||||||||||||
| 416 | $math->setAttribute(imageheight => $$conversion{height}); | ||||||||||||||||||||||||||||||
| 417 | $math->setAttribute(imagedepth => $$conversion{depth}); } } | ||||||||||||||||||||||||||||||
| 418 | return; } | ||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||
| 420 | # NOTE: Sort out how parallel & outerWrapper should work. | ||||||||||||||||||||||||||||||
| 421 | # It probably ought to be that if the conversion is being embedded in | ||||||||||||||||||||||||||||||
| 422 | # something from another namespace, it needs the wrapper. | ||||||||||||||||||||||||||||||
| 423 | # ie. when mixing parallel markups, NOT just at the top level, although certainly there too. | ||||||||||||||||||||||||||||||
| 424 | # | ||||||||||||||||||||||||||||||
| 425 | # This should wrap the resulting conversion with m:math or om:OMA or whatever appropriate? | ||||||||||||||||||||||||||||||
| 426 | sub outerWrapper { | ||||||||||||||||||||||||||||||
| 427 | my ($self, $doc, $xmath, $conversion) = @_; | ||||||||||||||||||||||||||||||
| 428 | return $conversion; } | ||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||
| 430 | # This should proably be from the core of the current ->processNode | ||||||||||||||||||||||||||||||
| 431 | sub convertNode { | ||||||||||||||||||||||||||||||
| 432 | my ($self, $doc, $node) = @_; | ||||||||||||||||||||||||||||||
| 433 | Fatal('misdefined', (ref $self), undef, | ||||||||||||||||||||||||||||||
| 434 | "Abstract package: math conversion has not been defined for this MathProcessor"); | ||||||||||||||||||||||||||||||
| 435 | return; } | ||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||
| 437 | # This should be implemented by potential Primaries | ||||||||||||||||||||||||||||||
| 438 | # Maybe the caller of this should check the namespaces, and call wrapper if needed? | ||||||||||||||||||||||||||||||
| 439 | sub combineParallel { | ||||||||||||||||||||||||||||||
| 440 | my ($self, $doc, $xmath, $primary, @secondaries) = @_; | ||||||||||||||||||||||||||||||
| 441 | Error('misdefined', (ref $self), undef, | ||||||||||||||||||||||||||||||
| 442 | "Abstract package: combining parallel markup has not been defined for this MathProcessor", | ||||||||||||||||||||||||||||||
| 443 | "dropping the extra markup from: " . join(',', map { $$_{processor} } @secondaries)); | ||||||||||||||||||||||||||||||
| 444 | return $primary; } | ||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||
| 446 | # A helper for converting XMText | ||||||||||||||||||||||||||||||
| 447 | # ltx:XMText escapes back to general ltx markup; the only element within XMath that does. | ||||||||||||||||||||||||||||||
| 448 | # BUT it can contain nested ltx:Math! | ||||||||||||||||||||||||||||||
| 449 | # When converting to (potentially parallel markup, coarse-grained), | ||||||||||||||||||||||||||||||
| 450 | # the non-math needs to be duplicated, but with the ID's modified, | ||||||||||||||||||||||||||||||
| 451 | # AND the nested math needs to be converted to ONLY the current target's markup | ||||||||||||||||||||||||||||||
| 452 | # NOT parallel within each nested math, although it should still be cross-referencable to others! | ||||||||||||||||||||||||||||||
| 453 | # moreover, the math will need the outerWrapper. | ||||||||||||||||||||||||||||||
| 454 | my $NBSP = pack('U', 0xA0); # CONSTANT | ||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||
| 456 | sub convertXMTextContent { | ||||||||||||||||||||||||||||||
| 457 | my ($self, $doc, $convertspaces, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 458 | my @result = (); | ||||||||||||||||||||||||||||||
| 459 | foreach my $node (@nodes) { | ||||||||||||||||||||||||||||||
| 460 | if ($node->nodeType == XML_TEXT_NODE) { | ||||||||||||||||||||||||||||||
| 461 | my $string = $node->textContent; | ||||||||||||||||||||||||||||||
| 462 | if ($convertspaces) { | ||||||||||||||||||||||||||||||
| 463 | $string =~ s/^\s+/$NBSP/; $string =~ s/\s+$/$NBSP/; } | ||||||||||||||||||||||||||||||
| 464 | push(@result, $string); } | ||||||||||||||||||||||||||||||
| 465 | else { | ||||||||||||||||||||||||||||||
| 466 | my $tag = $doc->getQName($node); | ||||||||||||||||||||||||||||||
| 467 | if ($tag eq 'ltx:XMath') { | ||||||||||||||||||||||||||||||
| 468 | my $conversion = $self->convertNode($doc, $node); | ||||||||||||||||||||||||||||||
| 469 | my $xml = $$conversion{xml}; | ||||||||||||||||||||||||||||||
| 470 | # And if no xml ???? | ||||||||||||||||||||||||||||||
| 471 | push(@result, $self->outerWrapper($doc, $node, $xml)); } | ||||||||||||||||||||||||||||||
| 472 | else { | ||||||||||||||||||||||||||||||
| 473 | my %attr = (); | ||||||||||||||||||||||||||||||
| 474 | foreach my $attr ($node->attributes) { | ||||||||||||||||||||||||||||||
| 475 | my $atype = $attr->nodeType; | ||||||||||||||||||||||||||||||
| 476 | if ($atype == XML_ATTRIBUTE_NODE) { | ||||||||||||||||||||||||||||||
| 477 | my $key = $attr->nodeName; | ||||||||||||||||||||||||||||||
| 478 | my $value = $attr->getValue; | ||||||||||||||||||||||||||||||
| 479 | if ($key =~ /^_/) { } # don't copy internal attributes ??? | ||||||||||||||||||||||||||||||
| 480 | elsif ($key eq 'xml:id') { } # ignore; we'll handle fragid??? | ||||||||||||||||||||||||||||||
| 481 | elsif ($key eq 'fragid') { | ||||||||||||||||||||||||||||||
| 482 | my $id = $doc->uniquifyID($value, $self->IDSuffix); | ||||||||||||||||||||||||||||||
| 483 | $attr{'xml:id'} = $id; } | ||||||||||||||||||||||||||||||
| 484 | else { | ||||||||||||||||||||||||||||||
| 485 | $attr{$key} = $attr->value; } } } | ||||||||||||||||||||||||||||||
| 486 | # Probably should invoke associateNode ??? | ||||||||||||||||||||||||||||||
| 487 | push(@result, | ||||||||||||||||||||||||||||||
| 488 | [$tag, {%attr}, $self->convertXMTextContent($doc, $convertspaces, $node->childNodes)]); } } } | ||||||||||||||||||||||||||||||
| 489 | return @result; } | ||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||
| 491 | # When converting an XMath node (with an id) to some other format, | ||||||||||||||||||||||||||||||
| 492 | # we will generate an id for the new node. | ||||||||||||||||||||||||||||||
| 493 | # This method returns a suffix to be added to the XMath id. | ||||||||||||||||||||||||||||||
| 494 | # The primary format gets the id's unchanged, but secondary ones get a suffix (eg. ".pmml") | ||||||||||||||||||||||||||||||
| 495 | sub IDSuffix { | ||||||||||||||||||||||||||||||
| 496 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 497 | return ($$self{is_secondary} ? $self->rawIDSuffix : ''); } | ||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||
| 499 | sub rawIDSuffix { | ||||||||||||||||||||||||||||||
| 500 | return ''; } | ||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||
| 502 | # In order to do cross-referencing betweeen formats, and to relate semantic/presentation | ||||||||||||||||||||||||||||||
| 503 | # information to content/presentation nodes (resp), we want to associate each | ||||||||||||||||||||||||||||||
| 504 | # generated node (MathML, OpenMath,...) with a "source" XMath node "responsible" for it's generation. | ||||||||||||||||||||||||||||||
| 505 | # This is often the "current" XMath node that was being converted, but sometimes | ||||||||||||||||||||||||||||||
| 506 | # * the containing XMDual (which makes more sense when we've generated a "container") | ||||||||||||||||||||||||||||||
| 507 | # * the containing XMDual's semantic operator (makes more sense when we're generating | ||||||||||||||||||||||||||||||
| 508 | # tokens that are only visible from the presentation branch) | ||||||||||||||||||||||||||||||
| 509 | # | ||||||||||||||||||||||||||||||
| 510 | # Additionally: piggy-back here copying any foreign-namespaced attributes from source to target | ||||||||||||||||||||||||||||||
| 511 | sub associateNode { | ||||||||||||||||||||||||||||||
| 512 | my ($self, $node, $currentnode, $noxref) = @_; | ||||||||||||||||||||||||||||||
| 513 | my $r = ref $node; | ||||||||||||||||||||||||||||||
| 514 | return unless $currentnode && $r && ($r eq 'ARRAY' || $r eq 'XML::LibXML::Element'); | ||||||||||||||||||||||||||||||
| 515 | $self->copy_foreign_attributes($node, $currentnode); | ||||||||||||||||||||||||||||||
| 516 | my $document = $LaTeXML::Post::DOCUMENT; | ||||||||||||||||||||||||||||||
| 517 | # Check if already associated with a source node | ||||||||||||||||||||||||||||||
| 518 | my $isarray = ref $node eq 'ARRAY'; | ||||||||||||||||||||||||||||||
| 519 | # What kind of branch are we generating? | ||||||||||||||||||||||||||||||
| 520 | my $ispresentation = $self->rawIDSuffix eq '.pmml'; # TEMPORARY HACK: BAAAAD method! | ||||||||||||||||||||||||||||||
| 521 | my $iscontainer = 0; | ||||||||||||||||||||||||||||||
| 522 | my $container; | ||||||||||||||||||||||||||||||
| 523 | if ($isarray) { | ||||||||||||||||||||||||||||||
| 524 | return if $$node[1]{'_sourced'}; | ||||||||||||||||||||||||||||||
| 525 | $$node[1]{'_sourced'} = 1; | ||||||||||||||||||||||||||||||
| 526 | my ($tag, $attr, @children) = @$node; | ||||||||||||||||||||||||||||||
| 527 | $iscontainer = grep { ref $_ } @children; } | ||||||||||||||||||||||||||||||
| 528 | else { | ||||||||||||||||||||||||||||||
| 529 | return if $node->getAttribute('_sourced'); | ||||||||||||||||||||||||||||||
| 530 | $node->setAttribute('_sourced' => 1); | ||||||||||||||||||||||||||||||
| 531 | $iscontainer = scalar(element_nodes($node)); } | ||||||||||||||||||||||||||||||
| 532 | my $sourcenode = $currentnode; | ||||||||||||||||||||||||||||||
| 533 | # If the current node is declared, use it (but meaning is overridden) | ||||||||||||||||||||||||||||||
| 534 | if ($currentnode->getAttribute('decl_id')) { } | ||||||||||||||||||||||||||||||
| 535 | # If the generated node is a "container" (non-token!), use the containing XMDual as source | ||||||||||||||||||||||||||||||
| 536 | elsif ($iscontainer) { | ||||||||||||||||||||||||||||||
| 537 | my $sid = $sourcenode->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 538 | # But ONLY if that XMDual is the "direct" parent, or is parent of XRef that points to $current | ||||||||||||||||||||||||||||||
| 539 | if ($container = $document->findnode('parent::ltx:XMDual[1]', $sourcenode) | ||||||||||||||||||||||||||||||
| 540 | || ($sid && | ||||||||||||||||||||||||||||||
| 541 | $document->findnode("ancestor-or-self::ltx:XMDual[ltx:XMRef[\@idref='$sid']][1]", | ||||||||||||||||||||||||||||||
| 542 | $sourcenode))) { | ||||||||||||||||||||||||||||||
| 543 | $sourcenode = $container; } } | ||||||||||||||||||||||||||||||
| 544 | # Parent App w/decl_id or meaning is source, unless current node has decl_id (but ignore meaning) | ||||||||||||||||||||||||||||||
| 545 | elsif ($container = $document->findnode('ancestor::ltx:XMApp[@decl_id or @meaning][1]', $sourcenode)) { | ||||||||||||||||||||||||||||||
| 546 | $sourcenode = $container; } | ||||||||||||||||||||||||||||||
| 547 | # If the current node is appropriately visible, use it. | ||||||||||||||||||||||||||||||
| 548 | elsif ($currentnode->getAttribute(($ispresentation ? '_cvis' : '_pvis'))) { } | ||||||||||||||||||||||||||||||
| 549 | # Else (current node isn't visible); try to find content OPERATOR | ||||||||||||||||||||||||||||||
| 550 | elsif ($container = $document->findnode('ancestor-or-self::ltx:XMDual[1]', $sourcenode)) { | ||||||||||||||||||||||||||||||
| 551 | my ($op) = element_nodes($container); | ||||||||||||||||||||||||||||||
| 552 | my $q = $document->getQName($op) || 'unknown'; | ||||||||||||||||||||||||||||||
| 553 | if ($container->hasAttribute('decl_id')) { | ||||||||||||||||||||||||||||||
| 554 | $op = undef; } | ||||||||||||||||||||||||||||||
| 555 | elsif ($q eq 'ltx:XMTok') { } | ||||||||||||||||||||||||||||||
| 556 | elsif ($q eq 'ltx:XMApp') { | ||||||||||||||||||||||||||||||
| 557 | while (($q eq 'ltx:XMApp') && !$op->hasAttribute('meaning')) { | ||||||||||||||||||||||||||||||
| 558 | ($op) = element_nodes($op); | ||||||||||||||||||||||||||||||
| 559 | $q = $document->getQName($op) || 'unknown'; } } # get "real" operator | ||||||||||||||||||||||||||||||
| 560 | if ($q eq 'ltx:XMRef') { | ||||||||||||||||||||||||||||||
| 561 | $op = $document->realizeXMNode($op); } | ||||||||||||||||||||||||||||||
| 562 | # Be a bit fuzzy about whether something is "visible" | ||||||||||||||||||||||||||||||
| 563 | if ($op && !($op->getAttribute('_pvis') | ||||||||||||||||||||||||||||||
| 564 | && (($op->getAttribute('thickness') || '<anything>') ne '0pt'))) { | ||||||||||||||||||||||||||||||
| 565 | $sourcenode = $op; } | ||||||||||||||||||||||||||||||
| 566 | else { | ||||||||||||||||||||||||||||||
| 567 | $sourcenode = $container; } } | ||||||||||||||||||||||||||||||
| 568 | # If we're intending to cross-reference, then source & generated nodes will need ID's | ||||||||||||||||||||||||||||||
| 569 | if ($$self{crossreferencing}) { | ||||||||||||||||||||||||||||||
| 570 | if (!$noxref && !$sourcenode->getAttribute('fragid')) { # If no ID, but need one | ||||||||||||||||||||||||||||||
| 571 | $document->generateNodeID($sourcenode, '', 1); } # but the ID is reusable | ||||||||||||||||||||||||||||||
| 572 | if (my $sourceid = $sourcenode->getAttribute('fragid')) { # If source has ID | ||||||||||||||||||||||||||||||
| 573 | my $nodeid = $currentnode->getAttribute('fragid') || $sourceid; | ||||||||||||||||||||||||||||||
| 574 | my $id = $document->uniquifyID($nodeid, $self->IDSuffix); | ||||||||||||||||||||||||||||||
| 575 | if ($isarray) { | ||||||||||||||||||||||||||||||
| 576 | $$node[1]{'xml:id'} = $id; } | ||||||||||||||||||||||||||||||
| 577 | else { | ||||||||||||||||||||||||||||||
| 578 | $node->setAttribute('xml:id' => $id); } | ||||||||||||||||||||||||||||||
| 579 | push(@{ $$self{convertedIDs}{$sourceid} }, $id) unless $noxref; } } | ||||||||||||||||||||||||||||||
| 580 | $self->associateNodeHook($node, $sourcenode, $noxref); | ||||||||||||||||||||||||||||||
| 581 | if ($isarray) { # Array represented | ||||||||||||||||||||||||||||||
| 582 | map { $self->associateNode($_, $currentnode, $noxref) } @$node[2 .. $#$node]; } | ||||||||||||||||||||||||||||||
| 583 | else { # LibXML node | ||||||||||||||||||||||||||||||
| 584 | map { $self->associateNode($_, $currentnode, $noxref) } element_nodes($node); } | ||||||||||||||||||||||||||||||
| 585 | return; } | ||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||
| 587 | # Customization hook for adding other attributes to the generated math nodes. | ||||||||||||||||||||||||||||||
| 588 | sub associateNodeHook { | ||||||||||||||||||||||||||||||
| 589 | my ($self, $node, $sourcenode, $noxref) = @_; | ||||||||||||||||||||||||||||||
| 590 | return; } | ||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||
| 592 | sub shownode { | ||||||||||||||||||||||||||||||
| 593 | my ($node, $level) = @_; | ||||||||||||||||||||||||||||||
| 594 | $level = 0 unless defined $level; | ||||||||||||||||||||||||||||||
| 595 | my $ref = ref $node; | ||||||||||||||||||||||||||||||
| 596 | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 597 | my ($tag, $attr, @children) = @$node; | ||||||||||||||||||||||||||||||
| 598 | return "\n" . (' ' x $level) | ||||||||||||||||||||||||||||||
| 599 | . '[' . $tag . ',{' . join(',', map { $_ . '=>' . ($$attr{$_} || '') } sort keys %$attr) . '},' | ||||||||||||||||||||||||||||||
| 600 | . join(',', map { shownode($_, $level + 1) } @children) . ']'; } | ||||||||||||||||||||||||||||||
| 601 | elsif ($ref =~ /^XML/) { | ||||||||||||||||||||||||||||||
| 602 | return $node->toString; } | ||||||||||||||||||||||||||||||
| 603 | else { | ||||||||||||||||||||||||||||||
| 604 | return "$node"; } } | ||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||
| 606 | # Add backref linkages (eg. xref) onto the nodes that $self created (converted from XMath) | ||||||||||||||||||||||||||||||
| 607 | # to reference those that $otherprocessor created. | ||||||||||||||||||||||||||||||
| 608 | # NOTE: Subclass MUST define addCrossref($node,$xref_id) to add the | ||||||||||||||||||||||||||||||
| 609 | # id of the "Other Interesting Node" to the (array represented) xml $node | ||||||||||||||||||||||||||||||
| 610 | # in whatever fashion the markup for that processor uses. | ||||||||||||||||||||||||||||||
| 611 | # | ||||||||||||||||||||||||||||||
| 612 | # This may be another useful place to add a hook? | ||||||||||||||||||||||||||||||
| 613 | # It would provide the list of cross-refenced nodes in document order | ||||||||||||||||||||||||||||||
| 614 | # This would allow deciding whether or not to copy foreign attributes or other interesting things | ||||||||||||||||||||||||||||||
| 615 | sub addCrossrefs { | ||||||||||||||||||||||||||||||
| 616 | my ($self, $doc, $otherprocessor) = @_; | ||||||||||||||||||||||||||||||
| 617 | my $selfs_map = $$self{convertedIDs}; | ||||||||||||||||||||||||||||||
| 618 | my $others_map = $$otherprocessor{convertedIDs}; | ||||||||||||||||||||||||||||||
| 619 | my $xrefids = $$self{crossreferencing_ids}; | ||||||||||||||||||||||||||||||
| 620 | my $backref = {}; | ||||||||||||||||||||||||||||||
| 621 | foreach my $id (keys %$selfs_map) { | ||||||||||||||||||||||||||||||
| 622 | foreach my $t (@{ $$selfs_map{$id} }) { | ||||||||||||||||||||||||||||||
| 623 | $$backref{$t} = $id; } } | ||||||||||||||||||||||||||||||
| 624 | foreach my $xid (keys %$selfs_map) { # For each XMath id that $self converted | ||||||||||||||||||||||||||||||
| 625 | my $other_ids = $$others_map{$xid}; # the ids where $xid ended up in $other processor | ||||||||||||||||||||||||||||||
| 626 | if (!$other_ids) { | ||||||||||||||||||||||||||||||
| 627 | # But If this node didn't directly end up in $other, try to find alternative | ||||||||||||||||||||||||||||||
| 628 | # Typically happens when a "visible" node doesn't have visible representation in other format! | ||||||||||||||||||||||||||||||
| 629 | # So, see if an ancestor got mapped. | ||||||||||||||||||||||||||||||
| 630 | if (my $mapped = $$selfs_map{$xid}) { | ||||||||||||||||||||||||||||||
| 631 | foreach my $mid (@$mapped) { | ||||||||||||||||||||||||||||||
| 632 | if (my $node = $doc->findNodeByID($mid)) { | ||||||||||||||||||||||||||||||
| 633 | my ($parent, $pid, $xpid) = ($node, undef, undef); | ||||||||||||||||||||||||||||||
| 634 | while (($parent = $parent->parentNode) | ||||||||||||||||||||||||||||||
| 635 | && ($parent->nodeType == XML_ELEMENT_NODE) # in case we hit Document | ||||||||||||||||||||||||||||||
| 636 | && (!($pid = $parent->getAttribute('xml:id')) | ||||||||||||||||||||||||||||||
| 637 | || !($xpid = $$backref{$pid}) | ||||||||||||||||||||||||||||||
| 638 | || !$$others_map{$xpid})) { } | ||||||||||||||||||||||||||||||
| 639 | if ($xpid) { | ||||||||||||||||||||||||||||||
| 640 | $other_ids = $$others_map{$xpid}; } } } } } | ||||||||||||||||||||||||||||||
| 641 | if ($other_ids) { # Hopefully, we've got the targets, now | ||||||||||||||||||||||||||||||
| 642 | my $xref_id = $$other_ids[0]; | ||||||||||||||||||||||||||||||
| 643 | if (scalar(@$other_ids) > 1) { # Find 1st in document order! (order is cached) | ||||||||||||||||||||||||||||||
| 644 | ($xref_id) = sort { $$xrefids{$a} <=> $$xrefids{$b} } @$other_ids; } | ||||||||||||||||||||||||||||||
| 645 | foreach my $id (@{ $$selfs_map{$xid} }) { # look at each node $self created from $xid | ||||||||||||||||||||||||||||||
| 646 | if (my $node = $doc->findNodeByID($id)) { # If we find a node, | ||||||||||||||||||||||||||||||
| 647 | $self->addCrossref($node, $xref_id); } } } # } # add a crossref from it to $others's nod | ||||||||||||||||||||||||||||||
| 648 | else { | ||||||||||||||||||||||||||||||
| 649 | } } | ||||||||||||||||||||||||||||||
| 650 | return; } | ||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||
| 652 | sub mathIsParsed { | ||||||||||||||||||||||||||||||
| 653 | my ($doc, $math) = @_; | ||||||||||||||||||||||||||||||
| 654 | return $math && (($math->getAttribute('class') || '') !~ 'ltx_math_unparsed'); } | ||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||
| 656 | #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||
| 658 | package LaTeXML::Post::Document; | ||||||||||||||||||||||||||||||
| 659 | use strict; | ||||||||||||||||||||||||||||||
| 660 | use LaTeXML::Common::XML; | ||||||||||||||||||||||||||||||
| 661 | use LaTeXML::Util::Pathname; | ||||||||||||||||||||||||||||||
| 662 | use LaTeXML::Util::Radix; | ||||||||||||||||||||||||||||||
| 663 | use DB_File; | ||||||||||||||||||||||||||||||
| 664 | use Unicode::Normalize; | ||||||||||||||||||||||||||||||
| 665 | use LaTeXML::Post; # to import error handling... | ||||||||||||||||||||||||||||||
| 666 | use LaTeXML::Common::Error; | ||||||||||||||||||||||||||||||
| 667 | use URI::file; | ||||||||||||||||||||||||||||||
| 668 | use base qw(LaTeXML::Common::Object); | ||||||||||||||||||||||||||||||
| 669 | our $NSURI = "http://dlmf.nist.gov/LaTeXML"; | ||||||||||||||||||||||||||||||
| 670 | our $XPATH = LaTeXML::Common::XML::XPath->new(ltx => $NSURI); | ||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||
| 672 | # Useful options: | ||||||||||||||||||||||||||||||
| 673 | # destination = the ultimate destination file for this document to be written. | ||||||||||||||||||||||||||||||
| 674 | # destinationDirectory = the directory it will be stored in (derived from $destination) | ||||||||||||||||||||||||||||||
| 675 | # siteDirectory = the root directory of where the entire site will be contained | ||||||||||||||||||||||||||||||
| 676 | # namespaces = a hash of namespace prefix => namespace uri | ||||||||||||||||||||||||||||||
| 677 | # namespaceURIs = reverse hash of above. | ||||||||||||||||||||||||||||||
| 678 | # nocache = a boolean, disables storing of permanent LaTeXML.cache | ||||||||||||||||||||||||||||||
| 679 | # the cache is used to remember things like image conversions from previous runs. | ||||||||||||||||||||||||||||||
| 680 | # searchpaths = array of paths to search for other resources | ||||||||||||||||||||||||||||||
| 681 | # Note that these may not be LaTeXML documents (maybe html or ....) | ||||||||||||||||||||||||||||||
| 682 | sub new { | ||||||||||||||||||||||||||||||
| 683 | my ($class, $xmldoc, %options) = @_; | ||||||||||||||||||||||||||||||
| 684 | my $self = $class->new_internal($xmldoc, %options); | ||||||||||||||||||||||||||||||
| 685 | $self->setDocument_internal($xmldoc); | ||||||||||||||||||||||||||||||
| 686 | return $self; } | ||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||
| 688 | sub new_internal { | ||||||||||||||||||||||||||||||
| 689 | my ($class, $xmldoc, %options) = @_; | ||||||||||||||||||||||||||||||
| 690 | my %data = (); | ||||||||||||||||||||||||||||||
| 691 | if (ref $class) { # Cloning! | ||||||||||||||||||||||||||||||
| 692 | map { $data{$_} = $$class{$_} } keys %$class; | ||||||||||||||||||||||||||||||
| 693 | $class = ref $class; } | ||||||||||||||||||||||||||||||
| 694 | map { $data{$_} = $options{$_} } keys %options; # These override. | ||||||||||||||||||||||||||||||
| 695 | if ((defined $options{destination}) && (!defined $options{destinationDirectory})) { | ||||||||||||||||||||||||||||||
| 696 | my ($dir, $name, $ext) = pathname_split($data{destination}); | ||||||||||||||||||||||||||||||
| 697 | $data{destinationDirectory} = $dir || '.'; } | ||||||||||||||||||||||||||||||
| 698 | # Check consistency of siteDirectory (providing there's a destinationDirectory) | ||||||||||||||||||||||||||||||
| 699 | if ($data{destinationDirectory}) { | ||||||||||||||||||||||||||||||
| 700 | if ($data{siteDirectory}) { | ||||||||||||||||||||||||||||||
| 701 | Fatal('unexpected', $data{destinationDirectory}, undef, | ||||||||||||||||||||||||||||||
| 702 | "The destination directory ($data{destinationDirectory})" | ||||||||||||||||||||||||||||||
| 703 | . " must be within the siteDirectory ($data{siteDirectory})") | ||||||||||||||||||||||||||||||
| 704 | unless pathname_is_contained($data{destinationDirectory}, $data{siteDirectory}); } | ||||||||||||||||||||||||||||||
| 705 | else { | ||||||||||||||||||||||||||||||
| 706 | $data{siteDirectory} = $data{destinationDirectory}; } } | ||||||||||||||||||||||||||||||
| 707 | # Start, at least, with our own namespaces. | ||||||||||||||||||||||||||||||
| 708 | $data{namespaces} = { ltx => $NSURI } unless $data{namespaces}; | ||||||||||||||||||||||||||||||
| 709 | $data{namespaceURIs} = { $NSURI => 'ltx' } unless $data{namespaceURIs}; | ||||||||||||||||||||||||||||||
| 710 | $data{idcache} = {}; | ||||||||||||||||||||||||||||||
| 711 | $data{idcache_reusable} = {}; | ||||||||||||||||||||||||||||||
| 712 | $data{idcache_reserve} = {}; | ||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||
| 714 | my $self = bless {%data}, $class; | ||||||||||||||||||||||||||||||
| 715 | return $self; } | ||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||
| 717 | sub newFromFile { | ||||||||||||||||||||||||||||||
| 718 | my ($class, $source, %options) = @_; | ||||||||||||||||||||||||||||||
| 719 | my $path = (ref $class | ||||||||||||||||||||||||||||||
| 720 | ? pathname_find($source, paths => $$class{searchpaths}) | ||||||||||||||||||||||||||||||
| 721 | : $source); | ||||||||||||||||||||||||||||||
| 722 | if (!$path) { | ||||||||||||||||||||||||||||||
| 723 | Error('missing_file', $source, $class, "No XML document '$source' found", | ||||||||||||||||||||||||||||||
| 724 | (ref $class ? "search paths are " . join(', ', @{ $$class{searchpaths} }) : ())); | ||||||||||||||||||||||||||||||
| 725 | return; } | ||||||||||||||||||||||||||||||
| 726 | $options{source} = $path; | ||||||||||||||||||||||||||||||
| 727 | if (!$options{sourceDirectory}) { | ||||||||||||||||||||||||||||||
| 728 | my ($dir, $name, $ext) = pathname_split($path); | ||||||||||||||||||||||||||||||
| 729 | $options{sourceDirectory} = $dir || '.'; } | ||||||||||||||||||||||||||||||
| 730 | my $doc = $class->new(LaTeXML::Common::XML::Parser->new()->parseFile($path), %options); | ||||||||||||||||||||||||||||||
| 731 | $doc->validate if $$doc{validate}; | ||||||||||||||||||||||||||||||
| 732 | return $doc; } | ||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||
| 734 | sub newFromString { | ||||||||||||||||||||||||||||||
| 735 | my ($class, $string, %options) = @_; | ||||||||||||||||||||||||||||||
| 736 | $options{sourceDirectory} = '.' unless $options{sourceDirectory}; | ||||||||||||||||||||||||||||||
| 737 | my $doc = $class->new(LaTeXML::Common::XML::Parser->new()->parseString($string), %options); | ||||||||||||||||||||||||||||||
| 738 | $doc->validate if $$doc{validate}; | ||||||||||||||||||||||||||||||
| 739 | return $doc; } | ||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||
| 741 | sub newFromSTDIN { | ||||||||||||||||||||||||||||||
| 742 | my ($class, %options) = @_; | ||||||||||||||||||||||||||||||
| 743 | my $string; | ||||||||||||||||||||||||||||||
| 744 | { local $/ = undef; $string = <>; } | ||||||||||||||||||||||||||||||
| 745 | $options{sourceDirectory} = '.' unless $options{sourceDirectory}; | ||||||||||||||||||||||||||||||
| 746 | my $doc = $class->new(LaTeXML::Common::XML::Parser->new()->parseString($string), %options); | ||||||||||||||||||||||||||||||
| 747 | $doc->validate if $$doc{validate}; | ||||||||||||||||||||||||||||||
| 748 | return $doc; } | ||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||
| 750 | #====================================================================== | ||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||
| 752 | # This is for creating essentially "sub documents" | ||||||||||||||||||||||||||||||
| 753 | # that are in some sense children of $self, possibly removed or cloned from it. | ||||||||||||||||||||||||||||||
| 754 | # And they are presumably LaTeXML documents | ||||||||||||||||||||||||||||||
| 755 | sub newDocument { | ||||||||||||||||||||||||||||||
| 756 | my ($self, $root, %options) = @_; | ||||||||||||||||||||||||||||||
| 757 | my $clone_suffix = $options{clone_suffix}; | ||||||||||||||||||||||||||||||
| 758 | delete $options{clone_suffix}; | ||||||||||||||||||||||||||||||
| 759 | my $doc = $self->new_internal(undef, %options); | ||||||||||||||||||||||||||||||
| 760 | $doc->setDocument_internal($root, clone_suffix => $clone_suffix); | ||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||
| 762 | if (my $root_id = $self->getDocumentElement->getAttribute('xml:id')) { | ||||||||||||||||||||||||||||||
| 763 | $$doc{split_from_id} = $root_id; } | ||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||
| 765 | # Copy any processing instructions. | ||||||||||||||||||||||||||||||
| 766 | foreach my $pi ($self->findnodes(".//processing-instruction('latexml')")) { | ||||||||||||||||||||||||||||||
| 767 | $doc->getDocument->appendChild($pi->cloneNode); } | ||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||
| 769 | # And any resource elements | ||||||||||||||||||||||||||||||
| 770 | if (my @resources = $self->findnodes("descendant::ltx:resource")) { | ||||||||||||||||||||||||||||||
| 771 | $doc->addNodes($doc->getDocumentElement, @resources); } # cloning, as needed... | ||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||
| 773 | # If new document has no date, try to add one | ||||||||||||||||||||||||||||||
| 774 | $doc->addDate($self); | ||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||
| 776 | # And copy class from the top-level document; This is risky... | ||||||||||||||||||||||||||||||
| 777 | # We want to preserve global document style information | ||||||||||||||||||||||||||||||
| 778 | # But some may refer specifically to the document, and NOT to the parts? | ||||||||||||||||||||||||||||||
| 779 | if (my $class = $self->getDocumentElement->getAttribute('class')) { | ||||||||||||||||||||||||||||||
| 780 | my $root = $doc->getDocumentElement; | ||||||||||||||||||||||||||||||
| 781 | my $oclass = $root->getAttribute('class'); | ||||||||||||||||||||||||||||||
| 782 | $root->setAttribute(class => ($oclass ? $oclass . ' ' . $class : $class)); } | ||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||
| 784 | # Finally, return the new document. | ||||||||||||||||||||||||||||||
| 785 | return $doc; } | ||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||
| 787 | sub setDocument_internal { | ||||||||||||||||||||||||||||||
| 788 | my ($self, $root, %options) = @_; | ||||||||||||||||||||||||||||||
| 789 | # Build the document's XML | ||||||||||||||||||||||||||||||
| 790 | my $roottype = ref $root; | ||||||||||||||||||||||||||||||
| 791 | if ($roottype eq 'LaTeXML::Core::Document') { | ||||||||||||||||||||||||||||||
| 792 | $root = $root->getDocument; | ||||||||||||||||||||||||||||||
| 793 | $roottype = ref $root; } | ||||||||||||||||||||||||||||||
| 794 | if (my $clone_suffix = $options{clone_suffix}) { | ||||||||||||||||||||||||||||||
| 795 | if ($roottype eq 'XML::LibXML::Document') { | ||||||||||||||||||||||||||||||
| 796 | Fatal('internal', 'unimplemented', undef, | ||||||||||||||||||||||||||||||
| 797 | "Have not yet implemented cloning for entire documents"); } | ||||||||||||||||||||||||||||||
| 798 | # Just make a clone, and then insert that. | ||||||||||||||||||||||||||||||
| 799 | $root = $self->cloneNode($root, $clone_suffix); } | ||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||
| 801 | if ($roottype eq 'XML::LibXML::Document') { | ||||||||||||||||||||||||||||||
| 802 | $$self{document} = $root; | ||||||||||||||||||||||||||||||
| 803 | foreach my $node ($self->findnodes("//*[\@xml:id]")) { # Now record all ID's | ||||||||||||||||||||||||||||||
| 804 | $$self{idcache}{ $node->getAttribute('xml:id') } = $node; } | ||||||||||||||||||||||||||||||
| 805 | # Fetch any additional namespaces from the root | ||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||
| 807 | if (my $docroot = $root->documentElement) { | ||||||||||||||||||||||||||||||
| 808 | foreach my $ns ($docroot->getNamespaces) { | ||||||||||||||||||||||||||||||
| 809 | my ($prefix, $uri) = ($ns->getLocalName, $ns->getData); | ||||||||||||||||||||||||||||||
| 810 | if ($prefix) { | ||||||||||||||||||||||||||||||
| 811 | $$self{namespaces}{$prefix} = $uri unless $$self{namespaces}{$prefix}; | ||||||||||||||||||||||||||||||
| 812 | $$self{namespaceURIs}{$uri} = $prefix unless $$self{namespaceURIs}{$uri}; } } } | ||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||
| 814 | # Extract data from latexml's ProcessingInstructions | ||||||||||||||||||||||||||||||
| 815 | # I'd like to provide structured access to the PI's for those modules that need them, | ||||||||||||||||||||||||||||||
| 816 | # but it isn't quite clear what that api should be. | ||||||||||||||||||||||||||||||
| 817 | $$self{processingInstructions} = | ||||||||||||||||||||||||||||||
| 818 | [map { $_->textContent } $XPATH->findnodes('.//processing-instruction("latexml")', $root)]; | ||||||||||||||||||||||||||||||
| 819 | # Combine specified paths with any from the PI's | ||||||||||||||||||||||||||||||
| 820 | my @paths = (); | ||||||||||||||||||||||||||||||
| 821 | @paths = @{ $$self{searchpaths} } if $$self{searchpaths}; | ||||||||||||||||||||||||||||||
| 822 | foreach my $pi (@{ $$self{processingInstructions} }) { | ||||||||||||||||||||||||||||||
| 823 | if ($pi =~ /^\s*searchpaths\s*=\s*([\"\'])(.*?)\1\s*$/) { | ||||||||||||||||||||||||||||||
| 824 | push(@paths, split(',', $2)); } } | ||||||||||||||||||||||||||||||
| 825 | ### No, this ultimately can be the xml source, which may be the destination; | ||||||||||||||||||||||||||||||
| 826 | ### adding this gets the wrong graphics (already processed!) | ||||||||||||||||||||||||||||||
| 827 | ### push(@paths, pathname_absolute($$self{sourceDirectory})) if $$self{sourceDirectory}; | ||||||||||||||||||||||||||||||
| 828 | # But do add cwd after the document specified paths. | ||||||||||||||||||||||||||||||
| 829 | $$self{searchpaths} = [@paths, '.']; } | ||||||||||||||||||||||||||||||
| 830 | elsif ($roottype eq 'XML::LibXML::Element') { | ||||||||||||||||||||||||||||||
| 831 | $$self{document} = XML::LibXML::Document->new("1.0", "UTF-8"); | ||||||||||||||||||||||||||||||
| 832 | # Assume we've got any namespaces already ? | ||||||||||||||||||||||||||||||
| 833 | if (my $parent = $self->findnode('ancestor::*[@id][1]', $root)) { | ||||||||||||||||||||||||||||||
| 834 | $$self{parent_id} = $parent->getAttribute('xml:id'); } | ||||||||||||||||||||||||||||||
| 835 | # if no cloning requested, we can just plug the node directly in. | ||||||||||||||||||||||||||||||
| 836 | # (otherwise, we should use addNodes?) | ||||||||||||||||||||||||||||||
| 837 | # Seems that only importNode (NOT adopt) works correctly, | ||||||||||||||||||||||||||||||
| 838 | # PROVIDED we also set the namespace. | ||||||||||||||||||||||||||||||
| 839 | $$self{document}->setDocumentElement($$self{document}->importNode($root)); | ||||||||||||||||||||||||||||||
| 840 | # $$self{document}->documentElement->setNamespace($root->namespaceURI, $root->prefix, 1); | ||||||||||||||||||||||||||||||
| 841 | $root->setNamespace($root->namespaceURI, $root->prefix, 1); | ||||||||||||||||||||||||||||||
| 842 | foreach my $node ($self->findnodes("//*[\@xml:id]")) { # Now record all ID's | ||||||||||||||||||||||||||||||
| 843 | $$self{idcache}{ $node->getAttribute('xml:id') } = $node; } } | ||||||||||||||||||||||||||||||
| 844 | elsif ($roottype eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 845 | $$self{document} = XML::LibXML::Document->new("1.0", "UTF-8"); | ||||||||||||||||||||||||||||||
| 846 | my ($tag, $attributes, @children) = @$root; | ||||||||||||||||||||||||||||||
| 847 | my ($prefix, $localname) = $tag =~ /^(.*):(.*)$/; | ||||||||||||||||||||||||||||||
| 848 | my $nsuri = $$self{namespaces}{$prefix}; | ||||||||||||||||||||||||||||||
| 849 | my $node = $$self{document}->createElementNS($nsuri, $localname); | ||||||||||||||||||||||||||||||
| 850 | $$self{document}->setDocumentElement($node); | ||||||||||||||||||||||||||||||
| 851 | map { $$attributes{$_} && $node->setAttribute($_ => $$attributes{$_}) } keys %$attributes | ||||||||||||||||||||||||||||||
| 852 | if $attributes; | ||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||
| 854 | if (my $id = $$attributes{'xml:id'}) { | ||||||||||||||||||||||||||||||
| 855 | $self->recordID($id => $node); } | ||||||||||||||||||||||||||||||
| 856 | $self->addNodes($node, @children); } | ||||||||||||||||||||||||||||||
| 857 | else { | ||||||||||||||||||||||||||||||
| 858 | Fatal('unexpected', $root, undef, "Dont know how to use '$root' as document element"); } | ||||||||||||||||||||||||||||||
| 859 | # set URI to destination | ||||||||||||||||||||||||||||||
| 860 | $$self{document}->setURI(URI::file->new($self->getDestination)); | ||||||||||||||||||||||||||||||
| 861 | return $self; } | ||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||
| 863 | our @MonthNames = (qw( January February March April May June | ||||||||||||||||||||||||||||||
| 864 | July August September October November December)); | ||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||
| 866 | sub addDate { | ||||||||||||||||||||||||||||||
| 867 | my ($self, $fromdoc) = @_; | ||||||||||||||||||||||||||||||
| 868 | if (!$self->findnodes('ltx:date', $self->getDocumentElement)) { | ||||||||||||||||||||||||||||||
| 869 | my @dates; | ||||||||||||||||||||||||||||||
| 870 | # $fromdoc's document has some, so copy them. | ||||||||||||||||||||||||||||||
| 871 | if ($fromdoc && (@dates = $fromdoc->findnodes('ltx:date', $fromdoc->getDocumentElement))) { | ||||||||||||||||||||||||||||||
| 872 | $self->addNodes($self->getDocumentElement, @dates); } } | ||||||||||||||||||||||||||||||
| 873 | return; } | ||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||
| 875 | #====================================================================== | ||||||||||||||||||||||||||||||
| 876 | # Accessors | ||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||
| 878 | sub getDocument { | ||||||||||||||||||||||||||||||
| 879 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 880 | return $$self{document}; } | ||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||
| 882 | sub getDocumentElement { | ||||||||||||||||||||||||||||||
| 883 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 884 | return $$self{document}->documentElement; } | ||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||
| 886 | sub getSource { | ||||||||||||||||||||||||||||||
| 887 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 888 | return $$self{source}; } | ||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||
| 890 | sub getSourceDirectory { | ||||||||||||||||||||||||||||||
| 891 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 892 | return $$self{sourceDirectory} || '.'; } | ||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||
| 894 | sub getSearchPaths { | ||||||||||||||||||||||||||||||
| 895 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 896 | return @{ $$self{searchpaths} }; } | ||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||
| 898 | sub getDestination { | ||||||||||||||||||||||||||||||
| 899 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 900 | return $$self{destination}; } | ||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||
| 902 | sub getDestinationDirectory { | ||||||||||||||||||||||||||||||
| 903 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 904 | return $$self{destinationDirectory}; } | ||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||
| 906 | sub getSiteDirectory { | ||||||||||||||||||||||||||||||
| 907 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 908 | return $$self{siteDirectory}; } | ||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||
| 910 | # Given an absolute pathname in the document destination directory, | ||||||||||||||||||||||||||||||
| 911 | # return the corresponding pathname relative to the site directory (they maybe different!). | ||||||||||||||||||||||||||||||
| 912 | sub siteRelativePathname { | ||||||||||||||||||||||||||||||
| 913 | my ($self, $pathname) = @_; | ||||||||||||||||||||||||||||||
| 914 | return (defined $pathname ? pathname_relative($pathname, $$self{siteDirectory}) : undef); } | ||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||
| 916 | sub siteRelativeDestination { | ||||||||||||||||||||||||||||||
| 917 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 918 | return (defined $$self{destination} | ||||||||||||||||||||||||||||||
| 919 | ? pathname_relative($$self{destination}, $$self{siteDirectory}) | ||||||||||||||||||||||||||||||
| 920 | : undef); } | ||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||
| 922 | sub getParentDocument { | ||||||||||||||||||||||||||||||
| 923 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 924 | return $$self{parentDocument}; } | ||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||
| 926 | sub getAncestorDocument { | ||||||||||||||||||||||||||||||
| 927 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 928 | my ($doc, $d) = $self; | ||||||||||||||||||||||||||||||
| 929 | while ($d = $$doc{parentDocument}) { | ||||||||||||||||||||||||||||||
| 930 | $doc = $d; } | ||||||||||||||||||||||||||||||
| 931 | return $doc; } | ||||||||||||||||||||||||||||||
| 932 | |||||||||||||||||||||||||||||||
| 933 | sub toString { | ||||||||||||||||||||||||||||||
| 934 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 935 | return $$self{document}->toString(1); } | ||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||
| 937 | sub getDestinationExtension { | ||||||||||||||||||||||||||||||
| 938 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 939 | return ($$self{destination} =~ /\.([^\.\/]*)$/ ? $1 : undef); } | ||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||
| 941 | sub checkDestination { | ||||||||||||||||||||||||||||||
| 942 | my ($self, $reldest) = @_; | ||||||||||||||||||||||||||||||
| 943 | # make absolute (if not already absolute), hopefully in destination directory. | ||||||||||||||||||||||||||||||
| 944 | my $dest = pathname_absolute($reldest, $self->getDestinationDirectory); | ||||||||||||||||||||||||||||||
| 945 | if (my $destdir = pathname_directory($dest)) { | ||||||||||||||||||||||||||||||
| 946 | pathname_mkdir($destdir) | ||||||||||||||||||||||||||||||
| 947 | or return Fatal("I/O", $destdir, undef, | ||||||||||||||||||||||||||||||
| 948 | "Could not create directory $destdir for $reldest: $!"); } | ||||||||||||||||||||||||||||||
| 949 | return $dest; } | ||||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||||
| 951 | sub stringify { | ||||||||||||||||||||||||||||||
| 952 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 953 | return 'Post::Document[' . $self->siteRelativeDestination . ']'; } | ||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||
| 955 | sub getLocator { | ||||||||||||||||||||||||||||||
| 956 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 957 | return $$self{source}; } | ||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||
| 959 | #====================================================================== | ||||||||||||||||||||||||||||||
| 960 | sub validate { | ||||||||||||||||||||||||||||||
| 961 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 962 | # Check for a RelaxNGSchema PI | ||||||||||||||||||||||||||||||
| 963 | my $schema; | ||||||||||||||||||||||||||||||
| 964 | foreach my $pi (@{ $$self{processingInstructions} }) { | ||||||||||||||||||||||||||||||
| 965 | if ($pi =~ /^\s*RelaxNGSchema\s*=\s*([\"\'])(.*?)\1\s*$/) { | ||||||||||||||||||||||||||||||
| 966 | $schema = $2; } } | ||||||||||||||||||||||||||||||
| 967 | if ($schema) { # Validate using rng | ||||||||||||||||||||||||||||||
| 968 | my $rng = LaTeXML::Common::XML::RelaxNG->new($schema, searchpaths => [$self->getSearchPaths]); | ||||||||||||||||||||||||||||||
| 969 | Error('I/O', $schema, undef, "Failed to load RelaxNG schema $schema" . "Response was: $@") | ||||||||||||||||||||||||||||||
| 970 | unless $rng; | ||||||||||||||||||||||||||||||
| 971 | my $v = eval { | ||||||||||||||||||||||||||||||
| 972 | local $LaTeXML::IGNORE_ERRORS = 1; | ||||||||||||||||||||||||||||||
| 973 | $rng->validate($$self{document}); }; | ||||||||||||||||||||||||||||||
| 974 | Error("malformed", 'document', undef, | ||||||||||||||||||||||||||||||
| 975 | "Document fails RelaxNG validation (" . $schema . ")", | ||||||||||||||||||||||||||||||
| 976 | "Validation reports: " . $@, | ||||||||||||||||||||||||||||||
| 977 | "(Jing may provide a more precise report; https://relaxng.org/jclark/jing.html)") | ||||||||||||||||||||||||||||||
| 978 | if $@ || !defined $v; } | ||||||||||||||||||||||||||||||
| 979 | elsif (my $decldtd = $$self{document}->internalSubset) { # Else look for DTD Declaration | ||||||||||||||||||||||||||||||
| 980 | my $dtd = XML::LibXML::Dtd->new($decldtd->publicId, $decldtd->systemId); | ||||||||||||||||||||||||||||||
| 981 | if (!$dtd) { | ||||||||||||||||||||||||||||||
| 982 | Error("I/O", $decldtd->publicId, undef, | ||||||||||||||||||||||||||||||
| 983 | "Failed to load DTD " . $decldtd->publicId . " at " . $decldtd->systemId, | ||||||||||||||||||||||||||||||
| 984 | "skipping validation"); } | ||||||||||||||||||||||||||||||
| 985 | else { | ||||||||||||||||||||||||||||||
| 986 | my $v = eval { | ||||||||||||||||||||||||||||||
| 987 | local $LaTeXML::IGNORE_ERRORS = 1; | ||||||||||||||||||||||||||||||
| 988 | $$self{document}->validate($dtd); }; | ||||||||||||||||||||||||||||||
| 989 | Error("malformed", 'document', undef, | ||||||||||||||||||||||||||||||
| 990 | "Document failed DTD validation (" . $decldtd->systemId . ")", | ||||||||||||||||||||||||||||||
| 991 | "Validation reports: " . $@) if $@ || !defined $v; } } | ||||||||||||||||||||||||||||||
| 992 | else { # Nothing found to validate with | ||||||||||||||||||||||||||||||
| 993 | Warn("expected", 'schema', undef, | ||||||||||||||||||||||||||||||
| 994 | "No Schema or DTD found for this document"); } | ||||||||||||||||||||||||||||||
| 995 | return; } | ||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||
| 997 | sub idcheck { | ||||||||||||||||||||||||||||||
| 998 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 999 | my %idcache = (); | ||||||||||||||||||||||||||||||
| 1000 | my %dups = (); | ||||||||||||||||||||||||||||||
| 1001 | my %missing = (); | ||||||||||||||||||||||||||||||
| 1002 | foreach my $node ($self->findnodes("//*[\@xml:id]")) { | ||||||||||||||||||||||||||||||
| 1003 | my $id = $node->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 1004 | $dups{$id} = 1 if $idcache{$id}; | ||||||||||||||||||||||||||||||
| 1005 | $idcache{$id} = 1; } | ||||||||||||||||||||||||||||||
| 1006 | foreach my $id (keys %{ $$self{idcache} }) { | ||||||||||||||||||||||||||||||
| 1007 | $missing{$id} = 1 unless $idcache{$id}; } | ||||||||||||||||||||||||||||||
| 1008 | Warn("unexpected", 'ids', undef, | ||||||||||||||||||||||||||||||
| 1009 | "IDs were duplicated in cache for " . $self->siteRelativeDestination, | ||||||||||||||||||||||||||||||
| 1010 | join(',', keys %dups)) | ||||||||||||||||||||||||||||||
| 1011 | if keys %dups; | ||||||||||||||||||||||||||||||
| 1012 | Warn("expected", 'ids', undef, "IDs were cached for " . $self->siteRelativeDestination | ||||||||||||||||||||||||||||||
| 1013 | . " but not in document", | ||||||||||||||||||||||||||||||
| 1014 | join(',', keys %missing)) | ||||||||||||||||||||||||||||||
| 1015 | if keys %missing; | ||||||||||||||||||||||||||||||
| 1016 | return; } | ||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||
| 1018 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1019 | sub findnodes { | ||||||||||||||||||||||||||||||
| 1020 | my ($self, $path, $node) = @_; | ||||||||||||||||||||||||||||||
| 1021 | return $XPATH->findnodes($path, $node || $$self{document}); } | ||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||
| 1023 | # Similar but returns only 1st node | ||||||||||||||||||||||||||||||
| 1024 | sub findnode { | ||||||||||||||||||||||||||||||
| 1025 | my ($self, $path, $node) = @_; | ||||||||||||||||||||||||||||||
| 1026 | my ($first) = $XPATH->findnodes($path, $node || $$self{document}); | ||||||||||||||||||||||||||||||
| 1027 | return $first; } | ||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||
| 1029 | sub findvalue { | ||||||||||||||||||||||||||||||
| 1030 | my ($self, $path, $node) = @_; | ||||||||||||||||||||||||||||||
| 1031 | return $XPATH->findvalue($path, $node || $$self{document}); } | ||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||
| 1033 | sub addNamespace { | ||||||||||||||||||||||||||||||
| 1034 | my ($self, $nsuri, $prefix) = @_; | ||||||||||||||||||||||||||||||
| 1035 | if (!$$self{namespaces}{$prefix} || ($$self{namespaces}{$prefix} ne $nsuri) | ||||||||||||||||||||||||||||||
| 1036 | || (($self->getDocumentElement->lookupNamespacePrefix($nsuri) || '') ne $prefix)) { | ||||||||||||||||||||||||||||||
| 1037 | $$self{namespaces}{$prefix} = $nsuri; | ||||||||||||||||||||||||||||||
| 1038 | $$self{namespaceURIs}{$nsuri} = $prefix; | ||||||||||||||||||||||||||||||
| 1039 | $XPATH->registerNS($prefix => $nsuri); | ||||||||||||||||||||||||||||||
| 1040 | $self->getDocumentElement->setNamespace($nsuri, $prefix, 0); } | ||||||||||||||||||||||||||||||
| 1041 | return; } | ||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||
| 1043 | use Carp; | ||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||
| 1045 | sub getQName { | ||||||||||||||||||||||||||||||
| 1046 | my ($self, $node) = @_; | ||||||||||||||||||||||||||||||
| 1047 | if (ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 1048 | return $$node[0]; } | ||||||||||||||||||||||||||||||
| 1049 | elsif (ref $node eq 'XML::LibXML::Element') { | ||||||||||||||||||||||||||||||
| 1050 | my $nsuri = $node->namespaceURI; | ||||||||||||||||||||||||||||||
| 1051 | if (!$nsuri) { # No namespace at all??? | ||||||||||||||||||||||||||||||
| 1052 | if ($node->nodeType == XML_ELEMENT_NODE) { | ||||||||||||||||||||||||||||||
| 1053 | return $node->localname; } | ||||||||||||||||||||||||||||||
| 1054 | else { | ||||||||||||||||||||||||||||||
| 1055 | return; } } | ||||||||||||||||||||||||||||||
| 1056 | elsif (my $prefix = $$self{namespaceURIs}{$nsuri}) { | ||||||||||||||||||||||||||||||
| 1057 | return $prefix . ":" . $node->localname; } | ||||||||||||||||||||||||||||||
| 1058 | else { | ||||||||||||||||||||||||||||||
| 1059 | # Hasn't got one; we'll create a prefix for internal use. | ||||||||||||||||||||||||||||||
| 1060 | my $prefix = "_ns" . (1 + scalar(grep { /^_ns\d+$/ } keys %{ $$self{namespaces} })); | ||||||||||||||||||||||||||||||
| 1061 | # Register it, but Don't add it to the document!!! (or xpath, for that matter) | ||||||||||||||||||||||||||||||
| 1062 | $$self{namespaces}{$prefix} = $nsuri; | ||||||||||||||||||||||||||||||
| 1063 | $$self{namespaceURIs}{$nsuri} = $prefix; | ||||||||||||||||||||||||||||||
| 1064 | return $prefix . ":" . $node->localname; } } | ||||||||||||||||||||||||||||||
| 1065 | else { | ||||||||||||||||||||||||||||||
| 1066 | # confess "What's this? $node\n"; | ||||||||||||||||||||||||||||||
| 1067 | return; } } | ||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||
| 1069 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1070 | # ADD nodes to $node in the document $self. | ||||||||||||||||||||||||||||||
| 1071 | # This takes a convenient recursive reprsentation for xml: | ||||||||||||||||||||||||||||||
| 1072 | # data = string | [$tagname, {attr=>value,..}, @children...] | ||||||||||||||||||||||||||||||
| 1073 | # The $tagname should have a namespace prefix whose URI has been | ||||||||||||||||||||||||||||||
| 1074 | # registered with addNamespace. | ||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||
| 1076 | # Note that we're currently ignoring duplicated ids. | ||||||||||||||||||||||||||||||
| 1077 | # these should only happen from rearrangement and copying of document fragments | ||||||||||||||||||||||||||||||
| 1078 | # with embedded bits of math in them, which have those XMTok/XMRef pairs. | ||||||||||||||||||||||||||||||
| 1079 | # If those are the cases, we should end up finding the original id'd item, anyway, right? | ||||||||||||||||||||||||||||||
| 1080 | # | ||||||||||||||||||||||||||||||
| 1081 | # NOTE that only XML::LibXML's addNewChild deals cleanly with namespaces | ||||||||||||||||||||||||||||||
| 1082 | # and since there is only an "add" (ie. append) version (not prepend, insert after, etc) | ||||||||||||||||||||||||||||||
| 1083 | # we have to orient everything towards appending. | ||||||||||||||||||||||||||||||
| 1084 | # In particular, see the perversity in the following few methods. | ||||||||||||||||||||||||||||||
| 1085 | sub addNodes { | ||||||||||||||||||||||||||||||
| 1086 | no warnings 'recursion'; | ||||||||||||||||||||||||||||||
| 1087 | my ($self, $node, @data) = @_; | ||||||||||||||||||||||||||||||
| 1088 | foreach my $child (@data) { | ||||||||||||||||||||||||||||||
| 1089 | if (ref $child eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 1090 | my ($tag, $attributes, @children) = @$child; | ||||||||||||||||||||||||||||||
| 1091 | if ($tag eq '_Fragment_') { | ||||||||||||||||||||||||||||||
| 1092 | my $indent; # Derive indentation from indentation of $node | ||||||||||||||||||||||||||||||
| 1093 | if (my $pre = $node->previousSibling) { | ||||||||||||||||||||||||||||||
| 1094 | if (($pre->nodeType == XML_TEXT_NODE) && (($pre = $pre->textContent) =~ /^\s*$/)) { | ||||||||||||||||||||||||||||||
| 1095 | $indent = $pre . ' '; } } | ||||||||||||||||||||||||||||||
| 1096 | if ($indent) { | ||||||||||||||||||||||||||||||
| 1097 | $self->addNodes($node, map { ($indent, $_) } @children); } | ||||||||||||||||||||||||||||||
| 1098 | else { | ||||||||||||||||||||||||||||||
| 1099 | $self->addNodes($node, @children); } } | ||||||||||||||||||||||||||||||
| 1100 | else { | ||||||||||||||||||||||||||||||
| 1101 | my ($prefix, $localname) = $tag =~ /^(.*):(.*)$/; | ||||||||||||||||||||||||||||||
| 1102 | my $nsuri = $prefix && $$self{namespaces}{$prefix}; | ||||||||||||||||||||||||||||||
| 1103 | Warn('expected', 'namespace', undef, "No namespace on '$tag'") unless $nsuri; | ||||||||||||||||||||||||||||||
| 1104 | my $new; | ||||||||||||||||||||||||||||||
| 1105 | if (ref $node eq 'LibXML::XML::Document') { | ||||||||||||||||||||||||||||||
| 1106 | $new = $node->createElementNS($nsuri, $localname); | ||||||||||||||||||||||||||||||
| 1107 | $node->setDocumentElement($new); } | ||||||||||||||||||||||||||||||
| 1108 | else { | ||||||||||||||||||||||||||||||
| 1109 | $new = $node->addNewChild($nsuri, $localname); } | ||||||||||||||||||||||||||||||
| 1110 | if ($attributes) { | ||||||||||||||||||||||||||||||
| 1111 | foreach my $key (sort keys %$attributes) { | ||||||||||||||||||||||||||||||
| 1112 | next unless defined $$attributes{$key}; | ||||||||||||||||||||||||||||||
| 1113 | next if $key =~ /^_/; # Ignore internal attributes | ||||||||||||||||||||||||||||||
| 1114 | my ($attrprefix, $attrname) = $key =~ /^(.*):(.*)$/; | ||||||||||||||||||||||||||||||
| 1115 | my $value = $$attributes{$key}; | ||||||||||||||||||||||||||||||
| 1116 | if ($key eq 'xml:id') { | ||||||||||||||||||||||||||||||
| 1117 | if (defined $$self{idcache}{$value}) { # Duplicated ID ?!?! | ||||||||||||||||||||||||||||||
| 1118 | my $newid = $self->uniquifyID($value); | ||||||||||||||||||||||||||||||
| 1119 | Info('unexpected', 'duplicate_id', undef, | ||||||||||||||||||||||||||||||
| 1120 | "Duplicated id=$value using $newid " . ($$self{destination} || '')); | ||||||||||||||||||||||||||||||
| 1121 | $value = $newid; } | ||||||||||||||||||||||||||||||
| 1122 | $self->recordID($value => $new); | ||||||||||||||||||||||||||||||
| 1123 | $new->setAttribute($key, $value); } | ||||||||||||||||||||||||||||||
| 1124 | elsif ($attrprefix && ($attrprefix ne 'xml')) { | ||||||||||||||||||||||||||||||
| 1125 | my $attrnsuri = $attrprefix && $$self{namespaces}{$attrprefix}; | ||||||||||||||||||||||||||||||
| 1126 | $new->setAttributeNS($attrnsuri, $key, $$attributes{$key}); } | ||||||||||||||||||||||||||||||
| 1127 | else { | ||||||||||||||||||||||||||||||
| 1128 | $new->setAttribute($key, $$attributes{$key}); } } } | ||||||||||||||||||||||||||||||
| 1129 | $self->addNodes($new, @children); } } | ||||||||||||||||||||||||||||||
| 1130 | elsif ((ref $child) =~ /^XML::LibXML::/) { | ||||||||||||||||||||||||||||||
| 1131 | my $type = $child->nodeType; | ||||||||||||||||||||||||||||||
| 1132 | if ($type == XML_ELEMENT_NODE) { | ||||||||||||||||||||||||||||||
| 1133 | # Note: this isn't actually much slower than $node->appendChild($child) ! | ||||||||||||||||||||||||||||||
| 1134 | my $nsuri = $child->namespaceURI; | ||||||||||||||||||||||||||||||
| 1135 | my $localname = $child->localname; | ||||||||||||||||||||||||||||||
| 1136 | my $new; | ||||||||||||||||||||||||||||||
| 1137 | if (ref $node eq 'LibXML::XML::Document') { | ||||||||||||||||||||||||||||||
| 1138 | $new = $node->createElementNS($nsuri, $localname); | ||||||||||||||||||||||||||||||
| 1139 | $node->setDocumentElement($new); } | ||||||||||||||||||||||||||||||
| 1140 | else { | ||||||||||||||||||||||||||||||
| 1141 | $new = $node->addNewChild($nsuri, $localname); } | ||||||||||||||||||||||||||||||
| 1142 | foreach my $attr ($child->attributes) { | ||||||||||||||||||||||||||||||
| 1143 | my $atype = $attr->nodeType; | ||||||||||||||||||||||||||||||
| 1144 | if ($atype == XML_ATTRIBUTE_NODE) { | ||||||||||||||||||||||||||||||
| 1145 | my $key = $attr->nodeName; | ||||||||||||||||||||||||||||||
| 1146 | if ($key =~ /^_/) { } # don't copy internal attributes | ||||||||||||||||||||||||||||||
| 1147 | elsif ($key eq 'xml:id') { | ||||||||||||||||||||||||||||||
| 1148 | my $value = $attr->getValue; | ||||||||||||||||||||||||||||||
| 1149 | my $old; | ||||||||||||||||||||||||||||||
| 1150 | if ((defined($old = $$self{idcache}{$value})) # if xml:id was already used | ||||||||||||||||||||||||||||||
| 1151 | && !$old->isSameNode($child)) { # and the node was a different one | ||||||||||||||||||||||||||||||
| 1152 | my $newid = $self->uniquifyID($value); | ||||||||||||||||||||||||||||||
| 1153 | Info('unexpected', 'duplicate_id', undef, | ||||||||||||||||||||||||||||||
| 1154 | "Duplicated id=$value using $newid " . ($$self{destination} || '')); | ||||||||||||||||||||||||||||||
| 1155 | $value = $newid; } | ||||||||||||||||||||||||||||||
| 1156 | $self->recordID($value => $new); | ||||||||||||||||||||||||||||||
| 1157 | $new->setAttribute($key, $value); } | ||||||||||||||||||||||||||||||
| 1158 | elsif (my $ns = $attr->namespaceURI) { | ||||||||||||||||||||||||||||||
| 1159 | $new->setAttributeNS($ns, $attr->name, $attr->getValue); } | ||||||||||||||||||||||||||||||
| 1160 | else { | ||||||||||||||||||||||||||||||
| 1161 | $new->setAttribute($attr->localname, $attr->getValue); } } | ||||||||||||||||||||||||||||||
| 1162 | } | ||||||||||||||||||||||||||||||
| 1163 | $self->addNodes($new, $child->childNodes); } | ||||||||||||||||||||||||||||||
| 1164 | elsif ($type == XML_DOCUMENT_FRAG_NODE) { | ||||||||||||||||||||||||||||||
| 1165 | $self->addNodes($node, $child->childNodes); } | ||||||||||||||||||||||||||||||
| 1166 | elsif ($type == XML_TEXT_NODE) { | ||||||||||||||||||||||||||||||
| 1167 | $node->appendTextNode($child->textContent); } | ||||||||||||||||||||||||||||||
| 1168 | } | ||||||||||||||||||||||||||||||
| 1169 | elsif (ref $child) { | ||||||||||||||||||||||||||||||
| 1170 | Warn('misdefined', $child, undef, "Dont know how to add $child to $node; ignoring"); } | ||||||||||||||||||||||||||||||
| 1171 | elsif (defined $child) { | ||||||||||||||||||||||||||||||
| 1172 | $node->appendTextNode($child); } } | ||||||||||||||||||||||||||||||
| 1173 | return; } | ||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||
| 1175 | # Remove @nodes from the document | ||||||||||||||||||||||||||||||
| 1176 | # Allow the nodes to be array form with possibly nested XML that needs to be removed. | ||||||||||||||||||||||||||||||
| 1177 | sub removeNodes { | ||||||||||||||||||||||||||||||
| 1178 | my ($self, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 1179 | foreach my $node (@nodes) { | ||||||||||||||||||||||||||||||
| 1180 | my $ref = ref $node; | ||||||||||||||||||||||||||||||
| 1181 | if (!$ref) { } | ||||||||||||||||||||||||||||||
| 1182 | elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 1183 | my ($t, $a, @n) = @$node; | ||||||||||||||||||||||||||||||
| 1184 | if (my $id = $$a{'xml:id'}) { | ||||||||||||||||||||||||||||||
| 1185 | if ($$self{idcache}{$id}) { | ||||||||||||||||||||||||||||||
| 1186 | delete $$self{idcache}{$id}; } } | ||||||||||||||||||||||||||||||
| 1187 | $self->removeNodes(@n); } | ||||||||||||||||||||||||||||||
| 1188 | elsif ($ref =~ /^XML::LibXML::/) { | ||||||||||||||||||||||||||||||
| 1189 | if ($node->nodeType == XML_ELEMENT_NODE) { | ||||||||||||||||||||||||||||||
| 1190 | foreach my $idd ($self->findnodes("descendant-or-self::*[\@xml:id]", $node)) { | ||||||||||||||||||||||||||||||
| 1191 | my $id = $idd->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 1192 | if ($$self{idcache}{$id}) { | ||||||||||||||||||||||||||||||
| 1193 | delete $$self{idcache}{$id}; } } } | ||||||||||||||||||||||||||||||
| 1194 | $node->unlinkNode; } } | ||||||||||||||||||||||||||||||
| 1195 | return; } | ||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||
| 1197 | # These nodes will be removed, but later | ||||||||||||||||||||||||||||||
| 1198 | # So mark all id's in these trees as reusable | ||||||||||||||||||||||||||||||
| 1199 | sub preremoveNodes { | ||||||||||||||||||||||||||||||
| 1200 | my ($self, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 1201 | foreach my $node (@nodes) { | ||||||||||||||||||||||||||||||
| 1202 | my $ref = ref $node; | ||||||||||||||||||||||||||||||
| 1203 | if (!$ref) { } | ||||||||||||||||||||||||||||||
| 1204 | elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||
| 1205 | my ($t, $a, @n) = @$node; | ||||||||||||||||||||||||||||||
| 1206 | if (my $id = $$a{'xml:id'}) { | ||||||||||||||||||||||||||||||
| 1207 | $$self{idcache_reusable}{$id} = 1; } | ||||||||||||||||||||||||||||||
| 1208 | $self->preremoveNodes(@n); } | ||||||||||||||||||||||||||||||
| 1209 | elsif ($ref =~ /^XML::LibXML::/) { | ||||||||||||||||||||||||||||||
| 1210 | if ($node->nodeType == XML_ELEMENT_NODE) { | ||||||||||||||||||||||||||||||
| 1211 | foreach my $idd ($self->findnodes("descendant-or-self::*[\@xml:id]", $node)) { | ||||||||||||||||||||||||||||||
| 1212 | my $id = $idd->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 1213 | $$self{idcache_reusable}{$id} = 1; } } } } | ||||||||||||||||||||||||||||||
| 1214 | return; } | ||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||
| 1216 | sub removeBlankNodes { | ||||||||||||||||||||||||||||||
| 1217 | my ($self, $node) = @_; | ||||||||||||||||||||||||||||||
| 1218 | my $n = 0; | ||||||||||||||||||||||||||||||
| 1219 | foreach my $child ($node->childNodes) { | ||||||||||||||||||||||||||||||
| 1220 | if (($child->nodeType == XML_TEXT_NODE) && ($child->textContent =~ /^\s*$/)) { | ||||||||||||||||||||||||||||||
| 1221 | $node->removeChild($child); $n++; } } | ||||||||||||||||||||||||||||||
| 1222 | return $n; } | ||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||
| 1224 | # Replace $node by @replacements in the document | ||||||||||||||||||||||||||||||
| 1225 | sub replaceNode { | ||||||||||||||||||||||||||||||
| 1226 | my ($self, $node, @replacements) = @_; | ||||||||||||||||||||||||||||||
| 1227 | my ($parent, $following) = ($node->parentNode, undef); | ||||||||||||||||||||||||||||||
| 1228 | # Note that since we can only append new stuff, we've got to remove the following first. | ||||||||||||||||||||||||||||||
| 1229 | my @save = (); | ||||||||||||||||||||||||||||||
| 1230 | while (($following = $parent->lastChild) && ($$following != $$node)) { # Remove & Save following siblings. | ||||||||||||||||||||||||||||||
| 1231 | unshift(@save, $parent->removeChild($following)); } | ||||||||||||||||||||||||||||||
| 1232 | $self->removeNodes($node); | ||||||||||||||||||||||||||||||
| 1233 | $self->addNodes($parent, @replacements); | ||||||||||||||||||||||||||||||
| 1234 | map { $parent->appendChild($_) } @save; # Put these back. | ||||||||||||||||||||||||||||||
| 1235 | return; } | ||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||
| 1237 | # Put @nodes at the beginning of $node. | ||||||||||||||||||||||||||||||
| 1238 | sub prependNodes { | ||||||||||||||||||||||||||||||
| 1239 | my ($self, $node, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 1240 | my @save = (); | ||||||||||||||||||||||||||||||
| 1241 | # Note that since we can only append new stuff, we've got to remove the following first. | ||||||||||||||||||||||||||||||
| 1242 | while (my $last = $node->lastChild) { # Remove, but save, all children | ||||||||||||||||||||||||||||||
| 1243 | unshift(@save, $node->removeChild($last)); } | ||||||||||||||||||||||||||||||
| 1244 | $self->addNodes($node, @nodes); # Now, add the new nodes. | ||||||||||||||||||||||||||||||
| 1245 | map { $node->appendChild($_) } @save; # Put these back. | ||||||||||||||||||||||||||||||
| 1246 | return; } | ||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||
| 1248 | # Clone a node, but adjusting it so that it has unique id's. | ||||||||||||||||||||||||||||||
| 1249 | # $document->cloneNode($node) or ->cloneNode($node,$idsuffix) | ||||||||||||||||||||||||||||||
| 1250 | # This clones the node and adjusts any xml:id's within it to be unique. | ||||||||||||||||||||||||||||||
| 1251 | # Any idref's to those ids will be changed to the new id values. | ||||||||||||||||||||||||||||||
| 1252 | # If $idsuffix is supplied, it can be a simple string to append to the ids; | ||||||||||||||||||||||||||||||
| 1253 | # else can be a function of the id to modify it. | ||||||||||||||||||||||||||||||
| 1254 | # Then each $id is checked to see whether it is unique; If needed, | ||||||||||||||||||||||||||||||
| 1255 | # one or more letters are appended, until a new id is found. | ||||||||||||||||||||||||||||||
| 1256 | sub cloneNode { | ||||||||||||||||||||||||||||||
| 1257 | my ($self, $node, $idsuffix, %options) = @_; | ||||||||||||||||||||||||||||||
| 1258 | return $node unless ref $node; | ||||||||||||||||||||||||||||||
| 1259 | return $node if ref $node eq 'ARRAY'; # Should we deep clone if we get an array? Just return for now | ||||||||||||||||||||||||||||||
| 1260 | my $copy = $node->cloneNode(1); | ||||||||||||||||||||||||||||||
| 1261 | my $nocache = $options{nocache}; | ||||||||||||||||||||||||||||||
| 1262 | #### $idsuffix = '' unless defined $idsuffix; | ||||||||||||||||||||||||||||||
| 1263 | # Find all id's defined in the copy and change the id. | ||||||||||||||||||||||||||||||
| 1264 | my %idmap = (); | ||||||||||||||||||||||||||||||
| 1265 | foreach my $n ($self->findnodes('descendant-or-self::*[@xml:id]', $copy)) { | ||||||||||||||||||||||||||||||
| 1266 | my $id = $n->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 1267 | my $newid = $self->uniquifyID($id, $idsuffix); | ||||||||||||||||||||||||||||||
| 1268 | $idmap{$id} = $newid; | ||||||||||||||||||||||||||||||
| 1269 | $self->recordID($newid => $n) unless $nocache; | ||||||||||||||||||||||||||||||
| 1270 | $n->setAttribute('xml:id' => $newid); | ||||||||||||||||||||||||||||||
| 1271 | if (my $fragid = $n->getAttribute('fragid')) { # GACK!! | ||||||||||||||||||||||||||||||
| 1272 | $n->setAttribute(fragid => substr($newid, length($id) - length($fragid))); } } | ||||||||||||||||||||||||||||||
| 1273 | |||||||||||||||||||||||||||||||
| 1274 | # Now, replace all REFERENCES to those modified ids. | ||||||||||||||||||||||||||||||
| 1275 | foreach my $n ($self->findnodes('descendant-or-self::*[@idref]', $copy)) { | ||||||||||||||||||||||||||||||
| 1276 | if (my $id = $idmap{ $n->getAttribute('idref') }) { | ||||||||||||||||||||||||||||||
| 1277 | $n->setAttribute(idref => $id); } } # use id or fragid? | ||||||||||||||||||||||||||||||
| 1278 | # Finally, we probably shouldn't have any labels attributes in here either | ||||||||||||||||||||||||||||||
| 1279 | foreach my $n ($self->findnodes('descendant-or-self::*[@labels]', $copy)) { | ||||||||||||||||||||||||||||||
| 1280 | $n->removeAttribute('labels'); } | ||||||||||||||||||||||||||||||
| 1281 | # And, if we're relocating the node across documents, | ||||||||||||||||||||||||||||||
| 1282 | # we may need to patch relative pathnames! | ||||||||||||||||||||||||||||||
| 1283 | # ????? Something to think about in the future... | ||||||||||||||||||||||||||||||
| 1284 | # if(my $base = $options{basepathname}){ | ||||||||||||||||||||||||||||||
| 1285 | # foreach my $n ($self->findnodes('descendant::*/@graphic or descendant::*/@href', $copy)) { | ||||||||||||||||||||||||||||||
| 1286 | # $n->setvalue(relocate($n->value,$base)); }} | ||||||||||||||||||||||||||||||
| 1287 | return $copy; } | ||||||||||||||||||||||||||||||
| 1288 | |||||||||||||||||||||||||||||||
| 1289 | sub cloneNodes { | ||||||||||||||||||||||||||||||
| 1290 | my ($self, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 1291 | return map { $self->cloneNode($_) } @nodes; } | ||||||||||||||||||||||||||||||
| 1292 | |||||||||||||||||||||||||||||||
| 1293 | sub addSSValues { | ||||||||||||||||||||||||||||||
| 1294 | my ($self, $node, $key, $values) = @_; | ||||||||||||||||||||||||||||||
| 1295 | $values = $values->toAttribute if ref $values; | ||||||||||||||||||||||||||||||
| 1296 | if ((defined $values) && ($values ne '')) { # Skip if `empty'; but 0 is OK! | ||||||||||||||||||||||||||||||
| 1297 | my @values = split(/\s/, $values); | ||||||||||||||||||||||||||||||
| 1298 | if (my $oldvalues = $node->getAttribute($key)) { # previous values? | ||||||||||||||||||||||||||||||
| 1299 | my @old = split(/\s/, $oldvalues); | ||||||||||||||||||||||||||||||
| 1300 | foreach my $new (@values) { | ||||||||||||||||||||||||||||||
| 1301 | push(@old, $new) unless grep { $_ eq $new } @old; } | ||||||||||||||||||||||||||||||
| 1302 | $node->setAttribute($key => join(' ', sort @old)); } | ||||||||||||||||||||||||||||||
| 1303 | else { | ||||||||||||||||||||||||||||||
| 1304 | $node->setAttribute($key => join(' ', sort @values)); } } | ||||||||||||||||||||||||||||||
| 1305 | return; } | ||||||||||||||||||||||||||||||
| 1306 | |||||||||||||||||||||||||||||||
| 1307 | sub addClass { | ||||||||||||||||||||||||||||||
| 1308 | my ($self, $node, $class) = @_; | ||||||||||||||||||||||||||||||
| 1309 | return $self->addSSValues($node, class => $class); } | ||||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||||
| 1311 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1312 | # DUPLICATED from Core::Document...(see discussion there) | ||||||||||||||||||||||||||||||
| 1313 | # Decorations on one side of an XMDual should be attributed to the | ||||||||||||||||||||||||||||||
| 1314 | # parent node on the other side (see ->associateIDs) | ||||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||||
| 1316 | sub markXMNodeVisibility { | ||||||||||||||||||||||||||||||
| 1317 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 1318 | foreach my $math ($self->findnodes('//ltx:XMath/*')) { | ||||||||||||||||||||||||||||||
| 1319 | $self->markXMNodeVisibility_aux($math, 1, 1); } | ||||||||||||||||||||||||||||||
| 1320 | return; } | ||||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||||
| 1322 | sub markXMNodeVisibility_aux { | ||||||||||||||||||||||||||||||
| 1323 | no warnings 'recursion'; | ||||||||||||||||||||||||||||||
| 1324 | my ($self, $node, $cvis, $pvis) = @_; | ||||||||||||||||||||||||||||||
| 1325 | return unless $node; | ||||||||||||||||||||||||||||||
| 1326 | my $qname = $self->getQName($node); | ||||||||||||||||||||||||||||||
| 1327 | return if (!$cvis || $node->getAttribute('_cvis')) && (!$pvis || $node->getAttribute('_pvis')); | ||||||||||||||||||||||||||||||
| 1328 | $node->setAttribute('_cvis' => 1) if $cvis; | ||||||||||||||||||||||||||||||
| 1329 | $node->setAttribute('_pvis' => 1) if $pvis; | ||||||||||||||||||||||||||||||
| 1330 | if ($qname eq 'ltx:XMDual') { | ||||||||||||||||||||||||||||||
| 1331 | my ($c, $p) = element_nodes($node); | ||||||||||||||||||||||||||||||
| 1332 | $self->markXMNodeVisibility_aux($c, 1, 0) if $cvis; | ||||||||||||||||||||||||||||||
| 1333 | $self->markXMNodeVisibility_aux($p, 0, 1) if $pvis; } | ||||||||||||||||||||||||||||||
| 1334 | elsif ($qname eq 'ltx:XMRef') { | ||||||||||||||||||||||||||||||
| 1335 | # $self->markXMNodeVisibility_aux($self->realizeXMNode($node),$cvis,$pvis); } | ||||||||||||||||||||||||||||||
| 1336 | my $id = $node->getAttribute('idref'); | ||||||||||||||||||||||||||||||
| 1337 | $self->markXMNodeVisibility_aux($self->findNodeByID($id), $cvis, $pvis); } | ||||||||||||||||||||||||||||||
| 1338 | else { | ||||||||||||||||||||||||||||||
| 1339 | foreach my $child (element_nodes($node)) { | ||||||||||||||||||||||||||||||
| 1340 | $self->markXMNodeVisibility_aux($child, $cvis, $pvis); } } | ||||||||||||||||||||||||||||||
| 1341 | return; } | ||||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||||
| 1343 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1344 | # Given a list of nodes (or node constructors [tag,attr,content...]) | ||||||||||||||||||||||||||||||
| 1345 | # conjoin given a conjunction like ',' or a pair like [',', ' and '] | ||||||||||||||||||||||||||||||
| 1346 | sub conjoin { | ||||||||||||||||||||||||||||||
| 1347 | my ($self, $conjunction, @nodes) = @_; | ||||||||||||||||||||||||||||||
| 1348 | my ($comma, $and) = ($conjunction, $conjunction); | ||||||||||||||||||||||||||||||
| 1349 | ($comma, $and) = @$conjunction if ref $conjunction; | ||||||||||||||||||||||||||||||
| 1350 | my $n = scalar(@nodes); | ||||||||||||||||||||||||||||||
| 1351 | if ($n < 2) { | ||||||||||||||||||||||||||||||
| 1352 | return @nodes; } | ||||||||||||||||||||||||||||||
| 1353 | else { | ||||||||||||||||||||||||||||||
| 1354 | my @foo = (); | ||||||||||||||||||||||||||||||
| 1355 | push(@foo, shift(@nodes)); | ||||||||||||||||||||||||||||||
| 1356 | while ($nodes[1]) { | ||||||||||||||||||||||||||||||
| 1357 | push(@foo, $comma, shift(@nodes)); } | ||||||||||||||||||||||||||||||
| 1358 | push(@foo, $and, shift(@nodes)); | ||||||||||||||||||||||||||||||
| 1359 | return @foo; } } | ||||||||||||||||||||||||||||||
| 1360 | |||||||||||||||||||||||||||||||
| 1361 | # Find the initial letter in a string, or *. | ||||||||||||||||||||||||||||||
| 1362 | # Uses unicode decomposition to reduce accented characters to A-Z | ||||||||||||||||||||||||||||||
| 1363 | # If $force is true, skips any non-letter initials | ||||||||||||||||||||||||||||||
| 1364 | sub initial { | ||||||||||||||||||||||||||||||
| 1365 | my ($self, $string, $force) = @_; | ||||||||||||||||||||||||||||||
| 1366 | $string = NFD($string); # Decompose accents, etc. | ||||||||||||||||||||||||||||||
| 1367 | $string =~ s/^\s+//gs; | ||||||||||||||||||||||||||||||
| 1368 | $string =~ s/^[^a-zA-Z]*// if $force; | ||||||||||||||||||||||||||||||
| 1369 | return ($string =~ /^([a-zA-Z])/ ? uc($1) : '*'); } | ||||||||||||||||||||||||||||||
| 1370 | |||||||||||||||||||||||||||||||
| 1371 | # This would typically be called to normalize the leading/trailing whitespace of nodes | ||||||||||||||||||||||||||||||
| 1372 | # that take mixed markup. WE SHOULDN'T BE DOING THIS. We need to NOT add "ignorable whitespace" | ||||||||||||||||||||||||||||||
| 1373 | # to nodes that CAN HAVE mixed content. otherwise we don't know if it is ignorable! | ||||||||||||||||||||||||||||||
| 1374 | sub trimChildNodes { | ||||||||||||||||||||||||||||||
| 1375 | my ($self, $node) = @_; | ||||||||||||||||||||||||||||||
| 1376 | if (!$node) { | ||||||||||||||||||||||||||||||
| 1377 | return (); } | ||||||||||||||||||||||||||||||
| 1378 | elsif (!ref $node) { | ||||||||||||||||||||||||||||||
| 1379 | return ($node); } | ||||||||||||||||||||||||||||||
| 1380 | elsif (my @children = $node->childNodes) { | ||||||||||||||||||||||||||||||
| 1381 | if ($children[0] && $children[0]->nodeType == XML_TEXT_NODE) { | ||||||||||||||||||||||||||||||
| 1382 | my $s = $children[0]->data; | ||||||||||||||||||||||||||||||
| 1383 | $s =~ s/^\s+//; | ||||||||||||||||||||||||||||||
| 1384 | if ($s) { | ||||||||||||||||||||||||||||||
| 1385 | $children[0]->setData($s); } | ||||||||||||||||||||||||||||||
| 1386 | else { | ||||||||||||||||||||||||||||||
| 1387 | shift(@children); } } | ||||||||||||||||||||||||||||||
| 1388 | if ($children[-1] && $children[-1]->nodeType == XML_TEXT_NODE) { | ||||||||||||||||||||||||||||||
| 1389 | my $s = $children[-1]->data; | ||||||||||||||||||||||||||||||
| 1390 | $s =~ s/\s+$//; | ||||||||||||||||||||||||||||||
| 1391 | if ($s) { | ||||||||||||||||||||||||||||||
| 1392 | $children[-1]->setData($s); } | ||||||||||||||||||||||||||||||
| 1393 | else { | ||||||||||||||||||||||||||||||
| 1394 | pop(@children); } } | ||||||||||||||||||||||||||||||
| 1395 | return @children; } | ||||||||||||||||||||||||||||||
| 1396 | else { | ||||||||||||||||||||||||||||||
| 1397 | return (); } } | ||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||
| 1399 | sub unisort { | ||||||||||||||||||||||||||||||
| 1400 | my ($self, @keys) = @_; | ||||||||||||||||||||||||||||||
| 1401 | # Get a (possibly cached) sorter from POST appropriate for this document's language | ||||||||||||||||||||||||||||||
| 1402 | my $lang = $self->getDocumentElement->getAttribute('xml:lang') || 'en'; | ||||||||||||||||||||||||||||||
| 1403 | return $LaTeXML::POST->getsorter($lang)->sort(@keys); } | ||||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||||
| 1405 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||||
| 1407 | sub addNavigation { | ||||||||||||||||||||||||||||||
| 1408 | my ($self, $relation, $id) = @_; | ||||||||||||||||||||||||||||||
| 1409 | return if $self->findnode('//ltx:navigation/ltx:ref[@rel="' . $relation . '"][@idref="' . $id . '"]'); | ||||||||||||||||||||||||||||||
| 1410 | my $ref = ['ltx:ref', { idref => $id, rel => $relation, show => 'toctitle' }]; | ||||||||||||||||||||||||||||||
| 1411 | if (my $nav = $self->findnode('//ltx:navigation')) { | ||||||||||||||||||||||||||||||
| 1412 | $self->addNodes($nav, $ref); } | ||||||||||||||||||||||||||||||
| 1413 | else { | ||||||||||||||||||||||||||||||
| 1414 | $self->addNodes($self->getDocumentElement, ['ltx:navigation', {}, $ref]); } | ||||||||||||||||||||||||||||||
| 1415 | return; } | ||||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||||
| 1417 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1418 | # Support for ID's | ||||||||||||||||||||||||||||||
| 1419 | |||||||||||||||||||||||||||||||
| 1420 | sub recordID { | ||||||||||||||||||||||||||||||
| 1421 | my ($self, $id, $node) = @_; | ||||||||||||||||||||||||||||||
| 1422 | # make an issue if already there? | ||||||||||||||||||||||||||||||
| 1423 | $$self{idcache}{$id} = $node; | ||||||||||||||||||||||||||||||
| 1424 | delete $$self{idcache_reserve}{$id}; # And no longer reserved | ||||||||||||||||||||||||||||||
| 1425 | delete $$self{idcache_reusable}{$id}; # or reusable | ||||||||||||||||||||||||||||||
| 1426 | return; } | ||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||
| 1428 | sub findNodeByID { | ||||||||||||||||||||||||||||||
| 1429 | my ($self, $id) = @_; | ||||||||||||||||||||||||||||||
| 1430 | my $node = $$self{idcache}{$id}; | ||||||||||||||||||||||||||||||
| 1431 | return $$self{idcache}{$id}; } | ||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||
| 1433 | # If $branch given, should be 'content' or 'presentation' | ||||||||||||||||||||||||||||||
| 1434 | sub realizeXMNode { | ||||||||||||||||||||||||||||||
| 1435 | my ($self, $node, $branch) = @_; | ||||||||||||||||||||||||||||||
| 1436 | if ($branch) { | ||||||||||||||||||||||||||||||
| 1437 | while ($node) { | ||||||||||||||||||||||||||||||
| 1438 | my $qname = $self->getQName($node); | ||||||||||||||||||||||||||||||
| 1439 | if ($qname eq 'ltx:XMRef') { | ||||||||||||||||||||||||||||||
| 1440 | my $id = $node->getAttribute('idref'); | ||||||||||||||||||||||||||||||
| 1441 | if (my $realnode = $self->findNodeByID($id)) { | ||||||||||||||||||||||||||||||
| 1442 | $node = $realnode; } | ||||||||||||||||||||||||||||||
| 1443 | else { | ||||||||||||||||||||||||||||||
| 1444 | Error('expected', 'id', undef, "Cannot find a node with xml:id='$id'"); | ||||||||||||||||||||||||||||||
| 1445 | return; } } | ||||||||||||||||||||||||||||||
| 1446 | elsif ($qname eq 'ltx:XMDual') { | ||||||||||||||||||||||||||||||
| 1447 | my ($content, $presentation) = element_nodes($node); | ||||||||||||||||||||||||||||||
| 1448 | $node = ($branch eq 'content' ? $content : $presentation); } | ||||||||||||||||||||||||||||||
| 1449 | else { | ||||||||||||||||||||||||||||||
| 1450 | return $node; } } } | ||||||||||||||||||||||||||||||
| 1451 | elsif ($self->getQName($node) eq 'ltx:XMRef') { | ||||||||||||||||||||||||||||||
| 1452 | my $id = $node->getAttribute('idref'); | ||||||||||||||||||||||||||||||
| 1453 | if (my $realnode = $self->findNodeByID($id)) { | ||||||||||||||||||||||||||||||
| 1454 | return $realnode; } | ||||||||||||||||||||||||||||||
| 1455 | else { | ||||||||||||||||||||||||||||||
| 1456 | Error('expected', 'id', undef, "Cannot find a node with xml:id='$id'"); | ||||||||||||||||||||||||||||||
| 1457 | return; } } | ||||||||||||||||||||||||||||||
| 1458 | return $node; } | ||||||||||||||||||||||||||||||
| 1459 | |||||||||||||||||||||||||||||||
| 1460 | sub uniquifyID { | ||||||||||||||||||||||||||||||
| 1461 | my ($self, $baseid, $suffix) = @_; | ||||||||||||||||||||||||||||||
| 1462 | my $id = $baseid; | ||||||||||||||||||||||||||||||
| 1463 | $id = (ref $suffix eq 'CODE' ? &$suffix($id) : $id . $suffix) if defined $suffix; | ||||||||||||||||||||||||||||||
| 1464 | my $cachekey = $id; | ||||||||||||||||||||||||||||||
| 1465 | while (($$self{idcache}{$id} || $$self{idcache_reserve}{$id}) && !$$self{idcache_reusable}{$id}) { | ||||||||||||||||||||||||||||||
| 1466 | $id = $baseid . radix_alpha(++$$self{idcache_clashes}{$cachekey}); | ||||||||||||||||||||||||||||||
| 1467 | $id = (ref $suffix eq 'CODE' ? &$suffix($id) : $id . $suffix) if defined $suffix; } | ||||||||||||||||||||||||||||||
| 1468 | delete $$self{idcache_reusable}{$id}; # $id is no longer reusable | ||||||||||||||||||||||||||||||
| 1469 | $$self{idcache_reserve}{$id} = 1; # and we'll consider it reserved until recorded. | ||||||||||||||||||||||||||||||
| 1470 | return $id; } | ||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||
| 1472 | # Generate, add and register an xml:id for $node. | ||||||||||||||||||||||||||||||
| 1473 | # Unless it already has an id, the created id will | ||||||||||||||||||||||||||||||
| 1474 | # be "structured" relative to it's parent using $prefix | ||||||||||||||||||||||||||||||
| 1475 | sub generateNodeID { | ||||||||||||||||||||||||||||||
| 1476 | my ($self, $node, $prefix, $reusable) = @_; | ||||||||||||||||||||||||||||||
| 1477 | my $id = $node->getAttribute('xml:id'); | ||||||||||||||||||||||||||||||
| 1478 | return $id if $id; | ||||||||||||||||||||||||||||||
| 1479 | # Find the closest parent with an ID | ||||||||||||||||||||||||||||||
| 1480 | my ($parent, $pid, $n) = ($node->parentNode, undef, undef); | ||||||||||||||||||||||||||||||
| 1481 | while ($parent && !($pid = $parent->getAttribute('xml:id'))) { | ||||||||||||||||||||||||||||||
| 1482 | $parent = $parent->parentNode; } | ||||||||||||||||||||||||||||||
| 1483 | # Now find the next unused id relative to the parent id, as "prefix<number>" | ||||||||||||||||||||||||||||||
| 1484 | $pid .= '.' if $pid; | ||||||||||||||||||||||||||||||
| 1485 | for ($n = 1 ; ($id = $pid . $prefix . $n) | ||||||||||||||||||||||||||||||
| 1486 | && ($$self{idcache}{$id} || $$self{idcache_reserved}{$id}) ; $n++) { } | ||||||||||||||||||||||||||||||
| 1487 | $node->setAttribute('xml:id' => $id); | ||||||||||||||||||||||||||||||
| 1488 | $$self{idcache}{$id} = $node; | ||||||||||||||||||||||||||||||
| 1489 | $$self{idcache_reusable}{$id} = $reusable; | ||||||||||||||||||||||||||||||
| 1490 | # If we've already been scanned, and have fragid's, create one here, too. | ||||||||||||||||||||||||||||||
| 1491 | if (my $fragid = $parent && $parent->getAttribute('fragid')) { | ||||||||||||||||||||||||||||||
| 1492 | $node->setAttribute(fragid => $fragid . '.' . $prefix . $n); } | ||||||||||||||||||||||||||||||
| 1493 | return $id; } | ||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||
| 1495 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1496 | # adjust_latexml_doctype($doc,"Foo","Bar") => | ||||||||||||||||||||||||||||||
| 1497 | # <!DOCTYPE document PUBLIC "-//NIST LaTeXML//LaTeXML article + Foo + Bar" | ||||||||||||||||||||||||||||||
| 1498 | # "http://dlmf.nist.gov/LaTeXML/LaTeXML-Foo-Bar.dtd"> | ||||||||||||||||||||||||||||||
| 1499 | sub adjust_latexml_doctype { | ||||||||||||||||||||||||||||||
| 1500 | my ($self, @additions) = @_; | ||||||||||||||||||||||||||||||
| 1501 | my $doc = $$self{document}; | ||||||||||||||||||||||||||||||
| 1502 | if (my $dtd = $doc->internalSubset) { | ||||||||||||||||||||||||||||||
| 1503 | if ($dtd->toString | ||||||||||||||||||||||||||||||
| 1504 | =~ /^<!DOCTYPE\s+(\w+)\s+PUBLIC\s+(\"|\')-\/\/NIST LaTeXML\/\/LaTeXML\s+([^\"]*)\2\s+(\"|\')([^\"]*)\4>$/) { | ||||||||||||||||||||||||||||||
| 1505 | my ($root, $parts, $system) = ($1, $3, $5); | ||||||||||||||||||||||||||||||
| 1506 | my ($type, @addns) = split(/ \+ /, $parts); | ||||||||||||||||||||||||||||||
| 1507 | my %addns = (); | ||||||||||||||||||||||||||||||
| 1508 | map { $addns{$_} = 1 } @addns, @additions; | ||||||||||||||||||||||||||||||
| 1509 | @addns = sort keys %addns; | ||||||||||||||||||||||||||||||
| 1510 | my $publicid = join(' + ', "-//NIST LaTeXML//LaTeXML $type", @addns); | ||||||||||||||||||||||||||||||
| 1511 | my $systemid = join('-', "http://dlmf.nist.gov/LaTeXML/LaTeXML", @addns) . ".dtd"; | ||||||||||||||||||||||||||||||
| 1512 | $doc->removeInternalSubset; # Apparently we've got to remove it first. | ||||||||||||||||||||||||||||||
| 1513 | $doc->createInternalSubset($root, $publicid, $systemid); } } | ||||||||||||||||||||||||||||||
| 1514 | return; } | ||||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||||
| 1516 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1517 | # Cache support: storage of data from previous run. | ||||||||||||||||||||||||||||||
| 1518 | # ? | ||||||||||||||||||||||||||||||
| 1519 | |||||||||||||||||||||||||||||||
| 1520 | # cacheFile as parameter ???? | ||||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||||
| 1522 | sub cacheLookup { | ||||||||||||||||||||||||||||||
| 1523 | my ($self, $key) = @_; | ||||||||||||||||||||||||||||||
| 1524 | $self->openCache; | ||||||||||||||||||||||||||||||
| 1525 | $key = Encode::encode_utf8($key) if $key; | ||||||||||||||||||||||||||||||
| 1526 | return $$self{cache}{$key}; } | ||||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||||
| 1528 | sub cacheStore { | ||||||||||||||||||||||||||||||
| 1529 | my ($self, $key, $value) = @_; | ||||||||||||||||||||||||||||||
| 1530 | $self->openCache; | ||||||||||||||||||||||||||||||
| 1531 | $key = Encode::encode_utf8($key) if $key; | ||||||||||||||||||||||||||||||
| 1532 | if (defined $value) { | ||||||||||||||||||||||||||||||
| 1533 | $$self{cache}{$key} = $value; } | ||||||||||||||||||||||||||||||
| 1534 | else { | ||||||||||||||||||||||||||||||
| 1535 | delete $$self{cache}{$key}; } | ||||||||||||||||||||||||||||||
| 1536 | return; } | ||||||||||||||||||||||||||||||
| 1537 | |||||||||||||||||||||||||||||||
| 1538 | sub openCache { | ||||||||||||||||||||||||||||||
| 1539 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 1540 | if (!$$self{cache}) { | ||||||||||||||||||||||||||||||
| 1541 | $$self{cache} = {}; | ||||||||||||||||||||||||||||||
| 1542 | my $dbfile = $self->checkDestination("LaTeXML.cache"); | ||||||||||||||||||||||||||||||
| 1543 | tie %{ $$self{cache} }, 'DB_File', $dbfile, O_RDWR | O_CREAT | ||||||||||||||||||||||||||||||
| 1544 | or return Fatal('internal', 'db', undef, | ||||||||||||||||||||||||||||||
| 1545 | "Couldn't create DB cache for " . $self->getDestination, | ||||||||||||||||||||||||||||||
| 1546 | "Message was: " . $!, | ||||||||||||||||||||||||||||||
| 1547 | (-f $dbfile ? "\n(possibly incompatible db format?)" : '')); | ||||||||||||||||||||||||||||||
| 1548 | } | ||||||||||||||||||||||||||||||
| 1549 | return; } | ||||||||||||||||||||||||||||||
| 1550 | |||||||||||||||||||||||||||||||
| 1551 | sub closeCache { | ||||||||||||||||||||||||||||||
| 1552 | my ($self) = @_; | ||||||||||||||||||||||||||||||
| 1553 | if ($$self{cache}) { | ||||||||||||||||||||||||||||||
| 1554 | untie %{ $$self{cache} }; | ||||||||||||||||||||||||||||||
| 1555 | $$self{cache} = undef; } | ||||||||||||||||||||||||||||||
| 1556 | return; } | ||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||
| 1558 | 1; | ||||||||||||||||||||||||||||||
| 1559 | #====================================================================== | ||||||||||||||||||||||||||||||
| 1560 | |||||||||||||||||||||||||||||||
| 1561 | __END__ | ||||||||||||||||||||||||||||||