@@ -296,6 +296,65 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
296296 </xsl : copy >
297297</xsl : template >
298298
299+ <!-- Pruning templates generate a subtree towards $target-node -->
300+ <!-- which prunes as many other branches as possible while -->
301+ <!-- retaining reasonably decent output of the desire tree. -->
302+ <!-- i.e. drop content other than titled structural elements. -->
303+ <xsl : template match =" @*" mode =" pruning" >
304+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
305+ </xsl : template >
306+
307+ <xsl : template match =" node()" mode =" pruning" >
308+ <xsl : param name =" target-node" />
309+ <xsl : choose >
310+ <!-- This node contains the target node, copy and keep going -->
311+ <xsl : when test =" count(descendant::*|$target-node) = count(descendant::*)" >
312+ <xsl : copy >
313+ <xsl : apply-templates select =" node()|@*" mode =" pruning" >
314+ <xsl : with-param name =" target-node" select =" $target-node" />
315+ </xsl : apply-templates >
316+ </xsl : copy >
317+ </xsl : when >
318+ <!-- This node is target node, keep it all -->
319+ <xsl : when test =" count(.|$target-node) = 1" >
320+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
321+ </xsl : when >
322+ <!-- Shift to aggressive pruning -->
323+ <xsl : otherwise >
324+ <xsl : apply-templates select =" ." mode =" pruning-prune" />
325+ </xsl : otherwise >
326+ </xsl : choose >
327+ </xsl : template >
328+
329+ <!-- Certain structural elements have data/metadata that we want to -->
330+ <!-- preserve, so we copy them over entirely. -->
331+ <xsl : template match =" frontmatter|docinfo|bookinfo|backmatter" mode =" pruning" >
332+ <xsl : copy >
333+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
334+ </xsl : copy >
335+ </xsl : template >
336+
337+ <!-- Preserve anything from here on down -->
338+ <xsl : template match =" node()|@*" mode =" pruning-keep" >
339+ <xsl : copy >
340+ <xsl : apply-templates select =" node()|@*" mode =" pruning-keep" />
341+ </xsl : copy >
342+ </xsl : template >
343+
344+ <!-- Once aggressively pruning, only keep structural elements that give us -->
345+ <!-- numbering and toc info. -->
346+ <xsl : template match =" &STRUCTURAL;|@*" mode =" pruning-prune" >
347+ <xsl : copy >
348+ <xsl : apply-templates select =" &STRUCTURAL; |title|@*" mode =" pruning-prune" >
349+ </xsl : apply-templates >
350+ </xsl : copy >
351+ </xsl : template >
352+
353+ <!-- If we hit a title, grab all the contents so the ToC gets content-->
354+ <xsl : template match =" title" mode =" pruning-prune" >
355+ <xsl : apply-templates select =" ." mode =" pruning-keep" />
356+ </xsl : template >
357+
299358<!-- These templates initiate and create several iterations of -->
300359<!-- the source tree via modal templates. Think of each as a -->
301360<!-- "pass" through the source. Generally this constructs the -->
@@ -362,12 +421,43 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
362421</xsl : variable >
363422<xsl : variable name =" original-labeled" select =" exsl:node-set($original-labeled-rtf)" />
364423
424+ <!-- See pretext-html for full version of these params -->
425+ <!-- "forward declared" here to avoid stylesheet ordering conflict -->
426+ <xsl : param name =" subtree" select =" ''" />
427+ <xsl : param name =" html.build-preview" select =" ''" />
428+ <!-- After labeling, but before further processing, build a -->
429+ <!-- pruned tree. Most of the time it is a copy of the labeled tree -->
430+ <!-- but when doing a preview build of a subtree, we prune -->
431+ <!-- down the labeled tree to the desired subtree and select other -->
432+ <!-- important elements. -->
433+ <xsl : variable name =" pruning-tree-rtf" >
434+ <xsl : choose >
435+ <xsl : when test =" $subtree != '' and $html.build-preview = 'yes'" >
436+ <xsl : variable name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
437+ <xsl : apply-templates select =" $original-labeled" mode =" pruning" >
438+ <xsl : with-param name =" target-node" select =" $original-labeled//*[@xml:id = $subtree]" />
439+ </xsl : apply-templates >
440+ </xsl : when >
441+ <xsl : otherwise >
442+ <xsl : copy-of select =" $original-labeled" />
443+ </xsl : otherwise >
444+ </xsl : choose >
445+ </xsl : variable >
446+ <xsl : variable name =" pruning-tree" select =" exsl:node-set($pruning-tree-rtf)" />
447+
448+ <!-- Variable just to allow some printing after pruning-tree is established -->
449+ <xsl : variable name =" not-used-pruning-info" >
450+ <xsl : if test =" $subtree != '' and $html.build-preview = 'yes'" >
451+ <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 >
452+ </xsl : if >
453+ </xsl : variable >
454+
365455<!-- A global list of all "webwork" used for -->
366456<!-- efficient backward-compatible indentification -->
367- <xsl : variable name =" all-webwork" select =" $original-labeled //webwork" />
457+ <xsl : variable name =" all-webwork" select =" $pruning-tree //webwork" />
368458
369459<xsl : variable name =" webwork-rtf" >
370- <xsl : apply-templates select =" $original-labeled " mode =" webwork" />
460+ <xsl : apply-templates select =" $pruning-tree " mode =" webwork" />
371461</xsl : variable >
372462<xsl : variable name =" webworked" select =" exsl:node-set($webwork-rtf)" />
373463
0 commit comments