| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::Collector | # | ||
| 3 | # | Abstract class; collects info & builds, like MakeIndex, MakeBib... | # | ||
| 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::Collector; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Util::Pathname; | ||
| 16 | use LaTeXML::Common::XML; | ||
| 17 | use charnames qw(:full); | ||
| 18 | use LaTeXML::Post; | ||
| 19 | use base qw(LaTeXML::Post::Processor); | ||
| 20 | |||
| 21 | # Options: | ||
| 22 | # scanner: pass in a scanner to scan what we've just built | ||
| 23 | # Since these classes are typically built from scanned data... | ||
| 24 | sub new { | ||
| 25 | my ($class, %options) = @_; | ||
| 26 | my $self = $class->SUPER::new(%options); | ||
| 27 | $$self{scanner} = $options{scanner}; | ||
| 28 | return $self; } | ||
| 29 | |||
| 30 | # Abstract class: | ||
| 31 | # Needs sub process {... | ||
| 32 | |||
| 33 | # This class creates a document from data "collected" from other documents, | ||
| 34 | # possibly directly (such as a bibliography) | ||
| 35 | # or indirectly (such as index data pulled from the ObjectDB). | ||
| 36 | # Well.... It would be clearer if it did. | ||
| 37 | # it actually "fills in" an element with collected data. | ||
| 38 | # AND THEN, possibly, splits it into pages based on some letter or initial. | ||
| 39 | # Actually, the instanciable class will do the splitting. | ||
| 40 | # This class just has some common methods that help support doing that! | ||
| 41 | |||
| 42 | # Given that the subclass has broken up the collected content into portions | ||
| 43 | # (presumably by Initials?), we'll | ||
| 44 | # (1) fill in the main document (at $root) with the first subcollection, | ||
| 45 | # and (2) create new documents for the rest. | ||
| 46 | # | ||
| 47 | # The data provided is a hash where the key is the initial | ||
| 48 | # and the value is the XML data (either LibXML data, or array form). | ||
| 49 | # $collections{initial}=>XML | ||
| 50 | # | ||
| 51 | # NOTE: This would also be cleaner if we knew which documents were | ||
| 52 | # going to be created and created them first before creating the XML | ||
| 53 | # to go into them, this would let the new subdocuments deal with cloning etc. | ||
| 54 | # probably resulting in less id morphing?!?! Probably not so critical anyway... | ||
| 55 | sub makeSubCollectionDocuments { | ||
| 56 | my ($self, $doc, $root, %collections) = @_; | ||
| 57 | my @docs = (); | ||
| 58 | |||
| 59 | my $roottag = $doc->getQName($root); | ||
| 60 | my $rootid = $root->getAttribute('xml:id'); | ||
| 61 | my $rootrole = $root->getAttribute('role'); | ||
| 62 | my @initials = sort keys %collections; | ||
| 63 | my $init0 = $initials[0]; | ||
| 64 | my @ids = ([$rootid, $init0], map { ["$rootid.$_", $_] } @initials[1 .. $#initials]); | ||
| 65 | # Patchup the main node; Replace title, add nav, add the 1st subcollection. | ||
| 66 | my @titles = $doc->findnodes('//ltx:title | //ltx:toctitle', $root); | ||
| 67 | my @titlestuff = $doc->trimChildNodes($titles[0]); | ||
| 68 | $doc->removeNodes(@titles); | ||
| 69 | for (my $i = 0 ; $i <= $#ids ; $i++) { | ||
| 70 | my $subdoc = ($i == 0 ? $doc | ||
| 71 | : $doc->newDocument([$roottag, { 'xml:id' => $ids[$i][0], role => $rootrole }], | ||
| 72 | parentDocument => $doc, | ||
| 73 | parent_id => $rootid, | ||
| 74 | destination => $self->getPageName($doc, $ids[$i][1]))); | ||
| 75 | push(@docs, $subdoc); | ||
| 76 | # Quite possibly the new data is made from pieces from the original; | ||
| 77 | # This hopefully will fix up the book-keeping. | ||
| 78 | $doc->removeNodes($collections{ $ids[$i][1] }); | ||
| 79 | # Now, finally, fill in the new sub-document! | ||
| 80 | $subdoc->addNodes($subdoc->findnode('//' . $roottag), | ||
| 81 | ['ltx:title', {}, @titlestuff, ($i == 0 ? () : (' ', $ids[$i][1]))], | ||
| 82 | ['ltx:TOC', { format => 'veryshort' }, | ||
| 83 | ['ltx:toclist', {}, | ||
| 84 | map { ($_ == $i | ||
| 85 | ? ['ltx:tocentry', {}, $ids[$_][1]] | ||
| 86 | : ['ltx:tocentry', {}, ['ltx:ref', | ||
| 87 | { idref => $ids[$_][0], show => 'refnum' }, | ||
| 88 | $ids[$_][1]]]) } | ||
| 89 | 0 .. $#ids]], | ||
| 90 | $collections{ $ids[$i][1] }); | ||
| 91 | if ($i > 0) { | ||
| 92 | #### $docs[$i ]->addNavigation(previous=>$ids[$i-1][0]); | ||
| 93 | #### $docs[$i-1]->addNavigation(next =>$ids[$i ][0]); | ||
| 94 | } } | ||
| 95 | return @docs; } | ||
| 96 | |||
| 97 | sub rescan { | ||
| 98 | my ($self, $doc) = @_; | ||
| 99 | return ($$self{scanner} ? $$self{scanner}->process($doc, $$self{scanner}->toProcess($doc)) : $doc); } | ||
| 100 | |||
| 101 | # If the main document is named "index", (like index.html) presumably this collection | ||
| 102 | # will be contained in its own directory, so the sub document names can be short. | ||
| 103 | # Otherwise, we'll append the initial to | ||
| 104 | sub getPageName { | ||
| 105 | my ($self, $doc, $initial) = @_; | ||
| 106 | my ($dir, $name, $type) = pathname_split($doc->getDestination); | ||
| 107 | return pathname_make(dir => $dir, | ||
| 108 | name => ($name eq 'index' ? $initial : "$name.$initial"), | ||
| 109 | type => $type); } | ||
| 110 | |||
| 111 | # ================================================================================ | ||
| 112 | 1; | ||
| 113 |