lib/LaTeXML/Post/Split.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 247 CC 21 SCAR 41.8
CC
21
Cov
0%
getPageName
156.0
process
42.0
processPages
30.0
CRAP 65.7
1 # /=====================================================================\ #
2 # | LaTeXML::Post::Split | #
3 # | Split documents into pages | #
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::Split;
14 use strict;
15 use warnings;
16 use LaTeXML::Util::Pathname;
17 use LaTeXML::Common::XML;
18 use LaTeXML::Post;
19 use base qw(LaTeXML::Post::Processor);
20  
21 sub new {
22 my ($class, %options) = @_;
23 my $self = $class->SUPER::new(%options);
24 $$self{split_xpath} = $options{split_xpath};
25 $$self{splitnaming} = $options{splitnaming};
26 $$self{no_navigation} = $options{no_navigation};
27 return $self; }
28  
29 # Could this actually just return the nodes that are to become pages?
30 # sub toProcess { ??? }
31  
32 sub process {
33 my ($self, $doc, $root) = @_;
34 # RISKY and annoying; to split, we really need an id on the root.
35 # Writer will remove it.
36 $root->setAttribute('xml:id' => 'TEMPORARY_DOCUMENT_ID') unless $root->hasAttribute('xml:id');
37  
38 my @docs = ($doc);
39 my @pages = $self->getPages($doc);
40 # Weird test: exclude the "whole document" from the list (?)
41 @pages = grep { $_->parentNode->parentNode } @pages; # Strip out the root node.
42 if (@pages) {
43 my @nav = $doc->findnodes("descendant::ltx:navigation");
44 $doc->removeNodes(@nav) if @nav;
45 my $tree = { node => $root, document => $doc,
46 id => $root->getAttribute('xml:id'), name => $doc->getDestination,
47 children => [] };
48 # Group the pages into a tree, in case they are nested.
49 my $haschildren = {};
50 presortPages($tree, $haschildren, @pages);
51 # Work out the destination paths for each page
52 $self->prenamePages($doc, $tree, $haschildren);
53 # Now, create remove and create documents for each page.
54 push(@docs, $self->processPages($doc, @{ $$tree{children} }));
55  
56 $self->addNavigation($tree, @nav) if @nav;
57 }
58 my $n = scalar(@docs);
59 NoteLog(($n > 1 ? " [Split into in $n TOCs]" : "[not split]"));
60 return @docs; }
61  
62 # Get the nodes in the document that WILL BECOME separate "pages".
63 # (they are not yet removed from the main document)
64 # Subclass can override, if needed.
65 sub getPages {
66 my ($self, $doc) = @_;
67 return $doc->findnodes($$self{split_xpath}); }
68  
69 # Sort the pages into a tree, in case some pages are children of others
70 # If a page contains NOTHING BUT child pages (except frontmatter),
71 # we could just merge that page as a level in it's containing TOC instead of a document.???
72 sub presortPages {
73 my ($tree, $haschildren, @pages) = @_;
74 my $nextlevel; # if $page is a descendant of some othe page
75 foreach my $page (@pages) {
76 # $page should be a child of the current node, or a child of some higher one.
77 while ($$tree{parent} && !isChild($page, $$tree{node})) {
78 $tree = $$tree{parent}; }
79 my $entry = { node => $page, upid => $$tree{id}, id => $page->getAttribute('xml:id'),
80 parent => $tree, children => [] };
81 $$haschildren{ $$tree{node}->localname } = 1; # Wrong key for this!?!
82 push(@{ $$tree{children} }, $entry);
83 $tree = $entry; } # go "down", in case following are children of current node.
84 return; }
85  
86 # Get destination pathnames for each page.
87 sub prenamePages {
88 my ($self, $doc, $tree, $haschildren) = @_;
89 foreach my $entry (@{ $$tree{children} }) {
90 $$entry{name} = $self->getPageName($doc, $$entry{node}, $$tree{node}, $$tree{name},
91 $$haschildren{ $$entry{node}->localname });
92 $self->prenamePages($doc, $entry, $haschildren); }
93 return; }
94  
95 # Process a sequence of page entries, removing them and generating documents for each.
96 sub processPages {
97 my ($self, $doc, @entries) = @_;
98 my $rootid = $doc->getDocumentElement->getAttribute('xml:id');
99 # Before any document surgery, copy inheritable attributes.
100 my $intoc = 0; # Whether ANY children appear in toc
101 foreach my $entry (@entries) {
102 my $node = $$entry{node};
103 $intoc ||= ($node->getAttribute('inlist') || '') =~ /\btoc\b/;
104 foreach my $attr (qw(xml:lang backgroundcolor)) {
105 if (my $anc = $doc->findnode('ancestor-or-self::*[@' . $attr . '][1]', $node)) {
106 $node->setAttribute($attr => $anc->getAttribute($attr)); } } }
107 my @docs = ();
108 while (@entries) {
109 my $parent = $entries[0]->{node}->parentNode;
110 # Remove $page & ALL following siblings (backwards).
111 my @removed = ();
112 while (my $sib = $parent->lastChild) {
113 $parent->removeChild($sib);
114 unshift(@removed, $sib);
115 last if $sib->isSameNode($entries[0]->{node}); }
116 # Build toc from adjacent nodes that are being extracted.
117 my @toc = ();
118 # Process a sequence of adjacent pages; these will go into the same TOC.
119 while (@entries && @removed && $entries[0]->{node}->isSameNode($removed[0])) {
120 my $entry = shift(@entries);
121 my $page = $$entry{node};
122 # If any pages go in toc, Assume siblings on their own page should go also
123 $page->setAttribute(inlist => 'toc') if $intoc && !$page->hasAttribute('inlist');
124 $doc->removeNodes(shift(@removed));
125 my $id = $page->getAttribute('xml:id');
126 my $tocentry = ['ltx:tocentry', {},
127 ['ltx:ref', { idref => $id, show => 'toctitle' }]];
128 push(@toc, $tocentry);
129 # Due to the way document building works, we remove & process children pages
130 # BEFORE processing this page.
131 my @childdocs = $self->processPages($doc, @{ $$entry{children} });
132 my $subdoc = $doc->newDocument($page, destination => $$entry{name},
133 parentDocument => $doc, parent_id => $$entry{upid});
134 $$entry{document} = $subdoc;
135 push(@docs, $subdoc, @childdocs); }
136 # Finally, add the toc to reflect the consecutive, removed nodes, and add back the remainder
137 my $type = $parent->localname;
138 $doc->addNodes($parent, ['ltx:TOC', {}, ['ltx:toclist', { class => 'ltx_toclist_' . $type }, @toc]])
139 if @toc && !$doc->findnodes("descendant::ltx:TOC[\@lists='toc']", $parent);
140 map { $parent->addChild($_) } @removed; }
141 return @docs; }
142  
143 sub addNavigation {
144 my ($self, $entry, @nav) = @_;
145 my $doc = $$entry{document};
146 $doc->addNodes($doc->getDocumentElement, @nav); # cloning, as needed...
147 foreach my $child (@{ $$entry{children} }) {
148 my $childdoc = $$child{document};
149 $self->addNavigation($child, @nav); } # now, recurse
150 return; }
151  
152 # error situation: generate some kind of unique name for page
153 sub generateUnnamedPageName {
154 my ($self) = @_;
155 my $ctr = ++$$self{unnamed_page_counter};
156 return "FOO" . $ctr; }
157  
158 sub getPageName {
159 my ($self, $doc, $page, $parent, $parentpath, $recursive) = @_;
160 my $asdir;
161 my $naming = $$self{splitnaming};
162 my $attr = ($naming =~ /^id/ ? 'xml:id'
163 : ($naming =~ /^label/ ? 'labels' : undef));
164 my $name = $page->getAttribute($attr);
165 $name =~ s/\s+.*// if $name; # Truncate in case multiple labels.
166 $name =~ s/^LABEL:// if $name;
167 if (!$name) {
168 if (($attr eq 'labels') && ($name = $page->getAttribute('xml:id'))) {
169 Info('expected', $attr, $doc->getQName($page),
170 "Using '$name' to create page pathname, instead of missing '$attr'");
171 $attr = 'xml:id'; }
172 else {
173 $name = $self->generateUnnamedPageName;
174 Info('expected', $attr, $doc->getQName($page),
175 "Using '$name' to create page pathname, instead of missing '$attr'"); } }
176 if ($naming =~ /relative$/) {
177 my $pname = $parent->getAttribute($attr);
178 $pname =~ s/\s+.*// if $pname; # Truncate in case multiple labels.
179 $pname =~ s/^LABEL:// if $pname;
180 if ($pname && $name =~ /^\Q$pname\E(\.|_|:)+(.*)$/) {
181 $name = $2; }
182 $asdir = $recursive; }
183 $name =~ s/:+/_/g;
184 if ($asdir) {
185 return pathname_make(dir => pathname_concat(pathname_directory($parentpath), $name),
186 name => 'index',
187 type => $doc->getDestinationExtension); }
188 else {
189 return pathname_make(dir => pathname_directory($parentpath),
190 name => $name,
191 type => $doc->getDestinationExtension); } }
192  
193 # ================================================================================
194 1;
195