Skip to content

Commit 75870fb

Browse files
committed
Minor fixes to xml2md.xsl
- Remove outdated comment - Fix code block delimiter
1 parent 0707ada commit 75870fb

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

tools/xml2md.xsl

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Since the XML output is lossy, a lossless MD->XML->MD roundtrip isn't
1010
possible. The XML->MD->XML roundtrip should produce the original XML,
1111
though.
1212
13-
HTML blocks and inlines aren't supported.
14-
1513
Example usage with xsltproc:
1614
1715
cmark -t xml doc.md | xsltproc -novalid xml2md.xsl -
@@ -123,15 +121,23 @@ Example usage with xsltproc:
123121

124122
<xsl:template match="md:code_block">
125123
<xsl:apply-templates select="." mode="indent-block"/>
126-
<!-- TODO: Longer delimiter if text contains ``` -->
127-
<xsl:text>```</xsl:text>
124+
125+
<xsl:variable name="t" select="string(.)"/>
126+
<xsl:variable name="delim">
127+
<xsl:call-template name="code-delim">
128+
<xsl:with-param name="text" select="$t"/>
129+
<xsl:with-param name="delim" select="'```'"/>
130+
</xsl:call-template>
131+
</xsl:variable>
132+
133+
<xsl:value-of select="$delim"/>
128134
<xsl:value-of select="@info"/>
129135
<xsl:text>&#10;</xsl:text>
130136
<xsl:call-template name="indent-lines">
131-
<xsl:with-param name="code" select="."/>
137+
<xsl:with-param name="code" select="$t"/>
132138
</xsl:call-template>
133139
<xsl:apply-templates select="ancestor::md:*" mode="indent"/>
134-
<xsl:text>```</xsl:text>
140+
<xsl:value-of select="$delim"/>
135141
<xsl:text>&#10;</xsl:text>
136142
</xsl:template>
137143

@@ -228,28 +234,16 @@ Example usage with xsltproc:
228234
<!-- Inline code -->
229235

230236
<xsl:template match="md:code">
231-
<xsl:call-template name="escape-code">
232-
<xsl:with-param name="text" select="string(.)"/>
233-
</xsl:call-template>
234-
</xsl:template>
235-
236-
<xsl:template name="escape-code">
237-
<xsl:param name="text"/>
238-
<xsl:param name="delim" select="'`'"/>
239-
240-
<xsl:choose>
241-
<xsl:when test="contains($text, $delim)">
242-
<xsl:call-template name="escape-code">
243-
<xsl:with-param name="text" select="$text"/>
244-
<xsl:with-param name="delim" select="concat($delim, '`')"/>
245-
</xsl:call-template>
246-
</xsl:when>
247-
<xsl:otherwise>
248-
<xsl:value-of select="$delim"/>
249-
<xsl:value-of select="$text"/>
250-
<xsl:value-of select="$delim"/>
251-
</xsl:otherwise>
252-
</xsl:choose>
237+
<xsl:variable name="t" select="string(.)"/>
238+
<xsl:variable name="delim">
239+
<xsl:call-template name="code-delim">
240+
<xsl:with-param name="text" select="$t"/>
241+
<xsl:with-param name="delim" select="'`'"/>
242+
</xsl:call-template>
243+
</xsl:variable>
244+
<xsl:value-of select="$delim"/>
245+
<xsl:value-of select="$t"/>
246+
<xsl:value-of select="$delim"/>
253247
</xsl:template>
254248

255249
<!-- Links and images -->
@@ -280,7 +274,7 @@ Example usage with xsltproc:
280274
<xsl:value-of select="."/>
281275
</xsl:template>
282276

283-
<!-- Escape text -->
277+
<!-- Escaping helpers -->
284278

285279
<xsl:template name="escape-text">
286280
<xsl:param name="text"/>
@@ -305,4 +299,21 @@ Example usage with xsltproc:
305299
</xsl:choose>
306300
</xsl:template>
307301

302+
<xsl:template name="code-delim">
303+
<xsl:param name="text"/>
304+
<xsl:param name="delim"/>
305+
306+
<xsl:choose>
307+
<xsl:when test="contains($text, $delim)">
308+
<xsl:call-template name="code-delim">
309+
<xsl:with-param name="text" select="$text"/>
310+
<xsl:with-param name="delim" select="concat($delim, '`')"/>
311+
</xsl:call-template>
312+
</xsl:when>
313+
<xsl:otherwise>
314+
<xsl:value-of select="$delim"/>
315+
</xsl:otherwise>
316+
</xsl:choose>
317+
</xsl:template>
318+
308319
</xsl:stylesheet>

0 commit comments

Comments
 (0)