Skip to content

Commit 2351139

Browse files
quick-dirty prunes source tree in assembly
1 parent d19248e commit 2351139

1 file changed

Lines changed: 80 additions & 9 deletions

File tree

xsl/pretext-assembly.xsl

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,63 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
260260
</xsl:copy>
261261
</xsl:template>
262262

263+
264+
<!-- Pruning templates generate a subtree towards $target-node -->
265+
<!-- that prunes as many other branches as possible while -->
266+
<!-- retaining halfway decent output of the desire tree. -->
267+
<xsl:template match="node()|@*" mode="pruning">
268+
<xsl:param name="target-node"/>
269+
<xsl:choose>
270+
<!-- This node contains the target node, copy and keep going -->
271+
<xsl:when test="count(descendant::*|$target-node) = count(descendant::*)">
272+
<xsl:copy>
273+
<xsl:apply-templates select="node()|@*" mode="pruning">
274+
<xsl:with-param name="target-node" select="$target-node"/>
275+
</xsl:apply-templates>
276+
</xsl:copy>
277+
</xsl:when>
278+
<!-- This node is target node, keep it all -->
279+
<xsl:when test="count(.|$target-node) = 1">
280+
<xsl:apply-templates select="." mode="pruning-keep"/>
281+
</xsl:when>
282+
<!-- Shift to aggressive pruning -->
283+
<xsl:otherwise>
284+
<xsl:apply-templates select="." mode="pruning-prune"/>
285+
</xsl:otherwise>
286+
</xsl:choose>
287+
</xsl:template>
288+
289+
<!-- If we hit frontmatter or docinfo, preserve all the content -->
290+
<!-- docinfo and bookinfo have important config info. frontmatter -->
291+
<!-- has authors/subtitle/etc... -->
292+
<xsl:template match="frontmatter|docinfo|bookinfo" mode="pruning">
293+
<xsl:copy>
294+
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
295+
</xsl:copy>
296+
</xsl:template>
297+
298+
<!-- Preserve anything from here on down -->
299+
<xsl:template match="node()|@*" mode="pruning-keep">
300+
<xsl:copy>
301+
<xsl:apply-templates select="node()|@*" mode="pruning-keep"/>
302+
</xsl:copy>
303+
</xsl:template>
304+
305+
<!-- Once aggressively pruning, only keep structural elements that give us -->
306+
<!-- numbering and toc info. We could prune these and lose that, but -->
307+
<!-- keeping them is cheap and looks a lot less broken on generated pages. -->
308+
<xsl:template match="&STRUCTURAL;|@*" mode="pruning-prune">
309+
<xsl:copy>
310+
<xsl:apply-templates select="&STRUCTURAL;|title|@*" mode="pruning-prune">
311+
</xsl:apply-templates>
312+
</xsl:copy>
313+
</xsl:template>
314+
315+
<!-- If we hit a title, grab all the contents so the ToC gets content-->
316+
<xsl:template match="title" mode="pruning-prune">
317+
<xsl:apply-templates select="." mode="pruning-keep"/>
318+
</xsl:template>
319+
263320
<!-- These templates initiate and create several iterations of -->
264321
<!-- the source tree via modal templates. Think of each as a -->
265322
<!-- "pass" through the source. Generally this constructs the -->
@@ -288,15 +345,6 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
288345
</xsl:variable>
289346
<xsl:variable name="version" select="exsl:node-set($version-rtf)"/>
290347

291-
<xsl:variable name="commentary-rtf">
292-
<xsl:apply-templates select="$version" mode="commentary"/>
293-
</xsl:variable>
294-
<xsl:variable name="commentaried" select="exsl:node-set($commentary-rtf)"/>
295-
296-
<!-- A global list of all "webwork" used for -->
297-
<!-- efficient backward-compatible indentification -->
298-
<xsl:variable name="all-webwork" select="$commentaried//webwork"/>
299-
300348
<!-- Support for versions mean there may be multiple instances of -->
301349
<!-- the same structure in authored source, and conceivably they -->
302350
<!-- might all be absent once a version has been selected. We are -->
@@ -307,6 +355,29 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
307355
<xsl:variable name="version-docinfo" select="$version-root/docinfo"/>
308356
<xsl:variable name="version-document-root" select="$version-root/*[not(self::docinfo)]"/>
309357

358+
<xsl:variable name="pruning-tree-rtf">
359+
<xsl:choose>
360+
<xsl:when test="$subtree != '' and $b-quick-dirty">
361+
<xsl:apply-templates select="$version" mode="pruning">
362+
<xsl:with-param name="target-node" select="//*[@id=$subtree]"/>
363+
</xsl:apply-templates>
364+
</xsl:when>
365+
<xsl:otherwise>
366+
<xsl:copy-of select="$version"/>
367+
</xsl:otherwise>
368+
</xsl:choose>
369+
</xsl:variable>
370+
<xsl:variable name="pruning-tree" select="exsl:node-set($pruning-tree-rtf)"/>
371+
372+
<xsl:variable name="commentary-rtf">
373+
<xsl:apply-templates select="$pruning-tree" mode="commentary"/>
374+
</xsl:variable>
375+
<xsl:variable name="commentaried" select="exsl:node-set($commentary-rtf)"/>
376+
377+
<!-- A global list of all "webwork" used for -->
378+
<!-- efficient backward-compatible indentification -->
379+
<xsl:variable name="all-webwork" select="$commentaried//webwork"/>
380+
310381
<xsl:variable name="webwork-rtf">
311382
<xsl:apply-templates select="$commentaried" mode="webwork"/>
312383
</xsl:variable>

0 commit comments

Comments
 (0)