| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::CrossRef | # | ||
| 3 | # | Scan for ID's etc | # | ||
| 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::CrossRef; | ||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use LaTeXML::Util::Pathname; | ||
| 17 | use LaTeXML::Common::XML; | ||
| 18 | use LaTeXML::Common::Error; | ||
| 19 | use LaTeXML::Post::UnicodeMath; | ||
| 20 | use charnames qw(:full); | ||
| 21 | use LaTeXML::Post; | ||
| 22 | use base qw(LaTeXML::Post::Processor); | ||
| 23 | |||
| 24 | my $NBSP = pack('U', 0xA0); # CONSTANT | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ($class, %options) = @_; | ||
| 28 | my $self = $class->SUPER::new(%options); | ||
| 29 | $$self{db} = $options{db}; | ||
| 30 | $$self{urlstyle} = $options{urlstyle}; | ||
| 31 | ## $$self{toc_show} = ($options{number_sections} ? "typerefnum title" : "title"); | ||
| 32 | # Default format for ltx:ref's within TOC's | ||
| 33 | $$self{toc_show} = 'toctitle'; | ||
| 34 | # Default format for regular ltx:ref's | ||
| 35 | # [BTW: Does number_sections really still make sense?] | ||
| 36 | $$self{ref_show} = ($options{number_sections} ? "refnum" : "title"); | ||
| 37 | $$self{min_ref_length} = (defined $options{min_ref_length} ? $options{min_ref_length} : 1); | ||
| 38 | $$self{ref_join} = (defined $options{ref_join} ? $options{ref_join} : " \x{2023} "); # or " in " or ... ? | ||
| 39 | $$self{navigation_toc} = $options{navigation_toc}; | ||
| 40 | return $self; } | ||
| 41 | |||
| 42 | sub process { | ||
| 43 | my ($self, $doc, $root) = @_; | ||
| 44 | local %LaTeXML::Post::CrossRef::MISSING = (); | ||
| 45 | |||
| 46 | my $navtoc = $$self{navigation_toc}; # If a navigation toc requested, put a toc in nav to fill in | ||
| 47 | my $doctitle = $self->generateDocumentTile($doc); | ||
| 48 | my $nav = $doc->findnode('//ltx:navigation'); | ||
| 49 | if (!$nav && ($navtoc || (defined $doctitle))) { # Need navigation block | ||
| 50 | $doc->addNodes($doc->getDocumentElement, ['ltx:navigation', {}]); | ||
| 51 | $nav = $doc->findnode('//ltx:navigation'); } | ||
| 52 | $doc->addNodes($nav, ['ltx:TOC', { format => $navtoc }]) if $navtoc; | ||
| 53 | $doc->addNodes($nav, ['ltx:title', {}, $doctitle]) if defined $doctitle; | ||
| 54 | |||
| 55 | $self->fill_in_relations($doc); | ||
| 56 | $self->fill_in_tocs($doc); | ||
| 57 | $self->fill_in_frags($doc); | ||
| 58 | $self->fill_in_glossaryrefs($doc); | ||
| 59 | $self->fill_in_refs($doc); | ||
| 60 | $self->fill_in_RDFa_refs($doc); | ||
| 61 | $self->fill_in_bibrefs($doc); | ||
| 62 | $self->fill_in_mathlinks($doc); | ||
| 63 | $self->copy_resources($doc); | ||
| 64 | |||
| 65 | if (keys %LaTeXML::Post::CrossRef::MISSING) { | ||
| 66 | my $tempid = 0; | ||
| 67 | foreach my $severity (qw(error warn info)) { | ||
| 68 | my @msgs = (); | ||
| 69 | foreach my $type (sort keys %{ $LaTeXML::Post::CrossRef::MISSING{$severity} }) { | ||
| 70 | my @items = keys %{ $LaTeXML::Post::CrossRef::MISSING{$severity}{$type} }; | ||
| 71 | $tempid ||= grep { $_ eq 'TEMPORARY_DOCUMENT_ID' } @items; | ||
| 72 | my @args = ('expected', 'ids', undef, | ||
| 73 | "Missing $type: " . join(',', @items), | ||
| 74 | ($tempid ? "[Note TEMPORARY_DOCUMENT_ID is a stand-in ID for the main document.]" : ())); | ||
| 75 | if ($severity eq 'error') { Error(@args); } | ||
| 76 | elsif ($severity eq 'warn') { Warn(@args); } | ||
| 77 | elsif ($severity eq 'info') { Info(@args); } } } } | ||
| 78 | return $doc; } | ||
| 79 | |||
| 80 | sub note_missing { | ||
| 81 | my ($self, $severity, $type, $key) = @_; | ||
| 82 | $LaTeXML::Post::CrossRef::MISSING{$severity}{$type}{$key}++; | ||
| 83 | return; } | ||
| 84 | |||
| 85 | sub fill_in_relations { | ||
| 86 | my ($self, $doc) = @_; | ||
| 87 | my $db = $$self{db}; | ||
| 88 | if (my $id = $doc->getDocumentElement->getAttribute('xml:id')) { | ||
| 89 | if (my $entry = $db->lookup("ID:" . $id)) { | ||
| 90 | # First, add the basic relations | ||
| 91 | my $x; | ||
| 92 | # Apparently, "up", "up up", "up up up" is the desired form for html5 | ||
| 93 | my $xentry = $entry; | ||
| 94 | my $rel = 'up'; | ||
| 95 | while (($x = $xentry->getValue('parent')) && ($xentry = $db->lookup("ID:" . $x))) { | ||
| 96 | if ($xentry->getValue('title')) { # it's interesting if it has a title (INCONSISTENT!!!) | ||
| 97 | ### NOT pageid, like the others, because of the sleasy link to \part in dlmf!!! | ||
| 98 | $doc->addNavigation($rel => $xentry->getValue('id')); | ||
| 99 | $rel .= ' up'; } } | ||
| 100 | if ($xentry && ($id ne $xentry->getValue('pageid'))) { | ||
| 101 | $doc->addNavigation(start => $xentry->getValue('pageid')); } | ||
| 102 | if (my $prev = $self->findPreviousPage($entry)) { # previous page | ||
| 103 | $doc->addNavigation(prev => $prev->getValue('pageid')); } | ||
| 104 | if (my $next = $self->findNextPage($entry)) { | ||
| 105 | $doc->addNavigation(next => $next->getValue('pageid')); } | ||
| 106 | |||
| 107 | # Now, dig around for other interesting related documents | ||
| 108 | # Use the entry types themselves for the relations | ||
| 109 | $xentry = $entry; | ||
| 110 | # Firstly, look at siblings of this page, then at siblings of parent, | ||
| 111 | # then those of grandparent, etc. | ||
| 112 | # In a large/complex site, this gets way too much. But how to prune? | ||
| 113 | while ($xentry = $self->getParentPage($xentry)) { | ||
| 114 | # any siblings of (grand)parent are "interesting" structural elements | ||
| 115 | # OR, even more interesting: the index, bibliography, glossary related to current page! | ||
| 116 | foreach my $sib ($self->getChildPages($xentry)) { | ||
| 117 | my $sib_id = $sib->getValue('pageid'); | ||
| 118 | next if $sib_id eq $id; | ||
| 119 | if ($sib->getValue('primary')) { # If a primary page | ||
| 120 | # Use the element name (w/o prefix) as the relation !!!! | ||
| 121 | my $sib_rel = $sib->getValue('type'); $sib_rel =~ s/^(\w+)://; | ||
| 122 | $doc->addNavigation($sib_rel => $sib_id); } | ||
| 123 | else { # Else, consider it as some sort of sidebar. | ||
| 124 | $doc->addNavigation('sidebar' => $sib_id); } } } | ||
| 125 | # Then Look at (only?) 1st level of pages below this one. | ||
| 126 | foreach my $child ($self->getChildPages($entry)) { | ||
| 127 | my $child_id = $child->getValue('pageid'); | ||
| 128 | if ($child->getValue('primary')) { # If a primary page | ||
| 129 | # Use the element name (w/o prefix) as the relation !!!! | ||
| 130 | my $child_rel = $child->getValue('type'); $child_rel =~ s/^(\w+)://; | ||
| 131 | $doc->addNavigation($child_rel => $child_id); } | ||
| 132 | else { # Else, consider it as some sort of sidebar. | ||
| 133 | $doc->addNavigation('sidebar' => $child_id); } } | ||
| 134 | } } | ||
| 135 | return; } | ||
| 136 | |||
| 137 | sub findPreviousPage { | ||
| 138 | my ($self, $entry) = @_; | ||
| 139 | my $page = $entry->getValue('pageid'); | ||
| 140 | # Look at parent's entry, and get the list of our siblings | ||
| 141 | if (my $pentry = $self->getParentPage($entry)) { | ||
| 142 | my @sibs = $self->getChildPages($pentry); | ||
| 143 | while (@sibs && $sibs[-1]->getValue('pageid') ne $page) { # peel off following sibs | ||
| 144 | pop(@sibs); } | ||
| 145 | return unless @sibs && $sibs[-1]->getValue('pageid') eq $page; # Broken database? | ||
| 146 | pop(@sibs); # Now skip our own entry ($id) | ||
| 147 | @sibs = grep { $_->getValue('primary') } @sibs; | ||
| 148 | # If there IS a preceding sibling, find it's rightmost descendant | ||
| 149 | while (@sibs) { | ||
| 150 | $pentry = $sibs[-1]; | ||
| 151 | @sibs = grep { $_->getValue('primary') } $self->getChildPages($pentry); } | ||
| 152 | return $pentry; } # Return deepest page found | ||
| 153 | return; } | ||
| 154 | |||
| 155 | sub findNextPage { | ||
| 156 | my ($self, $entry) = @_; | ||
| 157 | # Return first child page, if any | ||
| 158 | my @ch = grep { $_->getValue('primary') } $self->getChildPages($entry); | ||
| 159 | return $ch[0] if @ch; | ||
| 160 | my $page = $entry->getValue('pageid'); | ||
| 161 | # Look at parent's entry, and get the list of siblings | ||
| 162 | while ($entry = $self->getParentPage($entry)) { | ||
| 163 | my @sibs = $self->getChildPages($entry); | ||
| 164 | while (@sibs && $sibs[0]->getValue('pageid') ne $page) { # peel off preceding sibs, till found, | ||
| 165 | shift(@sibs); } | ||
| 166 | return unless @sibs && ($sibs[0]->getValue('pageid') eq $page); # Broken database? | ||
| 167 | shift(@sibs); # remove our own entry ($id) | ||
| 168 | @sibs = grep { $_->getValue('primary') } @sibs; # Skip uninteresting pages | ||
| 169 | return $sibs[0] if @sibs; | ||
| 170 | $page = $entry->getValue('pageid'); } | ||
| 171 | return; } | ||
| 172 | |||
| 173 | sub getParentPage { | ||
| 174 | my ($self, $entry) = @_; | ||
| 175 | my $x; | ||
| 176 | return ($x = $entry->getValue('pageid')) && ($x = $$self{db}->lookup("ID:" . $x)) | ||
| 177 | && ($x = $x->getValue('parent')) && ($x = $$self{db}->lookup("ID:" . $x)) | ||
| 178 | && ($x = $x->getValue('pageid')) && ($x = $$self{db}->lookup("ID:" . $x)) | ||
| 179 | && $x; } | ||
| 180 | |||
| 181 | sub getRootPage { | ||
| 182 | my ($self, $entry) = @_; | ||
| 183 | my $x = $entry; | ||
| 184 | my $root = $entry; | ||
| 185 | while (($x = $x->getValue('parent')) && ($x = $$self{db}->lookup("ID:" . $x)) | ||
| 186 | && ($x = $x->getValue('pageid')) && ($x = $$self{db}->lookup("ID:" . $x))) { | ||
| 187 | $root = $x; } | ||
| 188 | return $root; } | ||
| 189 | |||
| 190 | # Assuming this entry is for a page, find the closest descendants that are (distinct) pages | ||
| 191 | sub getChildPages { | ||
| 192 | my ($self, $entry) = @_; | ||
| 193 | my $page = $entry->getValue('pageid'); | ||
| 194 | my @p = (); | ||
| 195 | foreach my $ch (@{ $entry->getValue('children') || [] }) { | ||
| 196 | if (my $e = $$self{db}->lookup("ID:" . $ch)) { | ||
| 197 | if (my $p = $e->getValue('pageid')) { # if valid page | ||
| 198 | push(@p, ($p ne $page ? ($e) : $self->getChildPages($e))); } } } | ||
| 199 | return @p; } | ||
| 200 | |||
| 201 | # this is probably the same as "Interesting" for the above relations. | ||
| 202 | # To make it more extensible, it really should be integrated into the database? | ||
| 203 | # Eg. "sectional" things might mark their entries specially? | ||
| 204 | my $normaltoctypes = { map { ($_ => 1) } # CONSTANT | ||
| 205 | qw (ltx:document ltx:part ltx:chapter | ||
| 206 | ltx:section ltx:subsection ltx:subsubsection | ||
| 207 | ltx:paragraph ltx:subparagraph | ||
| 208 | ltx:index ltx:bibliography ltx:glossary ltx:appendix) }; | ||
| 209 | |||
| 210 | sub fill_in_tocs { | ||
| 211 | my ($self, $doc) = @_; | ||
| 212 | my $n = 0; | ||
| 213 | foreach my $toc ($doc->findnodes('descendant::ltx:TOC[not(ltx:toclist)]')) { | ||
| 214 | $n++; | ||
| 215 | my $selector = $toc->getAttribute('select'); | ||
| 216 | my $types; | ||
| 217 | if ($selector) { | ||
| 218 | $types = { map { ($_ => 1) } split(/\s*\|\s*/, $selector) }; } | ||
| 219 | # global vs children of THIS or Document node? | ||
| 220 | my $id = $doc->getDocumentElement->getAttribute('xml:id'); | ||
| 221 | my $scope = $toc->getAttribute('scope') || 'current'; | ||
| 222 | my $format = $toc->getAttribute('format') || 'normal'; | ||
| 223 | my $lists; | ||
| 224 | if (my $listname = $toc->getAttribute('lists')) { | ||
| 225 | $lists = { map { $_ => 1 } split(/\s/, $listname) }; } | ||
| 226 | else { | ||
| 227 | $lists = { toc => 1 }; } | ||
| 228 | if ($scope eq 'global') { | ||
| 229 | if (my $entry = $$self{db}->lookup("ID:" . $id)) { | ||
| 230 | if (my $root = $self->getRootPage($entry)) { | ||
| 231 | $id = $root->getValue('pageid'); } } } | ||
| 232 | my $show = $toc->getAttribute('show') || $$self{toc_show}; | ||
| 233 | my @list = (); | ||
| 234 | if (!$format || ($format =~ /^normal/)) { | ||
| 235 | @list = $self->gentoc($doc, $id, $show, $lists, $types); } | ||
| 236 | elsif ($format eq 'context') { | ||
| 237 | $lists = { toc => 1 }; | ||
| 238 | @list = $self->gentoc_context($doc, $id, $show, $lists, $types); } | ||
| 239 | $doc->addNodes($toc, ['ltx:toclist', {}, @list]) if @list; } | ||
| 240 | Debug("Filled in $n TOCs") if $LaTeXML::DEBUG{crossref}; | ||
| 241 | return; } | ||
| 242 | |||
| 243 | # generate TOC for $id & its children, | ||
| 244 | # providing that those objects are of appropriate type. | ||
| 245 | # Returns a list of 0 or more ltx:tocentry's (possibly containing ltx:toclist's) | ||
| 246 | # Note that parent/child relationships stored in ObjectDB can also reflect less | ||
| 247 | # `interesting' objects like para or p style paragraphs, and such. | ||
| 248 | sub gentoc { | ||
| 249 | my ($self, $doc, $id, $show, $lists, $types, $localto, $selfid) = @_; | ||
| 250 | if (my $entry = $$self{db}->lookup("ID:$id")) { | ||
| 251 | my @kids = (); | ||
| 252 | if ((!defined $localto) || (($entry->getValue('location') || '') eq $localto)) { | ||
| 253 | @kids = map { $self->gentoc($doc, $_, $show, $lists, $types, $localto, $selfid) } | ||
| 254 | @{ $entry->getValue('children') || [] }; } | ||
| 255 | my $type = $entry->getValue('type'); | ||
| 256 | my $role = $entry->getValue('role'); | ||
| 257 | if (($types ? ($type = $entry->getValue('type')) && $$types{$type} : 1) | ||
| 258 | && inlist_match($lists, $entry->getValue('inlist'))) { | ||
| 259 | return $self->gentocentry($doc, $entry, $selfid, $show, @kids); } | ||
| 260 | else { | ||
| 261 | return @kids; } } | ||
| 262 | else { | ||
| 263 | return (); } } | ||
| 264 | |||
| 265 | sub inlist_match { | ||
| 266 | my ($listsa, $listsb) = @_; | ||
| 267 | return ($listsa && $listsb && grep { $$listsb{$_} } keys %$listsa); } | ||
| 268 | |||
| 269 | # Experimental show pattern: before < filling > after | ||
| 270 | sub gentocentry { | ||
| 271 | my ($self, $doc, $entry, $selfid, $show, @children) = @_; | ||
| 272 | my $id = $entry->getValue('id'); | ||
| 273 | my $type = $entry->getValue('type'); | ||
| 274 | my $typename = $type; $typename =~ s/^ltx://; | ||
| 275 | my ($before, $after); | ||
| 276 | if ($show =~ /^(.*?)\<(.*?)$/) { $before = $1; $show = $2; } | ||
| 277 | if ($show =~ /^(.*?)\>(.*?)$/) { $show = $1; $after = $2; } | ||
| 278 | # Good candidate for before = thumbnail | ||
| 279 | return (['ltx:tocentry', | ||
| 280 | { class => "ltx_tocentry_$typename" | ||
| 281 | . (defined $selfid && ($selfid eq $id) ? ' ltx_ref_self' : "") }, | ||
| 282 | ($before ? $self->generateRef_simple($doc, $id, $before) : ()), | ||
| 283 | ['ltx:ref', { show => $show, idref => $id }], | ||
| 284 | ($after ? $self->generateRef_simple($doc, $id, $after) : ()), | ||
| 285 | (@children ? (['ltx:toclist', { class => "ltx_toclist_$typename" }, @children]) : ())]); } | ||
| 286 | |||
| 287 | # Generate a "context" TOC, that shows what's on the current page, | ||
| 288 | # but also shows the page in the context of it's siblings & ancestors. | ||
| 289 | # This is useful for putting in a navigation bar. | ||
| 290 | sub gentoc_context { | ||
| 291 | my ($self, $doc, $id, $show, $lists, $types) = @_; | ||
| 292 | if (my $entry = $$self{db}->lookup("ID:$id")) { | ||
| 293 | # Generate Downward TOC covering items WITHIN the current page. | ||
| 294 | my @navtoc = $self->gentoc($doc, $id, $show, $lists, $types, $entry->getValue('location') || '', $id); | ||
| 295 | # Then enclose it upwards along with siblings & ancestors | ||
| 296 | my $p_id; | ||
| 297 | while (($p_id = $entry->getValue('parent')) && ($entry = $$self{db}->lookup("ID:$p_id"))) { | ||
| 298 | @navtoc = | ||
| 299 | map { | ||
| 300 | ($_->getValue('id') eq $id | ||
| 301 | ? @navtoc | ||
| 302 | : $self->gentocentry($doc, $_, undef, $show)) } | ||
| 303 | grep { $$normaltoctypes{ $_->getValue('type') } } # or should we use @inlist??? | ||
| 304 | map { $$self{db}->lookup("ID:$_") } | ||
| 305 | @{ $entry->getValue('children') || [] }; | ||
| 306 | if (($types ? $$types{ $entry->getValue('type') } : 1) | ||
| 307 | && $entry->getValue('parent')) { | ||
| 308 | @navtoc = ($self->gentocentry($doc, $entry, undef, $show, @navtoc)); } | ||
| 309 | $id = $p_id; } | ||
| 310 | return @navtoc; } | ||
| 311 | else { | ||
| 312 | return (); } } | ||
| 313 | |||
| 314 | sub fill_in_frags { | ||
| 315 | my ($self, $doc) = @_; | ||
| 316 | my $n = 0; | ||
| 317 | my $db = $$self{db}; | ||
| 318 | # Any nodes with an ID will get a fragid; | ||
| 319 | # This is the id/name that will be used within xhtml/html. | ||
| 320 | foreach my $node ($doc->findnodes('//@xml:id')) { | ||
| 321 | if (my $entry = $db->lookup("ID:" . $node->value)) { | ||
| 322 | if (my $fragid = $entry->getValue('fragid')) { | ||
| 323 | $n++; | ||
| 324 | $node->parentNode->setAttribute(fragid => $fragid); } } } | ||
| 325 | Debug("Filled in fragment $n ids") if $LaTeXML::DEBUG{crossref}; | ||
| 326 | return; } | ||
| 327 | |||
| 328 | # Fill in content text for any <... @idref..>'s or @labelref | ||
| 329 | sub fill_in_refs { | ||
| 330 | my ($self, $doc) = @_; | ||
| 331 | my $db = $$self{db}; | ||
| 332 | my $n = 0; | ||
| 333 | foreach my $ref ($doc->findnodes('descendant::*[@idref or @labelref]')) { | ||
| 334 | my $tag = $doc->getQName($ref); | ||
| 335 | next if $tag eq 'ltx:XMRef'; # Blech; list those TO fill-in, or list those to exclude? | ||
| 336 | my $id = $ref->getAttribute('idref'); | ||
| 337 | my $show = $ref->getAttribute('show'); | ||
| 338 | $show = $$self{ref_show} unless $show; | ||
| 339 | if (!$id) { | ||
| 340 | if (my $label = $ref->getAttribute('labelref')) { | ||
| 341 | my $entry; | ||
| 342 | if (($entry = $db->lookup($label)) && ($id = $entry->getValue('id'))) { | ||
| 343 | $ref->setAttribute(idref => $id); } | ||
| 344 | else { | ||
| 345 | $self->note_missing('warn', 'Target for Label', $label); | ||
| 346 | my $cl = $ref->getAttribute('class'); | ||
| 347 | $ref->setAttribute(class => ($cl ? $cl . ' ltx_missing_label' : 'ltx_missing_label')); | ||
| 348 | if (!$ref->textContent) { | ||
| 349 | $doc->addNodes($ref, $label); # Just to reassure (?) readers. | ||
| 350 | $ref->setAttribute(broken => 1); } | ||
| 351 | } } } | ||
| 352 | |||
| 353 | if ($id) { | ||
| 354 | $n++; | ||
| 355 | if (!$ref->getAttribute('href')) { | ||
| 356 | if (my $url = $self->generateURL($doc, $id)) { | ||
| 357 | $ref->setAttribute(href => $url); } } | ||
| 358 | if (!$ref->getAttribute('title')) { | ||
| 359 | if (my $titlestring = $self->generateTitle($doc, $id, $show || 'toctitle')) { | ||
| 360 | $ref->setAttribute(title => $titlestring); } | ||
| 361 | if (my $rel = $ref->getAttribute('rel')) { | ||
| 362 | if (my $titlestring = $self->generateTitle($doc, $id)) { | ||
| 363 | $ref->setAttribute(fulltitle => $titlestring); } } } | ||
| 364 | if (!$ref->textContent && !element_nodes($ref) | ||
| 365 | && !(($tag eq 'ltx:graphics') || ($tag eq 'ltx:picture'))) { | ||
| 366 | my $is_nameref = ($ref->getAttribute('class') || '') =~ 'ltx_refmacro_nameref'; | ||
| 367 | $doc->addNodes($ref, $self->generateRef($doc, $id, $show, $is_nameref)); } | ||
| 368 | if (my $entry = $$self{db}->lookup("ID:$id")) { | ||
| 369 | $ref->setAttribute(stub => 1) if $entry->getValue('stub'); } | ||
| 370 | } } | ||
| 371 | Debug("Filled in $n refs") if $LaTeXML::DEBUG{crossref}; | ||
| 372 | return; } | ||
| 373 | |||
| 374 | # similar sorta thing for RDF about & resource labels & ids | ||
| 375 | sub fill_in_RDFa_refs { | ||
| 376 | my ($self, $doc) = @_; | ||
| 377 | my $db = $$self{db}; | ||
| 378 | my $n = 0; | ||
| 379 | foreach my $key (qw(about resource)) { | ||
| 380 | foreach my $ref ($doc->findnodes('descendant::*[@' . $key . 'idref or @' . $key . 'labelref]')) { | ||
| 381 | my $id = $ref->getAttribute($key . 'idref'); | ||
| 382 | if (!$id) { | ||
| 383 | if (my $label = $ref->getAttribute($key . 'labelref')) { | ||
| 384 | my $entry; | ||
| 385 | if (($entry = $db->lookup($label)) && ($id = $entry->getValue('id'))) { | ||
| 386 | $ref->setAttribute($key . 'idref' => $id); } | ||
| 387 | else { | ||
| 388 | $self->note_missing('warn', "Target for $key Label", $label); | ||
| 389 | } } } | ||
| 390 | if ($id) { | ||
| 391 | $n++; | ||
| 392 | if (!$ref->getAttribute($key)) { | ||
| 393 | if ($db->lookup("ID:" . $id)) { # RDF "id" need not be real, valid, ids!!! | ||
| 394 | if (my $url = $self->generateURL($doc, $id)) { | ||
| 395 | $ref->setAttribute($key => $url); } } | ||
| 396 | else { | ||
| 397 | $ref->setAttribute($key => '#' . $id); } } | ||
| 398 | } } } | ||
| 399 | set_RDFa_prefixes($doc->getDocument, {}); # what prefixes?? | ||
| 400 | Debug("Filled in $n RDFa refs") if $LaTeXML::DEBUG{crossref}; | ||
| 401 | return; } | ||
| 402 | |||
| 403 | sub fill_in_mathlinks { | ||
| 404 | my ($self, $doc) = @_; | ||
| 405 | my $db = $$self{db}; | ||
| 406 | my $n = 0; | ||
| 407 | foreach my $sym ($doc->findnodes('descendant::*[@decl_id or @meaning]')) { | ||
| 408 | my $tag = $doc->getQName($sym); | ||
| 409 | next if $tag eq 'ltx:XMRef'; # Blech; list those TO fill-in, or list those to exclude? | ||
| 410 | next if $sym->hasAttribute('href'); | ||
| 411 | my $decl_id = $sym->getAttribute('decl_id'); | ||
| 412 | my $meaning = $sym->getAttribute('meaning'); | ||
| 413 | my $entry; | ||
| 414 | if ($decl_id | ||
| 415 | && !$doc->findnodes('ancestor::ltx:glossaryphrase | ancestor::ltx:declare[@type]', $sym)) { | ||
| 416 | $entry = $$self{db}->lookup("DECLARATION:local:$decl_id"); } | ||
| 417 | elsif ($meaning) { | ||
| 418 | $entry = $$self{db}->lookup("DECLARATION:global:$meaning"); } | ||
| 419 | if ($entry) { | ||
| 420 | $n++; | ||
| 421 | ## HACK: DLMF copies $meaning to ltxx:meaning for search indexing | ||
| 422 | ## This should evolve into using (future) mml @mathrole? | ||
| 423 | ## if ($meaning && $$doc{namespaces}{ltxx}) { | ||
| 424 | ## $$node[1]{'ltxx:meaning'} = $meaning; } | ||
| 425 | if (my $id = $self->getIDForDeclaration($entry)) { # Where defined | ||
| 426 | $sym->setAttribute(href => $self->generateURL($doc, $id)); | ||
| 427 | if (my $tag = $entry->getValue('tag:short') || $entry->getValue('description')) { | ||
| 428 | $sym->setAttribute(title => getTextContent($doc, $tag)); } | ||
| 429 | } } } | ||
| 430 | Debug("Filled in $n math links") if $LaTeXML::DEBUG{crossref}; | ||
| 431 | return; } | ||
| 432 | |||
| 433 | # Given a declaration entry (ltx:declare, or ltx:mark or ...) | ||
| 434 | # Return the id of an appropriate link target. | ||
| 435 | # Basically this is the parent, except (DLMF specific?) it should be a table ROW, not CELL | ||
| 436 | # Or the numbered equationgroup, not the unnumbered equation | ||
| 437 | sub getIDForDeclaration { | ||
| 438 | my ($self, $entry) = @_; | ||
| 439 | if (my $pid = $entry && $entry->getValue('parent')) { | ||
| 440 | if (my $pentry = $$self{db}->lookup("ID:$pid")) { | ||
| 441 | my $ptype = $pentry->getValue('type') || ''; | ||
| 442 | # If definition is in a table cell, the correct id will be that of the row | ||
| 443 | if ($ptype eq 'ltx:td') { | ||
| 444 | if (my $gpid = $pentry->getValue('parent')) { | ||
| 445 | return $gpid; } } | ||
| 446 | # If definition is in unnumbered equation within an equation group, use id of the group | ||
| 447 | elsif (($ptype eq 'ltx:equation') && !$pentry->getValue('refnum')) { | ||
| 448 | if (my $gpid = $pentry->getValue('parent')) { | ||
| 449 | if (my $gpentry = $$self{db}->lookup("ID:$gpid")) { | ||
| 450 | my $gptype = $gpentry->getValue('type') || ''; | ||
| 451 | if ($gptype eq 'ltx:equationgroup') { | ||
| 452 | return $gpid; } } } } | ||
| 453 | } | ||
| 454 | return $pid; } } | ||
| 455 | |||
| 456 | sub fill_in_glossaryrefs { | ||
| 457 | my ($self, $doc) = @_; | ||
| 458 | my $n = 0; | ||
| 459 | foreach my $ref ($doc->findnodes('descendant::ltx:glossaryref')) { | ||
| 460 | $n++; | ||
| 461 | my $key = $ref->getAttribute('key'); | ||
| 462 | my $list = $ref->getAttribute('inlist'); | ||
| 463 | my $show = $ref->getAttribute('show') || 'name'; | ||
| 464 | my $entry = $$self{db}->lookup(join(':', 'GLOSSARY', $list, $key)); | ||
| 465 | if ($entry) { | ||
| 466 | my $title = $entry->getValue('phrase:definition'); | ||
| 467 | if (!$ref->getAttribute('title') && $title) { | ||
| 468 | $ref->setAttribute(title => $title->textContent); } | ||
| 469 | if (my $id = $entry->getValue('id')) { | ||
| 470 | $ref->setAttribute(idref => $id); } | ||
| 471 | if (!$ref->textContent && !element_nodes($ref)) { | ||
| 472 | my @stuff = $self->generateGlossaryRefTitle($doc, $entry, $show); | ||
| 473 | if (@stuff) { | ||
| 474 | $doc->addNodes($ref, @stuff); } | ||
| 475 | else { | ||
| 476 | $self->note_missing('warn', "Glossary contents ($show) for key", $key); | ||
| 477 | $doc->addNodes($ref, $key); | ||
| 478 | $doc->addClass($ref, 'ltx_missing'); } } } | ||
| 479 | else { | ||
| 480 | $self->note_missing('warn', "Glossary Entry for key", $key); } | ||
| 481 | if (!$ref->textContent && !element_nodes($ref)) { | ||
| 482 | $doc->addNodes($ref, $key); | ||
| 483 | $doc->addClass($ref, 'ltx_missing'); } } | ||
| 484 | return; } | ||
| 485 | |||
| 486 | # Needs to evolve into the combined stuff that we had in DLMF. | ||
| 487 | # (eg. concise author/year combinations for multiple bibrefs) | ||
| 488 | sub fill_in_bibrefs { | ||
| 489 | my ($self, $doc) = @_; | ||
| 490 | my $n = 0; | ||
| 491 | foreach my $bibref ($doc->findnodes('descendant::ltx:bibref')) { | ||
| 492 | $n++; | ||
| 493 | $doc->replaceNode($bibref, $self->make_bibcite($doc, $bibref)); } | ||
| 494 | Debug("Filled in $n bibrefs") if $LaTeXML::DEBUG{crossref}; | ||
| 495 | return; } | ||
| 496 | |||
| 497 | # Given a list of bibkeys, construct links to them. | ||
| 498 | # Mostly tuned to author-year style. | ||
| 499 | # Combines when multiple bibitems share the same authors. | ||
| 500 | sub make_bibcite { | ||
| 501 | my ($self, $doc, $bibref) = @_; | ||
| 502 | |||
| 503 | my @keys = grep { $_ } split(/,/, $bibref->getAttribute('bibrefs') || ''); | ||
| 504 | my $show = $bibref->getAttribute('show'); | ||
| 505 | my @preformatted = $bibref->childNodes(); | ||
| 506 | if ($show && ($show eq 'none') && !@preformatted) { | ||
| 507 | $show = 'refnum'; } | ||
| 508 | if (!$show) { | ||
| 509 | $show = 'refnum'; } | ||
| 510 | if ($show eq 'nothing') { # Ad Hoc support for \nocite!t | ||
| 511 | return (); } | ||
| 512 | my $sep = $bibref->getAttribute('separator') || ','; | ||
| 513 | my $yysep = $bibref->getAttribute('yyseparator') || ','; | ||
| 514 | my @phrases = element_nodes($bibref); # get the ltx;bibrefphrase's in the bibref! | ||
| 515 | # Collect all the data from the bibliography | ||
| 516 | my @data = (); | ||
| 517 | my @lists = split(/\s+/, $bibref->getAttribute('inlist') || 'bibliography'); | ||
| 518 | foreach my $key (@keys) { | ||
| 519 | my ($bentry, $id, $entry); | ||
| 520 | foreach my $list (@lists) { # Find the first of the lists that contains this bibkey | ||
| 521 | $bentry = $$self{db}->lookup("BIBLABEL:" . $list . ':' . $key); | ||
| 522 | last if $bentry; } | ||
| 523 | if ($bentry | ||
| 524 | && ($id = $bentry->getValue('id')) | ||
| 525 | && ($entry = $$self{db}->lookup("ID:$id"))) { | ||
| 526 | my $authors = $entry->getValue('authors'); | ||
| 527 | my $fauthors = $entry->getValue('fullauthors'); | ||
| 528 | my $keytag = $entry->getValue('keytag'); | ||
| 529 | my $year = $entry->getValue('year'); | ||
| 530 | my $typetag = $entry->getValue('typetag'); | ||
| 531 | my $number = $entry->getValue('number'); | ||
| 532 | my $title = $entry->getValue('title'); | ||
| 533 | my $refnum = $entry->getValue('refnum'); # This come's from the \bibitem, w/o BibTeX | ||
| 534 | my ($rawyear, $suffix); | ||
| 535 | |||
| 536 | my $titlestring = undef; | ||
| 537 | if (defined $title) { | ||
| 538 | $titlestring = $title->textContent; | ||
| 539 | $titlestring =~ s/^\s+//; # Trim leading whitespace | ||
| 540 | $titlestring =~ s/\s+$//; # and trailing | ||
| 541 | $titlestring =~ s/\s+/ /gs; } # and normalize all other whitespace. | ||
| 542 | if ($year && ($year->textContent) =~ /^(\d\d\d\d)(\w)$/) { | ||
| 543 | ($rawyear, $suffix) = ($1, $2); } | ||
| 544 | $show = 'refnum' unless ($show eq 'none') || $authors || $fauthors || $keytag; # Disable author-year format! | ||
| 545 | # fullnames ? | ||
| 546 | push(@data, { | ||
| 547 | key => $key, | ||
| 548 | authors => [$doc->trimChildNodes($authors || $fauthors || $keytag)], | ||
| 549 | fullauthors => [$doc->trimChildNodes($fauthors || $authors || $keytag)], | ||
| 550 | authortext => ($authors || $fauthors ? ($authors || $fauthors)->textContent : ''), | ||
| 551 | year => [$doc->trimChildNodes($year || $typetag)], | ||
| 552 | rawyear => $rawyear, | ||
| 553 | suffix => $suffix, | ||
| 554 | number => [$doc->trimChildNodes($number)], | ||
| 555 | refnum => [$doc->trimChildNodes($refnum)], | ||
| 556 | title => [$doc->trimChildNodes($title || $keytag)], | ||
| 557 | attr => { idref => $id, | ||
| 558 | href => orNull($self->generateURL($doc, $id)), | ||
| 559 | ($titlestring ? (title => $titlestring) : ()) } }); } | ||
| 560 | else { | ||
| 561 | $self->note_missing('warn', 'Entry for citation', $key); | ||
| 562 | push(@data, { key => $key, refnum => [$key], title => [$key], year => [], | ||
| 563 | attr => { idref => $key, title => $key, class => "ltx_missing_citation" } }); | ||
| 564 | } } | ||
| 565 | my $checkdups = ($show =~ /author/i) && ($show =~ /(year|number)/i); | ||
| 566 | my @refs = (); | ||
| 567 | my $saveshow = $show; | ||
| 568 | while (@data) { | ||
| 569 | my $datum = shift(@data); | ||
| 570 | my $didref = 0; | ||
| 571 | my @stuff = (); | ||
| 572 | $show = $saveshow; | ||
| 573 | if (($show eq 'none') && @preformatted) { | ||
| 574 | @stuff = @preformatted; $show = ''; } | ||
| 575 | elsif ($$datum{attr}{class} && ($$datum{attr}{class} eq 'ltx_missing_citation')) { | ||
| 576 | @stuff = (['ltx:ref', $$datum{attr}, $$datum{key}]); | ||
| 577 | $didref = 1; | ||
| 578 | $show = ''; | ||
| 579 | } | ||
| 580 | |||
| 581 | # Add delimeters for parsing... | ||
| 582 | $show =~ s/(\w)year/$1\{\}year/gi; | ||
| 583 | $show =~ s/(\w)phrase/$1\{\}phrase/gi; | ||
| 584 | $show =~ s/phrase(\d)(\w)/phrase$1\{\}$2/gi; | ||
| 585 | while ($show) { | ||
| 586 | if ($show =~ s/^(\w+)//) { | ||
| 587 | my $role = lc($1); $role =~ s/s$//; # remove trailing plural | ||
| 588 | if ($role eq 'author') { | ||
| 589 | push(@stuff, $doc->cloneNodes(@{ $$datum{authors} })); } | ||
| 590 | elsif ($role eq 'fullauthor') { | ||
| 591 | push(@stuff, $doc->cloneNodes(@{ $$datum{fullauthors} })); } | ||
| 592 | elsif ($role eq 'title') { | ||
| 593 | push(@stuff, $doc->cloneNodes(@{ $$datum{title} })); } | ||
| 594 | elsif ($role eq 'refnum') { | ||
| 595 | push(@stuff, $doc->cloneNodes(@{ $$datum{refnum} })); } | ||
| 596 | elsif ($role =~ /^phrase(\d)$/) { | ||
| 597 | # HACK! Avoid empty () from situations where we've set the show (CITE_STYLE) too early | ||
| 598 | # and don't actually have author-year information! | ||
| 599 | my $n = $1; | ||
| 600 | if (($n == 1) && ($show =~ /^\{\}year\{\}phrase2/i) && !scalar(@{ $$datum{year} }) | ||
| 601 | && (!$phrases[0] || (length($phrases[0]->textContent) <= 1)) | ||
| 602 | && (!$phrases[1] || (length($phrases[1]->textContent) <= 1))) { | ||
| 603 | $show =~ s/^\{\}year\{\}phrase2//i; } | ||
| 604 | else { | ||
| 605 | push(@stuff, $phrases[$n - 1]->childNodes) if $phrases[$n - 1]; } } | ||
| 606 | elsif ($role eq 'year') { | ||
| 607 | if (!$$datum{year}) { | ||
| 608 | $self->note_missing('warn', 'Date for citation', $$datum{key}); } | ||
| 609 | elsif (@{ $$datum{year} }) { | ||
| 610 | push(@stuff, ['ltx:ref', $$datum{attr}, @{ $$datum{year} }]); | ||
| 611 | $didref = 1; | ||
| 612 | while ($checkdups && @data && ($$datum{authortext} eq $data[0]{authortext})) { | ||
| 613 | my $next = shift(@data); | ||
| 614 | push(@stuff, $yysep, ' '); | ||
| 615 | if ((($$datum{rawyear} || 'no_year_1') eq ($$next{rawyear} || 'no_year_2')) && $$next{suffix}) { | ||
| 616 | push(@stuff, ['ltx:ref', $$next{attr}, $$next{suffix}]); } | ||
| 617 | else { | ||
| 618 | push(@stuff, ['ltx:ref', $$next{attr}, @{ $$next{year} }]); } } } } | ||
| 619 | elsif ($role eq 'number') { | ||
| 620 | push(@stuff, ['ltx:ref', $$datum{attr}, @{ $$datum{number} }]); | ||
| 621 | $didref = 1; | ||
| 622 | while ($checkdups && @data && ($$datum{authortext} eq $data[0]{authortext})) { | ||
| 623 | my $next = shift(@data); | ||
| 624 | push(@stuff, $yysep, ' ', ['ltx:ref', $$next{attr}, @{ $$next{number} }]); } } | ||
| 625 | elsif ($role eq 'super') { | ||
| 626 | my @r = (); | ||
| 627 | push(@r, ['ltx:ref', $$datum{attr}, @{ $$datum{number} }]); | ||
| 628 | $didref = 1; | ||
| 629 | while ($checkdups && @data && ($$datum{authortext} eq $data[0]{authortext})) { | ||
| 630 | my $next = shift(@data); | ||
| 631 | push(@r, $yysep, ' ', ['ltx:ref', $$next{attr}, @{ $$next{number} }]); } | ||
| 632 | push(@stuff, ['ltx:sup', {}, @r]); } | ||
| 633 | else { | ||
| 634 | Info('unexpected', $role, $doc, "CITE ignoring show key '$role'"); } } | ||
| 635 | elsif ($show =~ s/^\{([^\}]*)\}//) { # pass-thru literal, quoted with {} | ||
| 636 | push(@stuff, $1) if $1; } | ||
| 637 | elsif ($show =~ s/^~//) { # Pass-thru spaces | ||
| 638 | push(@stuff, $NBSP) if @stuff; } | ||
| 639 | elsif ($show =~ s/^(\s+)//) { # Pass-thru spaces | ||
| 640 | push(@stuff, $1) if @stuff; } | ||
| 641 | elsif ($show =~ s/^(\W+)//) { # Pass-thru non show keywords | ||
| 642 | push(@stuff, $1); } } | ||
| 643 | push(@refs, | ||
| 644 | (@refs ? ($sep, ' ') : ()), | ||
| 645 | ($didref ? @stuff : (['ltx:ref', $$datum{attr}, @stuff]))); } | ||
| 646 | return @refs; } | ||
| 647 | |||
| 648 | sub generateURL { | ||
| 649 | my ($self, $doc, $id) = @_; | ||
| 650 | my ($object, $location); | ||
| 651 | if ($object = $$self{db}->lookup("ID:" . $id)) { | ||
| 652 | if ($location = $object->getValue('location')) { | ||
| 653 | my $doclocation = $doc->siteRelativeDestination; | ||
| 654 | my $pathdir = pathname_directory($doclocation); | ||
| 655 | my $url = pathname_relative(($location =~ m|^/| ? $location : '/' . $location), | ||
| 656 | ($pathdir =~ m|^/| ? $pathdir : '/' . $pathdir)); | ||
| 657 | my $extension = $$self{extension} || 'xml'; | ||
| 658 | my $urlstyle = $$self{urlstyle} || 'file'; | ||
| 659 | if ($urlstyle eq 'server') { | ||
| 660 | # Remove trailing index.$extension but be careful not to leave url empty! (then it's "self") | ||
| 661 | $url =~ s/(^|\/)index.\Q$extension\E$/($1 ? $1 : '.\/')/e; } | ||
| 662 | elsif ($urlstyle eq 'negotiated') { | ||
| 663 | $url =~ s/\.\Q$extension\E$//; # Remove trailing $extension | ||
| 664 | $url =~ s/(^|\/)index$/$1/; # AND trailing index | ||
| 665 | } | ||
| 666 | $url = '.' unless $url; | ||
| 667 | if (my $fragid = $object->getValue('fragid')) { | ||
| 668 | $url = '' if ($url eq '.') or ($location eq $doclocation); | ||
| 669 | $url .= '#' . $fragid; } | ||
| 670 | elsif ($location eq $doclocation) { | ||
| 671 | $url = ''; } | ||
| 672 | return pathname_to_url($url); } | ||
| 673 | else { | ||
| 674 | $self->note_missing('warn', 'File location for ID', $id); } } | ||
| 675 | else { | ||
| 676 | $self->note_missing('warn', 'DB Entry for ID', $id); } | ||
| 677 | return; } | ||
| 678 | |||
| 679 | # Generate the contents of a <ltx:ref> of the given id. | ||
| 680 | # show is a string containing substrings 'type', 'refnum' and 'title' | ||
| 681 | # (standing for the type prefix, refnum and title of the id'd object) | ||
| 682 | # and any other random characters; the | ||
| 683 | sub generateRef { | ||
| 684 | my ($self, $doc, $reqid, $reqshow, $is_nameref) = @_; | ||
| 685 | my $pending = ''; | ||
| 686 | my @stuff; | ||
| 687 | # Try the requested show pattern, and if it fails, try a fallback of just the title or refnum | ||
| 688 | foreach my $show (($reqshow, ($reqshow !~ /title/ ? "title" : "refnum"))) { | ||
| 689 | my $id = $reqid; | ||
| 690 | # Start with requested ID, add some from parent(s), if needed/until to make "useful" link content | ||
| 691 | while (my $entry = $id && $$self{db}->lookup("ID:$id")) { | ||
| 692 | if (my @s = $self->generateRef_aux($doc, $entry, $show, $is_nameref)) { | ||
| 693 | push(@stuff, $pending) if $pending; | ||
| 694 | push(@stuff, @s); | ||
| 695 | return @stuff if $self->checkRefContent($doc, @stuff); | ||
| 696 | $pending = $$self{ref_join}; } # inside/outside this brace determines if text can START with the join. | ||
| 697 | $id = $entry->getValue('parent'); } } | ||
| 698 | if (!@stuff) { # Try first child for a title-less document? | ||
| 699 | if (my $entry = $$self{db}->lookup("ID:$reqid")) { | ||
| 700 | if (($entry->getValue('type') || '') eq 'ltx:document') { | ||
| 701 | foreach my $c (@{ $entry->getValue('children') }) { | ||
| 702 | if (my $centry = $$self{db}->lookup("ID:$c")) { | ||
| 703 | if (my @s = $self->generateRef_aux($doc, $centry, $reqshow, $is_nameref)) { | ||
| 704 | push(@stuff, @s); last; } } } } } } | ||
| 705 | if (@stuff) { | ||
| 706 | return @stuff; } | ||
| 707 | else { | ||
| 708 | $self->note_missing('info', 'Usable title for ID', $reqid); | ||
| 709 | return ($reqid); } } # id is crummy, but better than "?"... or? | ||
| 710 | |||
| 711 | # Just return the reqshow value for $reqid, or nothing | ||
| 712 | sub generateRef_simple { | ||
| 713 | my ($self, $doc, $reqid, $reqshow) = @_; | ||
| 714 | my $pending = ''; | ||
| 715 | my @stuff; | ||
| 716 | if (my $entry = $reqshow && $reqid && $$self{db}->lookup("ID:$reqid")) { | ||
| 717 | return $self->generateRef_aux($doc, $entry, $reqshow); } | ||
| 718 | return (); } | ||
| 719 | |||
| 720 | # Check if the proposed content of a <ltx:ref> is "Good Enough" | ||
| 721 | # (long enough, unique enough to give reader feedback,...) | ||
| 722 | sub checkRefContent { | ||
| 723 | my ($self, $doc, @stuff) = @_; | ||
| 724 | # Length? having _some_ actual text ? | ||
| 725 | my $s = text_content(@stuff); | ||
| 726 | # Could compare a minum length | ||
| 727 | # But perhaps this is better: check that there's some "text", not just symbols! | ||
| 728 | $s =~ s/\bin\s+//g; | ||
| 729 | return ($s =~ /\w/ ? 1 : 0); } | ||
| 730 | |||
| 731 | sub text_content { | ||
| 732 | my (@stuff) = @_; | ||
| 733 | return join('', map { text_content_aux($_) } @stuff); } | ||
| 734 | |||
| 735 | sub text_content_aux { | ||
| 736 | my ($n) = @_; | ||
| 737 | my $r = ref $n; | ||
| 738 | if (!$r) { | ||
| 739 | return $n; } | ||
| 740 | elsif ($r eq 'ARRAY') { | ||
| 741 | my ($t, $a, @c) = @$n; | ||
| 742 | return text_content(@c); } | ||
| 743 | elsif ($r =~ /^XML::/) { | ||
| 744 | return $n->textContent; } | ||
| 745 | else { | ||
| 746 | return $n; } } | ||
| 747 | |||
| 748 | my %ref_fallbacks = ( # Alternative fields, when not found | ||
| 749 | typerefnum => [qw(refnum)], | ||
| 750 | toctitle => [qw(title toccaption)], | ||
| 751 | title => [qw(toccaption)], | ||
| 752 | rawtoctitle => [qw(toctitle title toccaption)], | ||
| 753 | rawtitle => [qw(title toccaption)], | ||
| 754 | ); | ||
| 755 | |||
| 756 | # Generate text to fill in an ltx:ref from a database entry for some object. | ||
| 757 | # The show pattern indicates what data to use; usually a single keyword | ||
| 758 | # (or keywords separated by spaces, ~ or {} enclosed literal text) | ||
| 759 | # The keywords are things like refnum, title, caption, etc | ||
| 760 | # (possibly coming from ltx:tag or other data; see Scan) | ||
| 761 | sub generateRef_aux { | ||
| 762 | my ($self, $doc, $entry, $show, $is_nameref) = @_; | ||
| 763 | my @stuff = (); | ||
| 764 | my $OK = 0; | ||
| 765 | while ($show) { | ||
| 766 | if ($show =~ s/^(\w+)//) { # peel off next keyword | ||
| 767 | my $key = lc($1); | ||
| 768 | my $class = ($key =~ /title/ ? 'ltx_ref_title' : 'ltx_ref_tag'); | ||
| 769 | my @keys = ($key, 'tag:' . $key, | ||
| 770 | ($ref_fallbacks{$key} ? @{ $ref_fallbacks{$key} } : ())); | ||
| 771 | my $value; | ||
| 772 | foreach my $k (@keys) { # lookup the data for that keyword (or an alternative) | ||
| 773 | $value = $entry->getValue($k); | ||
| 774 | last if $value; } | ||
| 775 | if ($value) { | ||
| 776 | $OK = 1; | ||
| 777 | if ($is_nameref) { | ||
| 778 | # yank out the tag if this is nameref | ||
| 779 | my ($first_child) = element_nodes($value); | ||
| 780 | $first_child->unbindNode if $first_child && ($doc->getQName($first_child) eq 'ltx:tag'); } | ||
| 781 | push(@stuff, ['ltx:text', { class => $class }, $self->prepRefText($doc, $value)]); } } | ||
| 782 | elsif ($show =~ s/^\{([^\}]*)\}//) { # pass-thru literal, quoted with {} | ||
| 783 | push(@stuff, $1) if $1; } | ||
| 784 | elsif ($show =~ s/^~//) { # Pass-thru spaces | ||
| 785 | push(@stuff, $NBSP) if @stuff; } | ||
| 786 | elsif ($show =~ s/^(\s+)//) { # Pass-thru spaces | ||
| 787 | push(@stuff, $1) if @stuff; } | ||
| 788 | elsif ($show =~ s/^(\W+)//) { # Pass-thru non show keywords | ||
| 789 | push(@stuff, $1); } } | ||
| 790 | # Maybe nothing found for this entry (probably retry on parent?) | ||
| 791 | return ($OK ? @stuff : ()); } | ||
| 792 | |||
| 793 | sub prepRefText { | ||
| 794 | my ($self, $doc, $title) = @_; | ||
| 795 | return $doc->cloneNodes($doc->trimChildNodes($self->fillInTitle($doc, $title))); } | ||
| 796 | |||
| 797 | sub prepRawRefText { | ||
| 798 | my ($self, $doc, $title) = @_; | ||
| 799 | my $node = $self->prepRefText($doc, $title); | ||
| 800 | if ($doc->getQName($node) =~ /^ltx:(?:toc)title$/) { # Trim tags from titles | ||
| 801 | my ($first) = element_nodes($node); | ||
| 802 | if ($first && ($doc->getQName($first) eq 'ltx:tag')) { | ||
| 803 | $node->removeChild($first); } } | ||
| 804 | return $node; } | ||
| 805 | |||
| 806 | # Generate a title for this document. | ||
| 807 | sub generateDocumentTile { | ||
| 808 | my ($self, $doc) = @_; | ||
| 809 | my $title; | ||
| 810 | if (my $docid = $doc->getDocumentElement->getAttribute('xml:id')) { # If has a id? | ||
| 811 | $title = $self->generateTitle($doc, $docid); } | ||
| 812 | return $title if (defined $title) && ($title ne ''); | ||
| 813 | if (my $node = $doc->findnode('//ltx:title | //ltx:toctitle | //ltx:caption | //ltx:toccaption')) { | ||
| 814 | $title = getTextContent($doc, $node); } | ||
| 815 | return $title if (defined $title) && ($title ne ''); | ||
| 816 | return; } | ||
| 817 | |||
| 818 | # Generate a title string for ltx:ref | ||
| 819 | sub generateTitle { | ||
| 820 | my ($self, $doc, $id, $shown) = @_; | ||
| 821 | my @ids = (); | ||
| 822 | my $string = ""; | ||
| 823 | my $prefix; | ||
| 824 | $shown = '' unless $shown; | ||
| 825 | while (my $entry = $id && $$self{db}->lookup("ID:$id")) { | ||
| 826 | push(@ids, $id); | ||
| 827 | my ($type, $x, $t); | ||
| 828 | my $dup; | ||
| 829 | my @pieces = (); | ||
| 830 | # Attempt to construct a meaningful title (or type-refnum or refnum) for this level | ||
| 831 | # noting whether it apparently duplicates the ref's content. | ||
| 832 | if ($x = $entry->getValue('title')) { | ||
| 833 | $dup = $shown =~ /title/; | ||
| 834 | push(@pieces, $x); } | ||
| 835 | elsif (($t = $entry->getValue('tag:creftypecap') || $entry->getValue('tag:creftype')) | ||
| 836 | && ($x = $entry->getValue('refnum'))) { | ||
| 837 | $dup = ($shown =~ /type/) && ($shown =~ /refnum/); | ||
| 838 | push(@pieces, $t, $x); } | ||
| 839 | elsif ($x = $entry->getValue('typerefnum')) { | ||
| 840 | $dup = ($shown =~ /type/) && ($shown =~ /refnum/); | ||
| 841 | push(@pieces, $x); } | ||
| 842 | elsif ($x = $entry->getValue('refnum')) { | ||
| 843 | $dup = ($shown =~ /refnum/); | ||
| 844 | push(@pieces, $x); } | ||
| 845 | |||
| 846 | if ($dup) { # Omit if the title from the 1st node duplicates the content | ||
| 847 | $prefix = "In "; $shown = ""; } | ||
| 848 | elsif (my $title = join(' ', map { getTextContent($doc, $self->fillInTitle($doc, $_)); } @pieces)) { | ||
| 849 | $string .= $prefix if $prefix; | ||
| 850 | $prefix = $$self{ref_join}; | ||
| 851 | $string .= $title; } | ||
| 852 | $id = $entry->getValue('parent'); } # Loop to add context to title | ||
| 853 | return $string; } | ||
| 854 | |||
| 855 | sub getTextContent { | ||
| 856 | my ($doc, $title) = @_; | ||
| 857 | $title = getTextContent_rec($doc, $title) if $title && ref $title; | ||
| 858 | $title =~ s/^\s+//s if $title; # Trim leading whitespace | ||
| 859 | $title =~ s/\s+$//s if $title; # and trailing | ||
| 860 | $title =~ s/\s+/ /gs if $title; # and normalize all other whitespace. | ||
| 861 | return $title; } | ||
| 862 | |||
| 863 | sub getTextContent_rec { | ||
| 864 | my ($doc, $node) = @_; | ||
| 865 | my $type = $node->nodeType; | ||
| 866 | if ($type == XML_TEXT_NODE) { | ||
| 867 | return $node->textContent; } | ||
| 868 | elsif ($type == XML_ELEMENT_NODE) { | ||
| 869 | my $tag = $doc->getQName($node); | ||
| 870 | if ($tag eq 'ltx:tag') { | ||
| 871 | return ($node->getAttribute('open') || '') | ||
| 872 | . $node->textContent # assuming no nested ltx:tag | ||
| 873 | . ($node->getAttribute('close') || ''); } | ||
| 874 | elsif ($tag eq 'ltx:Math') { | ||
| 875 | return unicodemath($doc, $node); } | ||
| 876 | else { | ||
| 877 | return join('', map { getTextContent_rec($doc, $_); } $node->childNodes); } } | ||
| 878 | elsif ($type == XML_DOCUMENT_FRAG_NODE) { | ||
| 879 | return join('', map { getTextContent_rec($doc, $_); } $node->childNodes); } | ||
| 880 | else { | ||
| 881 | return ''; } } | ||
| 882 | |||
| 883 | # Fill in any embedded ltx:ref's & ltx:cite's within a title | ||
| 884 | sub fillInTitle { | ||
| 885 | my ($self, $doc, $title) = @_; | ||
| 886 | return $title unless $title && ref $title; | ||
| 887 | # Fill in any nested ref's! | ||
| 888 | foreach my $ref ($doc->findnodes('descendant::ltx:ref[@idref or @labelref]', $title)) { | ||
| 889 | next if $ref->textContent; | ||
| 890 | my $show = $ref->getAttribute('show'); | ||
| 891 | $show = $$self{ref_show} unless $show; | ||
| 892 | my $refentry; | ||
| 893 | if (my $id = $ref->getAttribute('idref')) { | ||
| 894 | $refentry = $$self{db}->lookup("ID:$id"); } | ||
| 895 | elsif (my $label = $ref->getAttribute('labelref')) { | ||
| 896 | $refentry = $$self{db}->lookup($label); | ||
| 897 | if ($id = $refentry->getValue('id')) { | ||
| 898 | $refentry = $$self{db}->lookup("ID:$id"); } } | ||
| 899 | if ($refentry) { | ||
| 900 | $doc->replaceNode($ref, $self->generateRef_aux($doc, $refentry, $show)); } } | ||
| 901 | # Fill in (replace, actually) any embedded citations. | ||
| 902 | foreach my $bibref ($doc->findnodes('descendant::ltx:bibref', $title)) { | ||
| 903 | $doc->replaceNode($bibref, $self->make_bibcite($doc, $bibref)); } | ||
| 904 | foreach my $break ($doc->findnodes('descendant::ltx:break', $title)) { | ||
| 905 | $doc->replaceNode($break, ['ltx:text', {}, " "]); } | ||
| 906 | return $title; } | ||
| 907 | |||
| 908 | sub generateGlossaryRefTitle { | ||
| 909 | my ($self, $doc, $entry, $show) = @_; | ||
| 910 | my $phrases = $entry->getValue('phrases'); | ||
| 911 | my @stuff = (); | ||
| 912 | if (my $phrase = $entry->getValue('phrase:' . $show)) { | ||
| 913 | push(@stuff, ['ltx:text', { class => 'ltx_glossary_' . $show }, | ||
| 914 | $self->prepRefText($doc, $phrase)]); } | ||
| 915 | elsif ($show =~ /^(\w+)-plural$/) { | ||
| 916 | my $sh = $1; | ||
| 917 | if (my $phrase = $entry->getValue('phrase:' . $sh)) { | ||
| 918 | push(@stuff, ['ltx:text', { class => 'ltx_glossary_' . $show }, | ||
| 919 | $self->prepRefText($doc, $phrase), 's']); } } | ||
| 920 | elsif ($show =~ /^(\w+)-indefinite$/) { | ||
| 921 | my $sh = $1; | ||
| 922 | if (my $phrase = $entry->getValue('phrase:' . $sh)) { | ||
| 923 | my $s = $phrase->textContent; | ||
| 924 | my $art = ($s =~ /^[aeiou]/i ? 'an ' : 'a '); | ||
| 925 | push(@stuff, ['ltx:text', { class => 'ltx_glossary_' . $show }, | ||
| 926 | $art, $self->prepRefText($doc, $phrase)]); } } | ||
| 927 | return @stuff; } | ||
| 928 | |||
| 929 | sub orNull { | ||
| 930 | return (grep { defined } @_) ? @_ : undef; } | ||
| 931 | |||
| 932 | # Possibly this needs support from Scan, as well? | ||
| 933 | # to manage resources, record in Manifest, something like that? | ||
| 934 | sub copy_resources { | ||
| 935 | my ($self, $doc) = @_; | ||
| 936 | # Copy any "resources" linked from the document | ||
| 937 | my $paths = [$doc->getSearchPaths]; | ||
| 938 | foreach my $n ($doc->findnodes('//ltx:ref[@href and not(@idref) and not(@labelref)]')) { | ||
| 939 | my $url = $n->getAttribute('href'); | ||
| 940 | if ($url !~ /^(\w+:|\/)/) { # relative path? (No explicit protocol, or absolute) | ||
| 941 | if (my $src = pathname_find($url, paths => $paths)) { # AND if file exists there. | ||
| 942 | my $dst = $doc->checkDestination($url); | ||
| 943 | pathname_copy($src, $dst); | ||
| 944 | } } } | ||
| 945 | return; } | ||
| 946 | |||
| 947 | # ================================================================================ | ||
| 948 | 1; |