Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for codeblock language prefix #228

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 11 additions & 2 deletions src/main/resources/ast2markdown.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
exclude-result-prefixes="xs ast"
version="2.0">

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

<xsl:variable name="linefeed" as="xs:string" select="'&#xA;'"/>

<!-- Block -->
Expand Down Expand Up @@ -123,11 +125,18 @@

<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 string-length($codeblock-languge-prefix) ne 0 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>
Loading