@@ -348,6 +348,65 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
348348 </xsl : copy >
349349</xsl : template >
350350
351+ <!-- Pruning templates generate a subtree towards $target-node -->
352+ <!-- which prunes as many other branches as possible while -->
353+ <!-- retaining reasonably decent output of the desire tree. -->
354+ <!-- i.e. drop content other than titled structural elements. -->
355+ <xsl : template match =" @*" mode =" pruning" >
356+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
357+ </xsl : template >
358+
359+ <xsl : template match =" node()" mode =" pruning" >
360+ <xsl : param name =" target-node" />
361+ <xsl : choose >
362+ <!-- This node contains the target node, copy and keep going -->
363+ <xsl : when test =" count(descendant::*|$target-node) = count(descendant::*)" >
364+ <xsl : copy >
365+ <xsl : apply-templates select =" node()|@*" mode =" pruning" >
366+ <xsl : with-param name =" target-node" select =" $target-node" />
367+ </xsl : apply-templates >
368+ </xsl : copy >
369+ </xsl : when >
370+ <!-- This node is target node, keep it all -->
371+ <xsl : when test =" count(.|$target-node) = 1" >
372+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
373+ </xsl : when >
374+ <!-- Shift to aggressive pruning -->
375+ <xsl : otherwise >
376+ <xsl : apply-templates select =" ." mode =" pruning-prune" />
377+ </xsl : otherwise >
378+ </xsl : choose >
379+ </xsl : template >
380+
381+ <!-- Certain structural elements have data/metadata that we want to -->
382+ <!-- preserve, so we copy them over entirely. -->
383+ <xsl : template match =" frontmatter|docinfo|bookinfo|backmatter" mode =" pruning" >
384+ <xsl : copy >
385+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
386+ </xsl : copy >
387+ </xsl : template >
388+
389+ <!-- Preserve anything from here on down -->
390+ <xsl : template match =" node()|@*" mode =" pruning-keep" >
391+ <xsl : copy >
392+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
393+ </xsl : copy >
394+ </xsl : template >
395+
396+ <!-- Once aggressively pruning, only keep structural elements that give us -->
397+ <!-- numbering and toc info. -->
398+ <xsl : template match =" &STRUCTURAL;|@*" mode =" pruning-prune" >
399+ <xsl : copy >
400+ <xsl : apply-templates select =" &STRUCTURAL; |title|@*" mode =" pruning-prune" >
401+ </xsl : apply-templates >
402+ </xsl : copy >
403+ </xsl : template >
404+
405+ <!-- If we hit a title, grab all the contents so the ToC gets content-->
406+ <xsl : template match =" title" mode =" pruning-prune" >
407+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
408+ </xsl : template >
409+
351410<!-- These templates initiate and create several iterations of -->
352411<!-- the source tree via modal templates. Think of each as a -->
353412<!-- "pass" through the source. Generally this constructs the -->
@@ -414,12 +473,43 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
414473</xsl : variable >
415474<xsl : variable name =" original-labeled" select =" exsl:node-set($original-labeled-rtf)" />
416475
476+ <!-- See pretext-html for full version of these params -->
477+ <!-- "forward declared" here to avoid stylesheet ordering conflict -->
478+ <xsl : param name =" subtree" select =" ''" />
479+ <xsl : param name =" html.build-preview" select =" ''" />
480+ <!-- After labeling, but before further processing, build a -->
481+ <!-- pruned tree. Most of the time it is a copy of the labeled tree -->
482+ <!-- but when doing a preview build of a subtree, we prune -->
483+ <!-- down the labeled tree to the desired subtree and select other -->
484+ <!-- important elements. -->
485+ <xsl : variable name =" pruning-tree-rtf" >
486+ <xsl : choose >
487+ <xsl : when test =" $subtree != '' and $html.build-preview = 'yes'" >
488+ <xsl : variable name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
489+ <xsl : apply-templates select =" $original-labeled" mode =" pruning" >
490+ <xsl : with-param name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
491+ </xsl : apply-templates >
492+ </xsl : when >
493+ <xsl : otherwise >
494+ <xsl : copy-of select =" $original-labeled" />
495+ </xsl : otherwise >
496+ </xsl : choose >
497+ </xsl : variable >
498+ <xsl : variable name =" pruning-tree" select =" exsl:node-set($pruning-tree-rtf)" />
499+
500+ <!-- Variable just to allow some printing after pruning-tree is established -->
501+ <xsl : variable name =" not-used-pruning-info" >
502+ <xsl : if test =" $subtree != '' and $html.build-preview = 'yes'" >
503+ <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 >
504+ </xsl : if >
505+ </xsl : variable >
506+
417507<!-- A global list of all "webwork" used for -->
418508<!-- efficient backward-compatible indentification -->
419- <xsl : variable name =" all-webwork" select =" $original-labeled //webwork" />
509+ <xsl : variable name =" all-webwork" select =" $pruning-tree //webwork" />
420510
421511<xsl : variable name =" webwork-rtf" >
422- <xsl : apply-templates select =" $original-labeled " mode =" webwork" />
512+ <xsl : apply-templates select =" $pruning-tree " mode =" webwork" />
423513</xsl : variable >
424514<xsl : variable name =" webworked" select =" exsl:node-set($webwork-rtf)" />
425515
0 commit comments