@@ -355,6 +355,65 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
355355 </xsl : copy >
356356</xsl : template >
357357
358+ <!-- Pruning templates generate a subtree towards $target-node -->
359+ <!-- which prunes as many other branches as possible while -->
360+ <!-- retaining reasonably decent output of the desire tree. -->
361+ <!-- i.e. drop content other than titled structural elements. -->
362+ <xsl : template match =" @*" mode =" pruning" >
363+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
364+ </xsl : template >
365+
366+ <xsl : template match =" node()" mode =" pruning" >
367+ <xsl : param name =" target-node" />
368+ <xsl : choose >
369+ <!-- This node contains the target node, copy and keep going -->
370+ <xsl : when test =" count(descendant::*|$target-node) = count(descendant::*)" >
371+ <xsl : copy >
372+ <xsl : apply-templates select =" node()|@*" mode =" pruning" >
373+ <xsl : with-param name =" target-node" select =" $target-node" />
374+ </xsl : apply-templates >
375+ </xsl : copy >
376+ </xsl : when >
377+ <!-- This node is target node, keep it all -->
378+ <xsl : when test =" count(.|$target-node) = 1" >
379+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
380+ </xsl : when >
381+ <!-- Shift to aggressive pruning -->
382+ <xsl : otherwise >
383+ <xsl : apply-templates select =" ." mode =" pruning-prune" />
384+ </xsl : otherwise >
385+ </xsl : choose >
386+ </xsl : template >
387+
388+ <!-- Certain structural elements have data/metadata that we want to -->
389+ <!-- preserve, so we copy them over entirely. -->
390+ <xsl : template match =" frontmatter|docinfo|bookinfo|backmatter" mode =" pruning" >
391+ <xsl : copy >
392+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
393+ </xsl : copy >
394+ </xsl : template >
395+
396+ <!-- Preserve anything from here on down -->
397+ <xsl : template match =" node()|@*" mode =" pruning-keep" >
398+ <xsl : copy >
399+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
400+ </xsl : copy >
401+ </xsl : template >
402+
403+ <!-- Once aggressively pruning, only keep structural elements that give us -->
404+ <!-- numbering and toc info. -->
405+ <xsl : template match =" &STRUCTURAL;|@*" mode =" pruning-prune" >
406+ <xsl : copy >
407+ <xsl : apply-templates select =" &STRUCTURAL; |title|@*" mode =" pruning-prune" >
408+ </xsl : apply-templates >
409+ </xsl : copy >
410+ </xsl : template >
411+
412+ <!-- If we hit a title, grab all the contents so the ToC gets content-->
413+ <xsl : template match =" title" mode =" pruning-prune" >
414+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
415+ </xsl : template >
416+
358417<!-- These templates initiate and create several iterations of -->
359418<!-- the source tree via modal templates. Think of each as a -->
360419<!-- "pass" through the source. Generally this constructs the -->
@@ -476,12 +535,43 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
476535</xsl : variable >
477536<xsl : variable name =" original-labeled" select =" exsl:node-set($original-labeled-rtf)" />
478537
538+ <!-- See pretext-html for full version of these params -->
539+ <!-- "forward declared" here to avoid stylesheet ordering conflict -->
540+ <xsl : param name =" subtree" select =" ''" />
541+ <xsl : param name =" html.build-preview" select =" ''" />
542+ <!-- After labeling, but before further processing, build a -->
543+ <!-- pruned tree. Most of the time it is a copy of the labeled tree -->
544+ <!-- but when doing a preview build of a subtree, we prune -->
545+ <!-- down the labeled tree to the desired subtree and select other -->
546+ <!-- important elements. -->
547+ <xsl : variable name =" pruning-tree-rtf" >
548+ <xsl : choose >
549+ <xsl : when test =" $subtree != '' and $html.build-preview = 'yes'" >
550+ <xsl : variable name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
551+ <xsl : apply-templates select =" $original-labeled" mode =" pruning" >
552+ <xsl : with-param name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
553+ </xsl : apply-templates >
554+ </xsl : when >
555+ <xsl : otherwise >
556+ <xsl : copy-of select =" $original-labeled" />
557+ </xsl : otherwise >
558+ </xsl : choose >
559+ </xsl : variable >
560+ <xsl : variable name =" pruning-tree" select =" exsl:node-set($pruning-tree-rtf)" />
561+
562+ <!-- Variable just to allow some printing after pruning-tree is established -->
563+ <xsl : variable name =" not-used-pruning-info" >
564+ <xsl : if test =" $subtree != '' and $html.build-preview = 'yes'" >
565+ <xsl : message >PTX:INFO: Pruned build tree to <xsl : value-of select =" count($pruning-tree//node())" /> out of original <xsl : value-of select =" count($original-labeled//node())" /> nodes.</xsl : message >
566+ </xsl : if >
567+ </xsl : variable >
568+
479569<!-- A global list of all "webwork" used for -->
480570<!-- efficient backward-compatible indentification -->
481- <xsl : variable name =" all-webwork" select =" $original-labeled //webwork" />
571+ <xsl : variable name =" all-webwork" select =" $pruning-tree //webwork" />
482572
483573<xsl : variable name =" webwork-rtf" >
484- <xsl : apply-templates select =" $original-labeled " mode =" webwork" />
574+ <xsl : apply-templates select =" $pruning-tree " mode =" webwork" />
485575</xsl : variable >
486576<xsl : variable name =" webworked" select =" exsl:node-set($webwork-rtf)" />
487577
0 commit comments