Skip to content

Commit 0bc02d3

Browse files
Assembly: add prunning tree between labeled and webwork
1 parent 81b652b commit 0bc02d3

1 file changed

Lines changed: 92 additions & 2 deletions

File tree

xsl/pretext-assembly.xsl

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,65 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
349349
</xsl:copy>
350350
</xsl:template>
351351

352+
<!-- Pruning templates generate a subtree towards $target-node -->
353+
<!-- which prunes as many other branches as possible while -->
354+
<!-- retaining reasonably decent output of the desire tree. -->
355+
<!-- i.e. drop content other than titled structural elements. -->
356+
<xsl:template match="@*" mode="pruning">
357+
<xsl:apply-templates select="." mode="pruning-keep"/>
358+
</xsl:template>
359+
360+
<xsl:template match="node()" mode="pruning">
361+
<xsl:param name="target-node"/>
362+
<xsl:choose>
363+
<!-- This node contains the target node, copy and keep going -->
364+
<xsl:when test="count(descendant::*|$target-node) = count(descendant::*)">
365+
<xsl:copy>
366+
<xsl:apply-templates select="node()|@*" mode="pruning">
367+
<xsl:with-param name="target-node" select="$target-node"/>
368+
</xsl:apply-templates>
369+
</xsl:copy>
370+
</xsl:when>
371+
<!-- This node is target node, keep it all -->
372+
<xsl:when test="count(.|$target-node) = 1">
373+
<xsl:apply-templates select="." mode="pruning-keep"/>
374+
</xsl:when>
375+
<!-- Shift to aggressive pruning -->
376+
<xsl:otherwise>
377+
<xsl:apply-templates select="." mode="pruning-prune"/>
378+
</xsl:otherwise>
379+
</xsl:choose>
380+
</xsl:template>
381+
382+
<!-- Certain structural elements have data/metadata that we want to -->
383+
<!-- preserve, so we copy them over entirely. -->
384+
<xsl:template match="frontmatter|docinfo|bookinfo|backmatter" mode="pruning">
385+
<xsl:copy>
386+
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
387+
</xsl:copy>
388+
</xsl:template>
389+
390+
<!-- Preserve anything from here on down -->
391+
<xsl:template match="node()|@*" mode="pruning-keep">
392+
<xsl:copy>
393+
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
394+
</xsl:copy>
395+
</xsl:template>
396+
397+
<!-- Once aggressively pruning, only keep structural elements that give us -->
398+
<!-- numbering and toc info. -->
399+
<xsl:template match="&STRUCTURAL;|@*" mode="pruning-prune">
400+
<xsl:copy>
401+
<xsl:apply-templates select="&STRUCTURAL;|title|@*" mode="pruning-prune">
402+
</xsl:apply-templates>
403+
</xsl:copy>
404+
</xsl:template>
405+
406+
<!-- If we hit a title, grab all the contents so the ToC gets content-->
407+
<xsl:template match="title" mode="pruning-prune">
408+
<xsl:apply-templates select="." mode="pruning-keep"/>
409+
</xsl:template>
410+
352411
<!-- These templates initiate and create several iterations of -->
353412
<!-- the source tree via modal templates. Think of each as a -->
354413
<!-- "pass" through the source. Generally this constructs the -->
@@ -415,12 +474,43 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
415474
</xsl:variable>
416475
<xsl:variable name="original-labeled" select="exsl:node-set($original-labeled-rtf)"/>
417476

477+
<!-- See pretext-html for full version of these params -->
478+
<!-- "forward declared" here to avoid stylesheet ordering conflict -->
479+
<xsl:param name="subtree" select="''"/>
480+
<xsl:param name="html.build-preview" select="''"/>
481+
<!-- After labeling, but before further processing, build a -->
482+
<!-- pruned tree. Most of the time it is a copy of the labeled tree -->
483+
<!-- but when doing a preview build of a subtree, we prune -->
484+
<!-- down the labeled tree to the desired subtree and select other -->
485+
<!-- important elements. -->
486+
<xsl:variable name="pruning-tree-rtf">
487+
<xsl:choose>
488+
<xsl:when test="$subtree != '' and $html.build-preview = 'yes'">
489+
<xsl:variable name="target-node" select="$original-labeled//*[@xml:id = $subtree]"/>
490+
<xsl:apply-templates select="$original-labeled" mode="pruning">
491+
<xsl:with-param name="target-node" select="$original-labeled//*[@xml:id = $subtree]"/>
492+
</xsl:apply-templates>
493+
</xsl:when>
494+
<xsl:otherwise>
495+
<xsl:copy-of select="$original-labeled"/>
496+
</xsl:otherwise>
497+
</xsl:choose>
498+
</xsl:variable>
499+
<xsl:variable name="pruning-tree" select="exsl:node-set($pruning-tree-rtf)"/>
500+
501+
<!-- Variable just to allow some printing after pruning-tree is established -->
502+
<xsl:variable name="not-used-pruning-info">
503+
<xsl:if test="$subtree != '' and $html.build-preview = 'yes'">
504+
<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>
505+
</xsl:if>
506+
</xsl:variable>
507+
418508
<!-- A global list of all "webwork" used for -->
419509
<!-- efficient backward-compatible indentification -->
420-
<xsl:variable name="all-webwork" select="$original-labeled//webwork"/>
510+
<xsl:variable name="all-webwork" select="$pruning-tree//webwork"/>
421511

422512
<xsl:variable name="webwork-rtf">
423-
<xsl:apply-templates select="$original-labeled" mode="webwork"/>
513+
<xsl:apply-templates select="$pruning-tree" mode="webwork"/>
424514
</xsl:variable>
425515
<xsl:variable name="webworked" select="exsl:node-set($webwork-rtf)"/>
426516

0 commit comments

Comments
 (0)