diff options
author | Simon Ogorodnik <Simon.Ogorodnik@jetbrains.com> | 2016-11-01 20:31:16 +0300 |
---|---|---|
committer | Simon Ogorodnik <Simon.Ogorodnik@jetbrains.com> | 2016-11-01 20:31:16 +0300 |
commit | 7922e2a940f91b587c70dfb3d41e0048bc93a19e (patch) | |
tree | 28055ed18f6eac89419e75f3e6fd18f05ba0bcf1 | |
parent | ee824b90249fd1d6d868af998c6826df31ab557b (diff) | |
download | dokka-7922e2a940f91b587c70dfb3d41e0048bc93a19e.tar.gz dokka-7922e2a940f91b587c70dfb3d41e0048bc93a19e.tar.bz2 dokka-7922e2a940f91b587c70dfb3d41e0048bc93a19e.zip |
Fix for GH #97 : Preserve language name of fenced code block for highlight
-rw-r--r-- | core/src/main/kotlin/Formats/HtmlFormatService.kt | 8 | ||||
-rw-r--r-- | core/src/main/kotlin/Kotlin/ContentBuilder.kt | 5 | ||||
-rw-r--r-- | core/src/test/kotlin/TestAPI.kt | 5 | ||||
-rw-r--r-- | core/src/test/kotlin/format/HtmlFormatTest.kt | 4 | ||||
-rw-r--r-- | core/src/test/kotlin/model/CommentTest.kt | 62 | ||||
-rw-r--r-- | core/testdata/comments/codeBlockComment.kt | 14 | ||||
-rw-r--r-- | core/testdata/format/codeBlock.html | 20 | ||||
-rw-r--r-- | core/testdata/format/codeBlock.kt | 9 | ||||
-rw-r--r-- | core/testdata/format/codeBlock.md | 12 |
9 files changed, 113 insertions, 26 deletions
diff --git a/core/src/main/kotlin/Formats/HtmlFormatService.kt b/core/src/main/kotlin/Formats/HtmlFormatService.kt index de41d4c6..d73e4e62 100644 --- a/core/src/main/kotlin/Formats/HtmlFormatService.kt +++ b/core/src/main/kotlin/Formats/HtmlFormatService.kt @@ -31,7 +31,13 @@ open class HtmlOutputBuilder(to: StringBuilder, to.append("<span class=\"identifier\"$id>${text.htmlEscape()}</span>") } - override fun appendBlockCode(language: String, body: () -> Unit) = wrap("<pre><code>", "</code></pre>", body) + override fun appendBlockCode(language: String, body: () -> Unit) { + val openTags = if (language.isNotBlank()) + "<pre><code class=\"lang-$language\">" + else + "<pre><code>" + wrap(openTags, "</code></pre>", body) + } override fun appendHeader(level: Int, body: () -> Unit) = wrapInTag("h$level", body, newlineBeforeOpen = true, newlineAfterClose = true) diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt index 61c67787..53afebaf 100644 --- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt +++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt @@ -51,7 +51,10 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri } } MarkdownElementTypes.CODE_BLOCK, - MarkdownElementTypes.CODE_FENCE -> appendNodeWithChildren(ContentBlockCode()) + MarkdownElementTypes.CODE_FENCE -> { + val language = node.child(MarkdownTokenTypes.FENCE_LANG)?.text?.trim() ?: "" + appendNodeWithChildren(ContentBlockCode(language)) + } MarkdownElementTypes.PARAGRAPH -> appendNodeWithChildren(ContentParagraph()) MarkdownElementTypes.INLINE_LINK -> { diff --git a/core/src/test/kotlin/TestAPI.kt b/core/src/test/kotlin/TestAPI.kt index e13a61f8..43a335fc 100644 --- a/core/src/test/kotlin/TestAPI.kt +++ b/core/src/test/kotlin/TestAPI.kt @@ -175,7 +175,10 @@ fun StringBuilder.appendNode(node: ContentNode): StringBuilder { } is ContentEmphasis -> append("*").appendChildren(node).append("*") is ContentBlockCode -> { - appendln("[code]") + if (node.language.isNotBlank()) + appendln("[code lang=${node.language}]") + else + appendln("[code]") appendChildren(node) appendln() appendln("[/code]") diff --git a/core/src/test/kotlin/format/HtmlFormatTest.kt b/core/src/test/kotlin/format/HtmlFormatTest.kt index 2f92366b..12df7c44 100644 --- a/core/src/test/kotlin/format/HtmlFormatTest.kt +++ b/core/src/test/kotlin/format/HtmlFormatTest.kt @@ -76,6 +76,10 @@ class HtmlFormatTest { } } + @Test fun codeBlock() { + verifyHtmlNode("codeBlock") + } + @Test fun javaLinkTag() { verifyJavaHtmlNode("javaLinkTag") } diff --git a/core/src/test/kotlin/model/CommentTest.kt b/core/src/test/kotlin/model/CommentTest.kt index 457affee..55fa3fc0 100644 --- a/core/src/test/kotlin/model/CommentTest.kt +++ b/core/src/test/kotlin/model/CommentTest.kt @@ -5,6 +5,26 @@ import org.junit.Assert.* import org.jetbrains.dokka.* public class CommentTest { + + @Test fun codeBlockComment() { + verifyModel("testdata/comments/codeBlockComment.kt") { model -> + with(model.members.single().members.first()) { + assertEquals("""[code lang=brainfuck] + |++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. + |[/code] + |""".trimMargin(), + content.toTestString()) + } + with(model.members.single().members.last()) { + assertEquals("""[code] + |a + b - c + |[/code] + |""".trimMargin(), + content.toTestString()) + } + } + } + @Test fun emptyDoc() { verifyModel("testdata/comments/emptyDoc.kt") { model -> with(model.members.single().members.single()) { @@ -133,27 +153,27 @@ line two""", toTestString()) with(model.members.single().members.first()) { assertEquals("Summary", content.summary.toTestString()) with (content.description) { - assertEqualsIgnoringSeparators("""[code] -if (true) { - println(property) -} -[/code] -[code] -if (true) { - println(property) -} -[/code] -[code] -if (true) { - println(property) -} -[/code] -[code] -if (true) { - println(property) -} -[/code] -""", toTestString()) + assertEqualsIgnoringSeparators("""[code lang=kotlin] + |if (true) { + | println(property) + |} + |[/code] + |[code lang=kotlin] + |if (true) { + | println(property) + |} + |[/code] + |[code lang=kotlin] + |if (true) { + | println(property) + |} + |[/code] + |[code lang=kotlin] + |if (true) { + | println(property) + |} + |[/code] + |""".trimMargin(), toTestString()) } } } diff --git a/core/testdata/comments/codeBlockComment.kt b/core/testdata/comments/codeBlockComment.kt new file mode 100644 index 00000000..aa5f5ffc --- /dev/null +++ b/core/testdata/comments/codeBlockComment.kt @@ -0,0 +1,14 @@ +/** + * ``` brainfuck + * ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. + * ``` + */ +val prop1 = "" + + +/** + * ``` + * a + b - c + * ``` + */ +val prop2 = ""
\ No newline at end of file diff --git a/core/testdata/format/codeBlock.html b/core/testdata/format/codeBlock.html new file mode 100644 index 00000000..b3b65dba --- /dev/null +++ b/core/testdata/format/codeBlock.html @@ -0,0 +1,20 @@ +<HTML> +<HEAD> +<meta charset="UTF-8"> +</HEAD> +<BODY> +<a href="test/index">test</a> / <a href="test/-throws/index">Throws</a><br/> +<br/> +<h1>Throws</h1> +<code><span class="keyword">class </span><span class="identifier">Throws</span></code> +<p>This annotation indicates what exceptions should be declared by a function when compiled to a JVM method.</p> +<p>Example:</p> +<pre><code>Throws(IOException::class) +fun readFile(name: String): String {...}</code></pre><a href="test/index">test</a> / <a href="test/-it-does-some-obfuscated-thing/index">ItDoesSomeObfuscatedThing</a><br/> +<br/> +<h1>ItDoesSomeObfuscatedThing</h1> +<code><span class="keyword">class </span><span class="identifier">ItDoesSomeObfuscatedThing</span></code> +<p>Check output of</p> +<pre><code class="lang-brainfuck">++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.</code></pre> +</BODY> +</HTML> diff --git a/core/testdata/format/codeBlock.kt b/core/testdata/format/codeBlock.kt index e3fa27d2..633bf414 100644 --- a/core/testdata/format/codeBlock.kt +++ b/core/testdata/format/codeBlock.kt @@ -11,3 +11,12 @@ import kotlin.reflect.KClass * ``` */ class Throws + + +/** + * Check output of + * ``` brainfuck + * ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. + * ``` + */ +class ItDoesSomeObfuscatedThing
\ No newline at end of file diff --git a/core/testdata/format/codeBlock.md b/core/testdata/format/codeBlock.md index 016ea9e8..55a054f6 100644 --- a/core/testdata/format/codeBlock.md +++ b/core/testdata/format/codeBlock.md @@ -13,7 +13,15 @@ Throws(IOException::class) fun readFile(name: String): String {...} ``` -### Constructors +[test](test/index) / [ItDoesSomeObfuscatedThing](test/-it-does-some-obfuscated-thing/index) -| [<init>](test/-throws/-init-) | `Throws()`<br>This annotation indicates what exceptions should be declared by a function when compiled to a JVM method. | +# ItDoesSomeObfuscatedThing + +`class ItDoesSomeObfuscatedThing` + +Check output of + +``` brainfuck +++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>. +``` |