Wrap pre-formatted example code in <pre> elements. This works
similar to markdown code blocks.
Every line indented with at least 2 spaces is considered a code
block and gets wrapped in a <pre> tag.
Signed-off-by: Claudio Bley <cbley(a)av-test.de>
---
docs/newapi.xsl | 89 +++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 67 insertions(+), 22 deletions(-)
diff --git a/docs/newapi.xsl b/docs/newapi.xsl
index 09395ea..b11ce9c 100644
--- a/docs/newapi.xsl
+++ b/docs/newapi.xsl
@@ -83,6 +83,60 @@
</xsl:for-each>
</xsl:template>
+
+ <!-- process blocks of text. blocks are separated by two consecutive line -->
+ <!-- breaks. -->
+ <!-- -->
+ <!-- blocks indented with at least 2 spaces are considered code blocks. -->
+ <!-- -->
+ <!-- consecutive code blocks are collapsed into a single code block. -->
+ <xsl:template name="formatblock">
+ <xsl:param name="block"/>
+ <xsl:param name="rest"/>
+
+ <xsl:variable name="multipleCodeBlocks"
+ select="starts-with($block, ' ') and starts-with($rest,
' ')"/>
+
+ <xsl:choose>
+ <xsl:when test="$multipleCodeBlocks">
+ <xsl:call-template name="formatblock">
+ <xsl:with-param name="block">
+ <xsl:choose>
+ <xsl:when test="contains($rest,
'

')">
+ <xsl:value-of select="concat($block, '


',
+ substring-before($rest,
'

'))" />
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="concat($block, '


', $rest)" />
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:with-param>
+ <xsl:with-param name="rest" select="substring-after($rest,
'

')"/>
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:when test="starts-with($block, ' ')">
+ <pre class="code"><xsl:for-each
select="str:tokenize($block, '
')">
+ <xsl:value-of select="substring(., 3)"/>
+ <xsl:if test="position() != last()">
+ <xsl:text>
</xsl:text>
+ </xsl:if>
+ </xsl:for-each></pre>
+ </xsl:when>
+ <xsl:otherwise>
+ <p>
+ <xsl:call-template name="dumptext">
+ <xsl:with-param name="text" select="$block"/>
+ </xsl:call-template>
+ </p>
+ </xsl:otherwise>
+ </xsl:choose>
+ <xsl:if test="not($multipleCodeBlocks)">
+ <xsl:call-template name="formattext">
+ <xsl:with-param name="text" select="$rest"/>
+ </xsl:call-template>
+ </xsl:if>
+ </xsl:template>
+
<xsl:template name="formattext">
<xsl:param name="text" />
@@ -90,28 +144,19 @@
<xsl:variable name="head" select="substring-before($text,
'

')"/>
<xsl:variable name="rest" select="substring-after($text,
'

')"/>
- <xsl:choose>
- <xsl:when test="$head">
- <p>
- <xsl:call-template name="dumptext">
- <xsl:with-param name="text" select="$head"/>
- </xsl:call-template>
- </p>
- </xsl:when>
- <xsl:when test="not($rest)">
- <p>
- <xsl:call-template name="dumptext">
- <xsl:with-param name="text" select="$text"/>
- </xsl:call-template>
- </p>
- </xsl:when>
- </xsl:choose>
-
- <xsl:if test="$rest">
- <xsl:call-template name="formattext">
- <xsl:with-param name="text" select="$rest"/>
- </xsl:call-template>
- </xsl:if>
+ <xsl:call-template name="formatblock">
+ <xsl:with-param name="block">
+ <xsl:choose>
+ <xsl:when test="contains($text,
'

')">
+ <xsl:value-of select="$head"/>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="$text"/>
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:with-param>
+ <xsl:with-param name="rest" select="$rest"/>
+ </xsl:call-template>
</xsl:if>
</xsl:template>
--
1.7.9.5