summaryrefslogtreecommitdiff
path: root/tangle-bootstrap.xsl
diff options
context:
space:
mode:
Diffstat (limited to 'tangle-bootstrap.xsl')
-rw-r--r--tangle-bootstrap.xsl111
1 files changed, 111 insertions, 0 deletions
diff --git a/tangle-bootstrap.xsl b/tangle-bootstrap.xsl
new file mode 100644
index 0000000..ffe19f5
--- /dev/null
+++ b/tangle-bootstrap.xsl
@@ -0,0 +1,111 @@
+<?xml version="1.0"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mped="https://labo.planete-kraus.eu/mped.git" xmlns:docbook="http://docbook.org/ns/docbook" version="1.0">
+ <xsl:output method="text" indent="no"/>
+ <xsl:strip-space elements="*"/>
+ <xsl:template match="docbook:programlisting[@mped:tangle-to]">
+ <xsl:text>mkdir -p $(dirname "</xsl:text>
+ <xsl:value-of select="@mped:tangle-to"/>
+ <xsl:text>")
+</xsl:text>
+ <xsl:text>cat &gt;&gt; </xsl:text>
+ <xsl:value-of select="@mped:tangle-to"/>
+ <xsl:text> &lt;&lt; "_MPED_EOF"
+</xsl:text>
+ <xsl:apply-templates mode="copy-source-code"/>
+ <xsl:text>
+_MPED_EOF
+</xsl:text>
+ </xsl:template>
+ <xsl:template name="mped-private-leading-empty-lines">
+ <xsl:param name="indentation" select="''"/>
+ <xsl:param name="text" select="."/>
+ <xsl:choose>
+ <xsl:when test="substring($text, 1, 1) = '&#10;'">
+ <xsl:call-template name="mped-private-leading-empty-lines">
+ <xsl:with-param name="indentation" select="''"/>
+ <xsl:with-param name="text" select="substring($text, 2)"/>
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:when test="( substring($text, 1, 1) = ' ' or substring($text, 1, 1) = '&#9;' or substring($text, 1, 1) = '&#13;')">
+ <xsl:call-template name="mped-private-leading-empty-lines">
+ <xsl:with-param name="indentation" select="concat($indentation, substring($text, 1, 1))"/>
+ <xsl:with-param name="text" select="substring($text, 2)"/>
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="$indentation"/>
+ <xsl:value-of select="$text"/>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+ <xsl:template name="remove-leading-empty-lines">
+ <xsl:param name="text" select="."/>
+ <xsl:call-template name="mped-private-leading-empty-lines">
+ <xsl:with-param name="indentation" select="''"/>
+ <xsl:with-param name="text" select="$text"/>
+ </xsl:call-template>
+ </xsl:template>
+ <xsl:template name="remove-trailing-whitespace">
+ <xsl:param name="text" select="."/>
+ <xsl:choose>
+ <xsl:when test="$text = ''">
+ <xsl:value-of select="$text"/>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:variable name="last" select="substring($text, string-length ($text), 1)"/>
+ <xsl:variable name="before" select="substring($text, 1, string-length ($text) - 1)"/>
+ <xsl:choose>
+ <xsl:when test="$last = ' ' or $last = '&#9;' or $last = '&#10;' or $last = '&#13;'">
+ <xsl:call-template name="remove-trailing-whitespace">
+ <xsl:with-param name="text" select="$before"/>
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="$text"/>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:template>
+ <xsl:template match="text()[position() = 1 and position() = last()]" mode="copy-source-code">
+ <xsl:call-template name="remove-trailing-whitespace">
+ <xsl:with-param name="text">
+ <xsl:call-template name="remove-leading-empty-lines">
+ <xsl:with-param name="text" select="."/>
+ </xsl:call-template>
+ </xsl:with-param>
+ </xsl:call-template>
+ </xsl:template>
+ <xsl:template match="text()[position() = 1 and position() != last()]" mode="copy-source-code">
+ <xsl:call-template name="remove-leading-empty-lines">
+ <xsl:with-param name="text" select="."/>
+ </xsl:call-template>
+ </xsl:template>
+ <xsl:template match="text()[position() &gt; 1 and position() = last()]" mode="copy-source-code">
+ <xsl:call-template name="remove-trailing-whitespace">
+ <xsl:with-param name="text" select="."/>
+ </xsl:call-template>
+ </xsl:template>
+ <xsl:template match="text()"/>
+ <xsl:template match="mped:copy" mode="copy-source-code">
+ <xsl:variable name="ref" select="@linkend"/>
+ <xsl:variable name="candidates" select="count(//docbook:programlisting[@xml:id = $ref])"/>
+ <xsl:if test="$candidates = 0">
+ <xsl:message terminate="yes">
+ <xsl:text>There are no listing with ID '</xsl:text>
+ <xsl:value-of select="$ref"/>
+ <xsl:text>'.
+</xsl:text>
+ </xsl:message>
+ </xsl:if>
+ <xsl:if test="$candidates &gt; 1">
+ <xsl:message terminate="yes">
+ <xsl:text>There are multiple listings with ID '</xsl:text>
+ <xsl:value-of select="$ref"/>
+ <xsl:text>'.
+</xsl:text>
+ </xsl:message>
+ </xsl:if>
+ <xsl:apply-templates select="//docbook:programlisting[@xml:id = $ref]" mode="copy-source"/>
+ </xsl:template>
+</xsl:stylesheet>