Bug 558314 - Set css class for ```language ``` markdown
Parse the language and set it as language-name css class so generated
html contains the css class as expected.
Change-Id: I40a4958b044b73ead011b374b55ec44a0d178700
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/main/java/org/eclipse/mylyn/wikitext/markdown/internal/block/CodeBlock.java b/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/main/java/org/eclipse/mylyn/wikitext/markdown/internal/block/CodeBlock.java
index 7d6d598..6465955 100644
--- a/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/main/java/org/eclipse/mylyn/wikitext/markdown/internal/block/CodeBlock.java
+++ b/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/main/java/org/eclipse/mylyn/wikitext/markdown/internal/block/CodeBlock.java
@@ -57,14 +57,20 @@
protected int processLineContent(String line, int offset) {
// start of block
if (blockLineCount == 0) {
- builder.beginBlock(BlockType.CODE, new Attributes());
+ Attributes attributes = new Attributes();
// if we have a fenced block, the first line will not contain contents
if (fencedBlock) {
+ String lang = line.substring(3).trim();
+ if (!lang.isEmpty()) {
+ attributes.setCssClass("language-" + lang);
+ }
+ builder.beginBlock(BlockType.CODE, attributes);
blockLineCount++;
return -1;
+ } else {
+ builder.beginBlock(BlockType.CODE, attributes);
}
}
-
Matcher matcher = fencedBlock
? FENCED_BLOCK.matcher(line.substring(offset))
: INDENTED_BLOCK.matcher(line.substring(offset));
diff --git a/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/test/java/org/eclipse/mylyn/internal/wikitext/markdown/tests/MarkdownLanguageTest.java b/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/test/java/org/eclipse/mylyn/internal/wikitext/markdown/tests/MarkdownLanguageTest.java
index f79f3c4..cbc36f4 100644
--- a/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/test/java/org/eclipse/mylyn/internal/wikitext/markdown/tests/MarkdownLanguageTest.java
+++ b/wikitext/core/org.eclipse.mylyn.wikitext.markdown/src/test/java/org/eclipse/mylyn/internal/wikitext/markdown/tests/MarkdownLanguageTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012, 2014 Stefan Seelmann and others.
+ * Copyright (c) 2012, 2019 Stefan Seelmann and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
@@ -92,7 +92,8 @@
assertTrue(html.contains("<pre><code>Code block"));
assertTrue(html.contains("<pre><code> Fenced Code block (Tildes)"));
assertTrue(html.contains("<pre><code> Fenced Code block (Backticks)"));
- assertTrue(html.contains("<strong>Some formatted text with an embedded anchor to <a href=\"#Header-2\" title=\"Header2 Title\">Header2</a></strong>"));
+ assertTrue(html.contains(
+ "<strong>Some formatted text with an embedded anchor to <a href=\"#Header-2\" title=\"Header2 Title\">Header2</a></strong>"));
assertTrue(html.contains("<ul>"));
assertTrue(html.contains("<li>List item 1</li>"));
assertTrue(html.contains("<li>List item 2</li>"));
@@ -116,6 +117,17 @@
assertTrue(html.contains("<p>AT&T again</p>"));
}
+ public void testBacktickWithLang() {
+ StringBuilder text = new StringBuilder();
+ text.append("```java\n");
+ text.append("new String();\n");
+ text.append("```");
+
+ String html = parseToHtml(text.toString());
+
+ assertEquals("<pre class=\"language-java\"><code class=\"language-java\">new String();</code></pre>", html);
+ }
+
public void testCreateDocumentBuilder() {
AbstractMarkupLanguage lang = new MarkdownLanguage();
DocumentBuilder builder = lang.createDocumentBuilder(new StringWriter());