From 7922e2a940f91b587c70dfb3d41e0048bc93a19e Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 1 Nov 2016 20:31:16 +0300 Subject: Fix for GH #97 : Preserve language name of fenced code block for highlight --- core/src/main/kotlin/Formats/HtmlFormatService.kt | 8 ++- core/src/main/kotlin/Kotlin/ContentBuilder.kt | 5 +- core/src/test/kotlin/TestAPI.kt | 5 +- core/src/test/kotlin/format/HtmlFormatTest.kt | 4 ++ core/src/test/kotlin/model/CommentTest.kt | 62 +++++++++++++++-------- 5 files changed, 60 insertions(+), 24 deletions(-) (limited to 'core/src') 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("${text.htmlEscape()}") } - override fun appendBlockCode(language: String, body: () -> Unit) = wrap("
", "
", body) + override fun appendBlockCode(language: String, body: () -> Unit) { + val openTags = if (language.isNotBlank()) + "
"
+        else
+            "
"
+        wrap(openTags, "
", 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()) } } } -- cgit