Skip to content

Commit

Permalink
Add support for codeblock language prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <[email protected]>
  • Loading branch information
jelovirt committed Aug 18, 2024
1 parent 53ca716 commit 894e104
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ public class DitaRendererOptions {

public final boolean doNotRenderLinksInDocument;
public final String noLanguageClass;
public final String languageClassPrefix;

public DitaRendererOptions(DataHolder options) {
doNotRenderLinksInDocument = DitaRenderer.DO_NOT_RENDER_LINKS.get(options);
noLanguageClass = DitaRenderer.FENCED_CODE_NO_LANGUAGE_CLASS.get(options);
languageClassPrefix = DitaRenderer.FENCED_CODE_LANGUAGE_CLASS_PREFIX.get(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ private void render(final FencedCodeBlock node, final NodeRendererContext contex
} else {
language = info.subSequence(0, space);
}
atts.add("outputclass", /*context.getDitaOptions().languageClassPrefix +*/language.unescape());
atts.add("outputclass", context.getDitaOptions().languageClassPrefix + language.unescape());
} else {
String noLanguageClass = context.getDitaOptions().noLanguageClass.trim();
if (!noLanguageClass.isEmpty()) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/resources/ast2markdown.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,21 @@
</xsl:apply-templates>
</xsl:template>

<xsl:variable name="codeblock-languge-prefix" as="xs:string?" select="'language-'"/>

<xsl:template match="codeblock" mode="ast">
<xsl:param name="indent" tunnel="yes" as="xs:string" select="''"/>

<xsl:variable name="classes" select="tokenize(normalize-space(@class), '\s+')" as="xs:string*"/>

<xsl:value-of select="$indent"/>
<xsl:text>```</xsl:text>
<xsl:choose>
<xsl:when test="empty(@id) and @class and not(contains(@class, ' '))">
<xsl:value-of select="@class"/>
<xsl:when test="exists($codeblock-languge-prefix) and count($classes) eq 1 and starts-with($classes, $codeblock-languge-prefix)">
<xsl:value-of select="substring-after($classes, $codeblock-languge-prefix)"/>
</xsl:when>
<xsl:when test="empty(@id) and count($classes) eq 1">
<xsl:value-of select="$classes"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="ast-attibutes"/>
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/ast/codeblock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<para>Fenced block:</para>
<codeblock xml:space="preserve" class="scala" id="codeblock__foreach-example">items.foreach(println)
</codeblock>
<para>Syntax highlighting:</para>
<codeblock xml:space="preserve" class="language-js">function add(a, b) {
return a + b;
}</codeblock>
</div>
</div>
</pandoc>
4 changes: 4 additions & 0 deletions src/test/resources/dita/codeblock.dita
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
<p class="- topic/p ">Fenced block:</p>
<codeblock class="+ topic/pre pr-d/codeblock " id="foreach-example" outputclass="scala" xml:space="preserve">items.foreach(println)
</codeblock>
<p class="- topic/p ">Syntax highlighting:</p>
<codeblock class="+ topic/pre pr-d/codeblock " outputclass="language-js" xml:space="preserve">function add(a, b) {
return a + b;
}</codeblock>
</body>
</topic>
4 changes: 4 additions & 0 deletions src/test/resources/hdita/codeblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ <h1>Codeblock </h1>
println(i)</tt></pre>
<p>Fenced block:</p>
<pre id="foreach-example" class="scala"><tt>items.foreach(println)</tt></pre>
<p>Syntax highlighting:</p>
<pre class="language-js"><tt>function add(a, b) {
return a + b;
}</tt></pre>
</article>
4 changes: 4 additions & 0 deletions src/test/resources/html/codeblock.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ <h1>Codeblock </h1>
println(i)</code></pre>
<p>Fenced block:</p>
<pre id="foreach-example" class="scala"><code>items.foreach(println)</code></pre>
<p>Syntax highlighting:</p>
<pre class="language-js"><code>function add(a, b) {
return a + b;
}</code></pre>
</article>
8 changes: 8 additions & 0 deletions src/test/resources/markdown/codeblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ Fenced block:
```{.scala #foreach-example}
items.foreach(println)
```

Syntax highlighting:

```js
function add(a, b) {
return a + b;
}
```
4 changes: 3 additions & 1 deletion src/test/resources/output/ast/codeblock.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?><pandoc><div><header id="codeblock" level="1">Codeblock</header><div><para>Code example on <code>for</code> loop:</para><codeblock xml:space="preserve">for i in items:
println(i)
</codeblock><para>Fenced block:</para><codeblock class="scala" id="foreach-example" xml:space="preserve">items.foreach(println)
</codeblock></div></div></pandoc>
</codeblock><para>Syntax highlighting:</para><codeblock xml:space="preserve" class="language-js">function add(a, b) {
return a + b;
}</codeblock></div></div></pandoc>
8 changes: 8 additions & 0 deletions src/test/resources/output/markdown/codeblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ items.foreach(println)
```

Syntax highlighting:

```js
function add(a, b) {
return a + b;
}
```

4 changes: 4 additions & 0 deletions src/test/resources/xdita/codeblock.dita
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
println(i)</tt></pre>
<p class="- topic/p ">Fenced block:</p>
<pre class="- topic/pre " id="foreach-example" outputclass="scala" xml:space="preserve"><tt class="+ topic/ph hi-d/tt ">items.foreach(println)</tt></pre>
<p class="- topic/p ">Syntax highlighting:</p>
<pre class="- topic/pre " outputclass="language-js" xml:space="preserve"><tt class="+ topic/ph hi-d/tt ">function add(a, b) {
return a + b;
}</tt></pre>
</body>
</topic>

0 comments on commit 894e104

Please sign in to comment.