<xsl:template match="pre|highlight">
<xsl:text>\begin{verbatim}</xsl:text>
-<xsl:call-template name="pre-rtrim">
+<!-- If it's a one-liner, trim the initial indentation as well -->
+<!-- it's most likely an accident -->
+<xsl:call-template name="pre-ltrim-one">
<xsl:with-param name="string">
- <xsl:call-template name="pre-ltrim">
+ <xsl:call-template name="pre-rtrim">
<xsl:with-param name="string">
- <xsl:value-of select="." />
+ <xsl:call-template name="pre-ltrim">
+ <xsl:with-param name="string">
+ <xsl:value-of select="." />
+ </xsl:with-param>
+ </xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:with-param>
<xsl:choose>
<!-- Simple case: only one text node -->
<xsl:when test="node()[position() = 1 and self::text()] and count(node()) = 1">
- <xsl:call-template name="pre-rtrim">
+ <xsl:call-template name="pre-ltrim-one">
<xsl:with-param name="string">
- <xsl:call-template name="pre-ltrim">
- <xsl:with-param name="string"
- select="node()[position() = 1 and self::text()]" />
+ <xsl:call-template name="pre-rtrim">
+ <xsl:with-param name="string">
+ <xsl:call-template name="pre-ltrim">
+ <xsl:with-param name="string"
+ select="node()[position() = 1 and self::text()]" />
+ </xsl:call-template>
+ </xsl:with-param>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:choose>
</xsl:variable>
+ <xsl:variable name="out">
+ <xsl:apply-templates select="node()[position() >= $from and position() <= $to]" />
+ </xsl:variable>
+
<xsl:if test="$from = 2">
- <xsl:call-template name="pre-ltrim">
- <xsl:with-param name="string"
- select="node()[position() = 1 and self::text()]" />
- </xsl:call-template>
+ <xsl:choose>
+ <xsl:when test="contains($out, '
')">
+ <xsl:call-template name="pre-ltrim">
+ <xsl:with-param name="string"
+ select="node()[position() = 1 and self::text()]" />
+ </xsl:call-template>
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:variable name="tmp" select="node()[position() = 1 and self::text()]" />
+ <xsl:value-of select="substring($tmp, string-length(substring-before($tmp, substring(normalize-space($tmp), 1, 1))) + 1, string-length($tmp))" />
+ </xsl:otherwise>
+ </xsl:choose>
</xsl:if>
<xsl:apply-templates select="node()[position() >= $from and position() <= $to]" />
<xsl:include href="string-reverse.xsl" />
+<!-- strip whitespace at the beginning if one-liner -->
+<xsl:template name="pre-ltrim-one">
+<xsl:param name="string" />
+
+<xsl:choose>
+<xsl:when test="contains($string, '
')">
+ <xsl:value-of select="$string" />
+</xsl:when>
+<xsl:otherwise>
+ <xsl:value-of select="substring($string, string-length(substring-before($string, substring(normalize-space($string), 1, 1))) + 1, string-length($string))" />
+</xsl:otherwise>
+</xsl:choose>
+</xsl:template>
+
+
<!-- strip empty lines at the beginning -->
<xsl:template name="pre-ltrim">
<xsl:param name="string" />