I'm trying to process plain text to turn it into XML/DITA <p> and <pre> elements. The idea is that consecutive lines of text with indents of exactly n spaces should be grouped into a <p> element, whereas lines with either fewer or more spaces before non-whitespace content should be grouped into <pre> elements.
I've come up with the following template that does the job for a specific indent, in this case 15 spaces, but I haven't figured out how to support an indent defined by my
indent parameter. Basically what I want is to dynamically create my regular expression with the correct indent value inserted where I currently have the value
15 hard-coded:
Code:
<xsl:template name="convertFixedIndentToParagraphs">
<xsl:param name="text"/>
<xsl:param name="indent"/>
<xsl:analyze-string select="$text" regex="(^ {{15}}[^ ][^\n]*\n?)+" flags="m">
<xsl:matching-substring>
<p>
<xsl:for-each select="tokenize(., '\n')">
<xsl:text>#10;</xsl:text>
<xsl:value-of select="substring(., $indent + 1)"/>
</xsl:for-each>
</p>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:if test="matches(., '\S')">
<pre><xsl:text>#10;</xsl:text>
<xsl:call-template name="eliminateMinimumIndent"/></pre>
</xsl:if>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
I really thought I had this working well enough with the hard-coded indent value, until I discovered that many of the text nodes I'm processing have slightly different standard indents, so I
need to be able to use the
indent parameter properly.
Is there an easy way to parameterize that value in my regex? Or am I going to have to come up with a completely different solution?
Ian