diff options
author | Dmitry Jemerov <yole@jetbrains.com> | 2016-07-06 14:39:31 +0200 |
---|---|---|
committer | Dmitry Jemerov <yole@jetbrains.com> | 2016-07-06 14:39:31 +0200 |
commit | 0f65cf0e4ce531715b76ac32a36a3dff488b2d70 (patch) | |
tree | 3682da97a5a1eb105f6319a0afc141af4a4fa6a1 /core/src | |
parent | b5367f295be47f2f6ad26f113d80362102b6c36d (diff) | |
download | dokka-0f65cf0e4ce531715b76ac32a36a3dff488b2d70.tar.gz dokka-0f65cf0e4ce531715b76ac32a36a3dff488b2d70.tar.bz2 dokka-0f65cf0e4ce531715b76ac32a36a3dff488b2d70.zip |
handle code blocks escaped with multiple backticks correctly; generate extra backticks if text in code block contains backticks (KT-12998, KT-12999)
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/main/kotlin/Formats/MarkdownFormatService.kt | 16 | ||||
-rw-r--r-- | core/src/main/kotlin/Kotlin/ContentBuilder.kt | 8 | ||||
-rw-r--r-- | core/src/test/kotlin/format/MarkdownFormatTest.kt | 4 |
3 files changed, 27 insertions, 1 deletions
diff --git a/core/src/main/kotlin/Formats/MarkdownFormatService.kt b/core/src/main/kotlin/Formats/MarkdownFormatService.kt index 6f2ab327..2e3c9f6d 100644 --- a/core/src/main/kotlin/Formats/MarkdownFormatService.kt +++ b/core/src/main/kotlin/Formats/MarkdownFormatService.kt @@ -19,6 +19,7 @@ open class MarkdownOutputBuilder(to: StringBuilder, protected var inTableCell = false protected var inCodeBlock = false private var lastTableCellStart = -1 + private var maxBackticksInCodeBlock = 0 private fun appendNewline() { while (to.endsWith(' ')) { @@ -64,9 +65,14 @@ open class MarkdownOutputBuilder(to: StringBuilder, to.append(" / ") } + private val backTickFindingRegex = """(`+)""".toRegex() + override fun appendText(text: String) { if (inCodeBlock) { to.append(text) + val backTicks = backTickFindingRegex.findAll(text) + val longestBackTickRun = backTicks.map { it.value.length }.max() ?: 0 + maxBackticksInCodeBlock = maxBackticksInCodeBlock.coerceAtLeast(longestBackTickRun) } else { to.append(text.htmlEscape()) @@ -75,7 +81,17 @@ open class MarkdownOutputBuilder(to: StringBuilder, override fun appendCode(body: () -> Unit) { inCodeBlock = true + val codeBlockStart = to.length + maxBackticksInCodeBlock = 0 + wrapIfNotEmpty("`", "`", body, checkEndsWith = true) + + if (maxBackticksInCodeBlock > 0) { + val extraBackticks = "`".repeat(maxBackticksInCodeBlock) + to.insert(codeBlockStart, extraBackticks) + to.append(extraBackticks) + } + inCodeBlock = false } diff --git a/core/src/main/kotlin/Kotlin/ContentBuilder.kt b/core/src/main/kotlin/Kotlin/ContentBuilder.kt index c16b19c5..7bee6936 100644 --- a/core/src/main/kotlin/Kotlin/ContentBuilder.kt +++ b/core/src/main/kotlin/Kotlin/ContentBuilder.kt @@ -43,7 +43,12 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri MarkdownElementTypes.EMPH -> appendNodeWithChildren(ContentEmphasis()) MarkdownElementTypes.STRONG -> appendNodeWithChildren(ContentStrong()) MarkdownElementTypes.CODE_SPAN -> { - appendNodeWithChildren(ContentCode()) + val startDelimiter = node.child(MarkdownTokenTypes.BACKTICK)?.text + if (startDelimiter != null) { + val text = node.text.substring(startDelimiter.length).removeSuffix(startDelimiter) + val codeSpan = ContentCode().apply { append(ContentText(text)) } + parent.append(codeSpan) + } } MarkdownElementTypes.CODE_BLOCK, MarkdownElementTypes.CODE_FENCE -> appendNodeWithChildren(ContentBlockCode()) @@ -126,6 +131,7 @@ fun buildContentTo(tree: MarkdownNode, target: ContentBlock, linkResolver: (Stri MarkdownTokenTypes.LBRACKET, MarkdownTokenTypes.RBRACKET, MarkdownTokenTypes.EXCLAMATION_MARK, + MarkdownTokenTypes.BACKTICK, MarkdownTokenTypes.CODE_FENCE_CONTENT -> { parent.append(ContentText(node.text)) } diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt index 00bbdd61..864fb9d1 100644 --- a/core/src/test/kotlin/format/MarkdownFormatTest.kt +++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt @@ -214,6 +214,10 @@ class MarkdownFormatTest { verifyMarkdownNodeByName("exclInCodeBlock", "foo") } + @Test fun backtickInCodeBlock() { + verifyMarkdownNodeByName("backtickInCodeBlock", "foo") + } + private fun verifyMarkdownPackage(fileName: String, withKotlinRuntime: Boolean = false) { verifyOutput("testdata/format/$fileName.kt", ".package.md", withKotlinRuntime = withKotlinRuntime) { model, output -> markdownService.createOutputBuilder(output, tempLocation).appendNodes(model.members) |