lib/LaTeXML/Post/Scan.pm untested

filter: Statementstmt 0.0% 0 / 383 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 121 MC/DCmcdc 0.0% 0 / 76 Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 771 CC 62 SCAR 38.5
CC
62
Cov
0%
process
210.0
inPageID
72.0
declare_handler
56.0
CRAP 47.1
1 # /=====================================================================\ #
2 # | LaTeXML::Post::Scan | #
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 package LaTeXML::Post::Scan;
13 use strict;
14 use warnings;
15 use LaTeXML::Util::Pathname;
16 use LaTeXML::Common::XML;
17 use LaTeXML::Post;
18 use base qw(LaTeXML::Post::Processor);
19  
20 # NOTE: This module is one that probably needs a lot of customizability.
21 sub new {
22 my ($class, %options) = @_;
23 my $self = $class->SUPER::new(%options);
24 $$self{db} = $options{db};
25 $$self{handlers} = {};
26 $self->registerHandler('ltx:document' => \&section_handler);
27 $self->registerHandler('ltx:part' => \&section_handler);
28 $self->registerHandler('ltx:chapter' => \&section_handler);
29 $self->registerHandler('ltx:section' => \&section_handler);
30 $self->registerHandler('ltx:appendix' => \&section_handler);
31 $self->registerHandler('ltx:subsection' => \&section_handler);
32 $self->registerHandler('ltx:subsubsection' => \&section_handler);
33 $self->registerHandler('ltx:paragraph' => \&section_handler);
34 $self->registerHandler('ltx:subparagraph' => \&section_handler);
35 $self->registerHandler('ltx:bibliography' => \&section_handler);
36 $self->registerHandler('ltx:index' => \&section_handler);
37 $self->registerHandler('ltx:glossary' => \&section_handler);
38  
39 $self->registerHandler('ltx:table' => \&captioned_handler);
40 $self->registerHandler('ltx:figure' => \&captioned_handler);
41 $self->registerHandler('ltx:float' => \&captioned_handler);
42 $self->registerHandler('ltx:listing' => \&captioned_handler);
43 $self->registerHandler('ltx:theorem' => \&section_handler);
44 $self->registerHandler('ltx:proof' => \&section_handler);
45  
46 $self->registerHandler('ltx:equation' => \&labelled_handler);
47 $self->registerHandler('ltx:equationgroup' => \&labelled_handler);
48 $self->registerHandler('ltx:item' => \&labelled_handler);
49 $self->registerHandler('ltx:listingline' => \&labelled_handler);
50 $self->registerHandler('ltx:anchor' => \&anchor_handler);
51 $self->registerHandler('ltx:note' => \&note_handler);
52  
53 $self->registerHandler('ltx:bibitem' => \&bibitem_handler);
54 $self->registerHandler('ltx:bibentry' => \&bibentry_handler);
55 $self->registerHandler('ltx:indexmark' => \&indexmark_handler);
56 $self->registerHandler('ltx:glossaryentry' => \&glossaryentry_handler);
57 $self->registerHandler('ltx:glossarydefinition' => \&glossaryentry_handler);
58 $self->registerHandler('ltx:ref' => \&ref_handler);
59 $self->registerHandler('ltx:bibref' => \&bibref_handler);
60 $self->registerHandler('ltx:glossaryref' => \&glossaryref_handler);
61  
62 $self->registerHandler('ltx:navigation' => \&navigation_handler);
63 $self->registerHandler('ltx:rdf' => \&rdf_handler);
64 $self->registerHandler('ltx:declare' => \&declare_handler);
65  
66 $self->registerHandler('ltx:rawhtml' => \&rawhtml_handler);
67  
68 return $self; }
69  
70 sub registerHandler {
71 my ($self, $tag, $handler) = @_;
72 $$self{handlers}{$tag} = $handler;
73 return; }
74  
75 sub process {
76 my ($self, $doc, $root) = @_;
77 # I think we really need an ID here to establish the root node in the DB,
78 # even if the document didn't have one originally.
79 # And for the common case of a single docucment, we'd like to be silent about it,
80 # UNLESS there seem to be multiple documents which would lead to a conflict.
81 my $id = $root->getAttribute('xml:id');
82 if (!defined $id) {
83 $id = "Document";
84 if (my $preventry = $$self{db}->lookup("ID:$id")) {
85 if (my $loc = $doc->siteRelativeDestination) {
86 my $prevloc = $preventry->getValue('location');
87 if ((defined $prevloc) && ($loc ne $prevloc)) {
88 Warn('unexpected', 'location', undef,
89 "Using default ID='$id', "
90 . "but there's an apparent conflict with location '$loc' and previous '$prevloc'"); } } }
91 $root->setAttribute('xml:id' => $id); }
92  
93 # By default, 1st document processed is considered the root of the site
94 my $siteentry = $$self{db}->lookup('SITE_ROOT');
95 if (!$siteentry) {
96 $siteentry = $$self{db}->register('SITE_ROOT', id => $id); }
97 my $siteid = $siteentry->getValue('id');
98  
99 $self->scan($doc, $root, $$doc{parent_id});
100  
101 # Set up interconnections on multidocument site.
102 $$self{db}->register("DOCUMENT:" . ($doc->siteRelativeDestination || ''), id => $id);
103  
104 # Question: If (on multidoc sites) a doc contains a single node (say ltx:chapter)
105 # might it make sense to treat the doc as ONLY that node?
106 # Alternative: May be necessary to extract title from that child?
107  
108 # Find a plausible parent doc, unless this is the root, or already has one
109 # Either by relative id's, destination location, or default to the site itself.
110 my $entry = $$self{db}->lookup("ID:$id");
111 if (($id ne $siteid) && !$entry->getValue('parent')) {
112 my $parent_id;
113 if (!$parent_id) { # Look for parent assuming it's id is component of $id
114 my $upid = $id;
115 while ($upid =~ s/\.[^\.]+$//) {
116 if ($$self{db}->lookup("ID:$upid")) {
117 $parent_id = $upid; last; } } }
118 if (!$parent_id) { # Look for parent as index.xml in a containing directory.
119 my $loc = $entry->getValue('location');
120 my $dir = $loc;
121 while (($dir) = pathname_split($dir)) {
122 if (my $pentry = $$self{db}->lookup("DOCUMENT:" . pathname_concat($dir, 'index.xml'))) {
123 my $pid = $pentry->getValue('id');
124 if ($pid && ($pid ne $id)) {
125 $parent_id = $pid; last; } } } }
126 if (!$parent_id) { # Else default to the id of the site itself.
127 $parent_id = $siteid; }
128 if ($parent_id && ($parent_id ne $id)) {
129 $entry->setValues(parent => $parent_id);
130 # Children are added in the order that they were scanned
131 $self->addAsChild($id, $parent_id); }
132 else {
133 Info('expected', 'parent', undef, "No parent document found for '$id'"); } }
134 NoteLog("Scan: DBStatus: " . $$self{db}->status);
135 return $doc; }
136  
137 sub scan {
138 my ($self, $doc, $node, $parent_id) = @_;
139 no warnings 'recursion';
140 my $tag = $doc->getQName($node);
141 my $handler = $$self{handlers}{$tag} || \&default_handler;
142 &$handler($self, $doc, $node, $tag, $parent_id);
143 return; }
144  
145 sub scanChildren {
146 my ($self, $doc, $node, $parent_id) = @_;
147 no warnings 'recursion';
148 foreach my $child ($node->childNodes) {
149 if ($child->nodeType == XML_ELEMENT_NODE) {
150 $self->scan($doc, $child, $parent_id); } }
151 return; }
152  
153 sub addAsChild {
154 my ($self, $id, $parent_id) = @_;
155 # Find the ancestor that maintains a children list
156 while (my $parent = $parent_id && $$self{db}->lookup("ID:$parent_id")) {
157 if ($parent->hasValue('children')) {
158 $parent->pushNew('children', $id);
159 last; }
160 else {
161 $parent_id = $parent->getValue('parent'); } }
162 return; }
163  
164 sub pageID {
165 my ($self, $doc) = @_;
166 return $doc->getDocumentElement->getAttribute('xml:id'); }
167  
168 # Compute a "Fragment ID", ie. an ID based on the given ID,
169 # but which is potentially shortened so that it need only be
170 # unique within the given page.
171 sub inPageID {
172 my ($self, $doc, $node) = @_;
173 my $id = $node->getAttribute('xml:id');
174 my $baseid = $doc->getDocumentElement->getAttribute('xml:id') || '';
175 # And we're using label-based ids in the target document...
176 if ($$self{labelids}) {
177 if (my $labels = $node->getAttribute('labels')) {
178 my ($l) = split(' ', $labels);
179 $l =~ s/^LABEL://;
180 $id = $l;
181 if (my $baselabels = $doc->getDocumentElement->getAttribute('labels')) {
182 my ($bl) = split(' ', $baselabels);
183 $bl =~ s/^LABEL://;
184 $baseid = $bl; } } }
185 if (!$id) {
186 return $id; }
187 elsif ($baseid eq $id) {
188 return; }
189 elsif ($baseid && ($id =~ /^\Q$baseid\E\.(.*)$/)) {
190 return $1; }
191 elsif ($$doc{split_from_id} && ($id =~ /^\Q$$doc{split_from_id}\E\.(.*)$/)) {
192 return $1; }
193 else {
194 return $id; } }
195  
196 sub noteLabels {
197 my ($self, $node) = @_;
198 if (my $id = $node->getAttribute('xml:id')) {
199 if (my $labels = $node->getAttribute('labels')) {
200 my @labels = split(' ', $node->getAttribute('labels'));
201 foreach my $label (@labels) {
202 $$self{db}->register($label, id => orNull($id)); }
203 return [@labels]; } }
204 return; }
205  
206 # Clean up a node before insertion into database.
207 sub cleanNode {
208 my ($self, $doc, $node) = @_;
209 return $node unless $node;
210 # Clone the node, and get the ID's unique (at least) within the originating document
211 my $cleaned = $doc->cloneNode($node);
212 # Remove indexmark (anything else ?)
213 map { $_->parentNode->removeChild($_) } $doc->findnodes('.//ltx:indexmark', $cleaned);
214 return $cleaned; }
215  
216 # Assumes $node has been cloned, if needed.
217 # Set to something smallish (eg. 6) to forcibly truncate toctitle/toccaption
218 our $TOCTEXT_MAX_LENGTH = undef;
219  
220 sub truncateNode {
221 my ($self, $doc, $node) = @_;
222 return $node if !$node || !defined $TOCTEXT_MAX_LENGTH;
223 my @children = $node->childNodes;
224 my $n = $TOCTEXT_MAX_LENGTH;
225 my $trunc = 0;
226 while ($n && @children) {
227 my $c = shift(@children);
228 if ($c->nodeType == XML_TEXT_NODE) {
229 my $s = $c->textContent;
230 my @w = split(/\s/, $s);
231 if (scalar(@w) > $n) {
232 $c->setData(join(' ', @w[0 .. $n]));
233 $trunc = 1; $n = 0; }
234 else {
235 $n--; } }
236 else {
237 $n--; } }
238 if ($trunc || (scalar(@children) > 1)) {
239 map { $node->removeChild($_) } @children; # Remove any remaining children.
240 $node->appendText("\x{2026}"); }
241 return $node; }
242  
243 sub addCommon {
244 my ($self, $doc, $node, $tag, $parent_id) = @_;
245 my $id = $node->getAttribute('xml:id');
246 my $inlist;
247 if (my $listnames = $node->getAttribute('inlist')) {
248 $inlist = { map { ($_ => 1) } split(/\s/, $listnames) }; }
249 my %props = (
250 id => orNull($id),
251 type => orNull($tag),
252 parent => orNull($parent_id),
253 labels => orNull($self->noteLabels($node)),
254 location => orNull($doc->siteRelativeDestination),
255 pageid => orNull($self->pageID($doc)),
256 fragid => orNull($self->inPageID($doc, $node)),
257 inlist => $inlist,
258 );
259 # Figure out sane, safe naming?
260 foreach my $tagnode ($doc->findnodes('ltx:tags/ltx:tag', $node)) {
261 my $key;
262 if (my $role = $tagnode->getAttribute('role')) {
263 if ($role =~ /.*refnum$/) {
264 $key = $role; }
265 else {
266 $key = 'tag:' . $role; } }
267 else {
268 $key = 'refnum'; }
269 $props{$key} = $self->cleanNode($doc, $tagnode); }
270 return %props; }
271  
272 sub default_handler {
273 my ($self, $doc, $node, $tag, $parent_id) = @_;
274 no warnings 'recursion';
275 my $id = $node->getAttribute('xml:id');
276 if ($id) {
277 $$self{db}->register("ID:$id",
278 $self->addCommon($doc, $node, $tag, $parent_id));
279 $self->addAsChild($id, $parent_id); }
280 $self->scanChildren($doc, $node, $id || $parent_id);
281 return; }
282  
283 sub section_handler {
284 my ($self, $doc, $node, $tag, $parent_id) = @_;
285 my $id = $node->getAttribute('xml:id');
286 if ($id) {
287 $$self{db}->register("ID:$id",
288 $self->addCommon($doc, $node, $tag, $parent_id),
289 primary => 1,
290 title => orNull($self->cleanNode($doc, $doc->findnode('ltx:title', $node))),
291 toctitle => orNull($self->cleanNode($doc, $doc->findnode('ltx:toctitle', $node))),
292 children => [],
293 stub => orNull($node->getAttribute('stub')));
294 $self->addAsChild($id, $parent_id); }
295 $self->scanChildren($doc, $node, $id || $parent_id);
296 return; }
297  
298 sub captioned_handler {
299 my ($self, $doc, $node, $tag, $parent_id) = @_;
300 my $id = $node->getAttribute('xml:id');
301 if ($id) {
302 # We're actually trying to find the shallowest caption
303 # Not one nested in another figure/table/float/whoknowswhat !
304 my ($caption) = ($doc->findnode('child::ltx:caption', $node),
305 $doc->findnode('descendant::ltx:caption', $node));
306 my ($toccaption) = ($doc->findnode('child::ltx:toccaption', $node),
307 $doc->findnode('descendant::ltx:toccaption', $node));
308 $$self{db}->register("ID:$id",
309 $self->addCommon($doc, $node, $tag, $parent_id),
310 role => orNull($node->getAttribute('role')),
311 caption => orNull($self->cleanNode($doc, $caption)),
312 ### toccaption => orNull($self->cleanNode($doc,
313 ### $doc->findnode('descendant::ltx:toccaption', $node))));
314 toccaption => orNull($self->truncateNode($doc, $self->cleanNode($doc, $toccaption))));
315 $self->addAsChild($id, $parent_id); }
316 $self->scanChildren($doc, $node, $id || $parent_id);
317 return; }
318  
319 sub labelled_handler {
320 my ($self, $doc, $node, $tag, $parent_id) = @_;
321 my $id = $node->getAttribute('xml:id');
322 if ($id) {
323 $$self{db}->register("ID:$id",
324 $self->addCommon($doc, $node, $tag, $parent_id),
325 role => orNull($node->getAttribute('role')),
326 );
327 $self->addAsChild($id, $parent_id); }
328 $self->scanChildren($doc, $node, $id || $parent_id);
329 return; }
330  
331 # Maybe with some careful redesign of the schema, this would fall under labelled?
332 sub note_handler {
333 my ($self, $doc, $node, $tag, $parent_id) = @_;
334 my $id = $node->getAttribute('xml:id');
335 if ($id) {
336 my $note = $self->cleanNode($doc, $node);
337 map { $note->removeChild($_) } $doc->findnodes('.//ltx:tags', $note);
338 $$self{db}->register("ID:$id",
339 $self->addCommon($doc, $node, $tag, $parent_id),
340 role => orNull($node->getAttribute('role')),
341 note => $note,
342 );
343 $self->addAsChild($id, $parent_id); }
344 $self->scanChildren($doc, $node, $id || $parent_id);
345 return; }
346  
347 sub anchor_handler {
348 my ($self, $doc, $node, $tag, $parent_id) = @_;
349 my $id = $node->getAttribute('xml:id');
350 if ($id) {
351 $$self{db}->register("ID:$id",
352 $self->addCommon($doc, $node, $tag, $parent_id),
353 title => orNull($self->cleanNode($doc, $node)),
354 );
355 $self->addAsChild($id, $parent_id); }
356 $self->scanChildren($doc, $node, $id || $parent_id);
357 return; }
358  
359 sub ref_handler {
360 my ($self, $doc, $node, $tag, $parent_id) = @_;
361 my $id = $node->getAttribute('xml:id');
362 if (my $label = $node->getAttribute('labelref')) { # Only record refs of labels
363 # Don't scan refs from TOC or 'cited' bibblock
364 if (!$doc->findnodes('ancestor::ltx:tocentry'
365 . '| ancestor::ltx:bibblock[contains(@class,"ltx_bib_cited")]',
366 $node)) {
367 my $entry = $$self{db}->register($label);
368 $entry->noteAssociation(referrers => $parent_id); } }
369 # Usually, a ref won't YET have content; but if it does, we should scan it.
370 $self->default_handler($doc, $node, $tag, $parent_id);
371 return; }
372  
373 sub bibref_handler {
374 my ($self, $doc, $node, $tag, $parent_id) = @_;
375 # Don't scan refs from 'cited' bibblock
376 if (!$doc->findnodes('ancestor::ltx:bibblock[contains(@class,"ltx_bib_cited")]', $node)) {
377 if (my $keys = $node->getAttribute('bibrefs')) {
378 # Citation specifies main 'bibliography', as well as any specific others (eg. per chapter)
379 my $l = $node->getAttribute('inlist');
380 my @lists = (($l ? split(/\s+/, $l) : ()), 'bibliography');
381 foreach my $bibkey (split(',', $keys)) {
382 if ($bibkey) {
383 foreach my $list (@lists) { # Records a *reference* to a bibkey! (for each list)
384 my $entry = $$self{db}->register("BIBLABEL:$list:$bibkey");
385 $entry->noteAssociation(referrers => $parent_id); } } } } }
386 # Usually, a bibref will have, at most, some ltx:bibphrase's; should be scanned.
387 $self->default_handler($doc, $node, $tag, $parent_id);
388 return; }
389  
390 sub glossaryref_handler {
391 my ($self, $doc, $node, $tag, $parent_id) = @_;
392 my $list = $node->getAttribute('inlist');
393 my $key = $node->getAttribute('key');
394 if ($key && $list) {
395 my $entry = $$self{db}->register(join(':', 'GLOSSARY', $list, $key));
396 $entry->noteAssociation(referrers => $parent_id); }
397 $self->default_handler($doc, $node, $tag, $parent_id);
398 return; }
399  
400 # Note that index entries get stored in simple form; just the terms & location.
401 # They will be turned into a tree, sorted, possibly permuted, whatever, by MakeIndex.
402 # [the only content of indexmark should be un-marked up(?) don't recurse]
403 sub indexmark_handler {
404 my ($self, $doc, $node, $tag, $parent_id) = @_;
405 # Get the actual phrases, and any see_also phrases (if any)
406 # Do these need ->cleanNode ???
407 my @phrases = $doc->findnodes('ltx:indexphrase', $node);
408 my @seealso = $doc->findnodes('ltx:indexsee', $node);
409 my $key = join(':', 'INDEX', map { $_->getAttribute('key') } @phrases);
410 my $inlist;
411 if (my $listnames = $node->getAttribute('inlist')) {
412 $inlist = { map { ($_ => 1) } split(/\s/, $listnames) }; }
413 my $entry = $$self{db}->lookup($key)
414 || $$self{db}->register($key, phrases => [@phrases], see_also => [], inlist => $inlist);
415 if (@seealso) {
416 $entry->pushNew('see_also', @seealso); }
417 else {
418 $entry->noteAssociation(referrers => $parent_id => ($node->getAttribute('style') || 'normal')); }
419 return; }
420  
421 # This handles glossaryentry or glossarydefinition
422 # * glossarydefinition is the original description of the item,
423 # randomly (perhaps) located in the document
424 # * glossaryentry is the duplicate(?) of the above
425 # located within a glossarylist.
426 # Probably, the latter is what should be referenced
427 sub glossaryentry_handler {
428 my ($self, $doc, $node, $tag, $parent_id) = @_;
429 # Only register the id for the glossaryentry (in the glossarylist) as desired target
430 my $id = ($tag eq 'ltx:glossaryentry' ? $node->getAttribute('xml:id') : undef);
431 my $p;
432 my $lists = $node->getAttribute('inlist') ||
433 (($p = $doc->findnode('ancestor::ltx:glossarylist[@lists] | ancestor::ltx:glossary[@lists]', $node))
434 && $p->getAttribute('lists'))
435 || 'glossary';
436 my $key = $node->getAttribute('key');
437 my @phrases = $doc->findnodes('ltx:glossaryphrase', $node);
438 # Create an entry for EACH list (they could be distinct definitions)
439 foreach my $list (split(/\s+/, $lists)) {
440 my $gkey = join(':', 'GLOSSARY', $list, $key);
441 my $entry = $$self{db}->lookup($gkey) || $$self{db}->register($gkey);
442 $entry->setValues(map { ('phrase:' . ($_->getAttribute('role') || 'label') => $_) } @phrases);
443 $entry->setValues(id => $id) if $id; }
444  
445 if ($id) {
446 $$self{db}->register("ID:$id",
447 $self->addCommon($doc, $node, $tag, $parent_id)); }
448 $self->scanChildren($doc, $node, $id || $parent_id);
449 return; }
450  
451 # Note this bit of perversity:
452 # <ltx:bibentry> is a semantic bibliographic entry,
453 # as generated from a BibTeX file.
454 # <ltx:bibitem> is a formatted bibliographic entry,
455 # as generated from an explicit thebibliography environment (eg. manually, or in a .bbl),
456 # or as formatted from a <ltx:bibentry> by MakeBibliography.
457 # For a bibitem, we'll store the bibliographic metadata in the DB, keyed by the ID of the item.
458 sub bibitem_handler {
459 my ($self, $doc, $node, $tag, $parent_id) = @_;
460 my $id = $node->getAttribute('xml:id');
461 if ($id) {
462 my $key = $node->getAttribute('key');
463 my $bib = $doc->findnode('ancestor-or-self::ltx:bibliography', $node);
464 # Probably should only be one list, but just in case?
465 my @lists = split(/\s+/, ($bib && $bib->getAttribute('lists')) || 'bibliography');
466 if ($key) {
467 foreach my $list (@lists) { # BIBLABEL is for the reference to a biblio. item/entry
468 $$self{db}->register("BIBLABEL:$list:$key", id => orNull($id)); } }
469 # The actual bibliographic data is recorded keyed by the xml:id of the bibitem!
470 # Do these need ->cleanNode ???
471 $$self{db}->register("ID:$id", id => orNull($id), type => orNull($tag), parent => orNull($parent_id), bibkey => orNull($key),
472 location => orNull($doc->siteRelativeDestination),
473 pageid => orNull($self->pageID($doc)),
474 fragid => orNull($self->inPageID($doc, $node)),
475 authors => orNull($doc->findnode('ltx:tags/ltx:tag[@role="authors"]', $node)),
476 fullauthors => orNull($doc->findnode('ltx:tags/ltx:tag[@role="fullauthors"]', $node)),
477 year => orNull($doc->findnode('ltx:tags/ltx:tag[@role="year"]', $node)),
478 number => orNull($doc->findnode('ltx:tags/ltx:tag[@role="number"]', $node)),
479 refnum => orNull($doc->findnode('ltx:tags/ltx:tag[@role="refnum"]', $node)),
480 title => orNull($doc->findnode('ltx:tags/ltx:tag[@role="title"]', $node)),
481 keytag => orNull($doc->findnode('ltx:tags/ltx:tag[@role="key"]', $node)),
482 typetag => orNull($doc->findnode('ltx:tags/ltx:tag[@role="bibtype"]', $node))); }
483 $self->scanChildren($doc, $node, $id || $parent_id);
484 return; }
485  
486 # For a bibentry, we'll only store the citation key, so we know it's there.
487 sub bibentry_handler {
488 my ($self, $doc, $node, $tag, $parent_id) = @_;
489 # The actual bibliographic data is recorded keyed by the xml:id of the bibitem
490 # AFTER the bibentry has been formatted into a bibitem by MakeBibliography!
491 # So, there's really nothing to do now.
492 ## HOWEVER; this ultimately requires formatting the bibliography twice (for complex sites).
493 ## This needs to be reworked!
494 return; }
495  
496 sub declare_handler {
497 my ($self, $doc, $node, $tag, $parent_id) = @_;
498 # See preprocess_symbols for the extraction of the "defined" symbol (if any)
499 # Also recognize marks for definition, notation...
500 my $type = $node->getAttribute('type');
501 my $sort = $node->getAttribute('sortkey');
502 my $decl_id = $node->getAttribute('xml:id');
503 my $term = $self->cleanNode($doc, $doc->findnode('child::ltx:tags/ltx:tag[@role="term"]', $node));
504 my $description = $self->cleanNode($doc, $doc->findnode('child::ltx:text', $node));
505 my $definiens = $node->getAttribute('definiens');
506 if (defined $type && ($type eq 'definition')) {
507 if ((!defined $definiens) && (defined $term)) {
508 # Extract the definiens from the term nade
509 my (@syms) = $doc->findnodes('descendant-or-self::ltx:XMTok[@meaning]', $term);
510 # We're probably not defining a relation, so put non-relations first.
511 @syms = ((grep { ($_->getAttribute('role') || '') ne 'RELOP'; } @syms), @syms);
512 # HACK; remove apparent definitions to lists
513 # [these will have to be handled much more intentionally]
514 @syms = grep { $_->getAttribute('meaning') !~ /^delimited-/ } @syms;
515 $definiens = $syms[0] && $syms[0]->getAttribute('meaning'); }
516 if (defined $definiens) {
517 $$self{db}->register("DECLARATION:global:$definiens",
518 $self->addCommon($doc, $node, $tag, $parent_id),
519 description => $description); } }
520 elsif ((!$type) && $parent_id) { # No type? Assume local definition. (or should be explicit scope?
521 if ($decl_id && ($description || $doc->findnode('ltx:tags/ltx:tag', $node))) {
522 $$self{db}->register("DECLARATION:local:$decl_id",
523 $self->addCommon($doc, $node, $tag, $parent_id),
524 description => $description); } }
525  
526 if ($sort) { # It only goes into Notation tables/indices if a sortkey.
527 $$self{db}->register("NOTATION:" . ($definiens || $decl_id || $sort),
528 $self->addCommon($doc, $node, $tag, $parent_id),
529 sortkey => $sort, description => $description); }
530 # No real benefit to scan the contents? (and makes it SLOW)
531 # $self->default_handler($doc,$node,$tag,$parent_id);
532 return; }
533  
534 # I'm thinking we shouldn't acknowledge navigation data at all?
535 sub navigation_handler {
536 my ($self, $doc, $node, $tag, $parent_id) = @_;
537 return; }
538  
539 # RDF should be recorded with its "about" designation, or its immediate parent
540 sub rdf_handler {
541 my ($self, $doc, $node, $tag, $parent_id) = @_;
542 my $id = $node->getAttribute('about');
543 if (!($id && ($id =~ s/^#//))) {
544 $id = $parent_id; }
545 my $property = $node->getAttribute('property');
546 my $value = $node->getAttribute('resource') || $node->getAttribute('content');
547 return unless ($property && $value);
548 $$self{db}->register("ID:$id", $property => orNull($value));
549 return; }
550  
551 # I'm thinking we shouldn't acknowledge rawhtml data at all?
552 sub rawhtml_handler {
553 my ($self, $doc, $node, $tag, $parent_id) = @_;
554 return; }
555  
556 sub orNull {
557 return (grep { defined } @_) ? @_ : undef; }
558  
559 # ================================================================================
560 1;