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/main/kotlin/Formats | |
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/main/kotlin/Formats')
-rw-r--r-- | core/src/main/kotlin/Formats/MarkdownFormatService.kt | 16 |
1 files changed, 16 insertions, 0 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 } |