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