diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-24 16:34:16 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-24 16:34:16 +0100 |
commit | 1cc009627643c6a3636472491825ae0d4a751616 (patch) | |
tree | 7efa4ad8bb93555f1f883ed337ca4b0071779b58 /src/main | |
parent | 306515545281007d1df8733e3aa2bee974e29123 (diff) | |
download | blog-infra-1cc009627643c6a3636472491825ae0d4a751616.tar.gz blog-infra-1cc009627643c6a3636472491825ae0d4a751616.tar.bz2 blog-infra-1cc009627643c6a3636472491825ae0d4a751616.zip |
Add code block test
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/CodeBlock.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt | 10 |
2 files changed, 9 insertions, 3 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/CodeBlock.kt b/src/main/kotlin/moe/nea/blog/md/CodeBlock.kt index 2882b77..4251001 100644 --- a/src/main/kotlin/moe/nea/blog/md/CodeBlock.kt +++ b/src/main/kotlin/moe/nea/blog/md/CodeBlock.kt @@ -6,7 +6,7 @@ import java.io.PrintStream data class CodeBlock(val language: String, val lines: List<String>) : MarkdownBlock { override fun debugFormat(indent: Int, printStream: PrintStream) { printStream.indent(indent) - printStream.println("<code lang=$language>") + printStream.println("<code language=$language>") lines.forEach { printStream.indent(indent) printStream.println(it) diff --git a/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt b/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt index 0c894fe..b7a0e9b 100644 --- a/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/CodeBlockParser.kt @@ -1,12 +1,15 @@ package moe.nea.blog.md +import moe.nea.blog.util.indentSize + object CodeBlockParser : BlockParser { override fun detect(line: String): Boolean { return line.startsWith("```") } override fun parse(parser: MarkdownParser): MarkdownBlock { - val language = parser.consumeLine()!!.removePrefix("```") + val tags = parser.consumeLine()!!.removePrefix("```").trim().split(" +".toRegex()) + val language = tags.firstOrNull() ?: "" val lines = mutableListOf<String>() while (true) { val line = parser.consumeLine() ?: error("Unfinished code block") @@ -14,7 +17,10 @@ object CodeBlockParser : BlockParser { break lines.add(line) } - return CodeBlock(language, lines) + val commonIndent = + if (tags.contains("notrim")) 0 + else lines.minOfOrNull { it.indentSize() ?: Int.MAX_VALUE } ?: 0 + return CodeBlock(language, lines.map { it.drop(commonIndent) }) } override val prio: Int |