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 | |
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')
-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 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/CodeBlockTest.kt | 66 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/MarkdownTest.kt | 36 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt | 20 |
5 files changed, 112 insertions, 22 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 diff --git a/src/test/kotlin/moe/nea/blog/md/test/CodeBlockTest.kt b/src/test/kotlin/moe/nea/blog/md/test/CodeBlockTest.kt new file mode 100644 index 0000000..3e9bbd8 --- /dev/null +++ b/src/test/kotlin/moe/nea/blog/md/test/CodeBlockTest.kt @@ -0,0 +1,66 @@ +package moe.nea.blog.md.test + +import kotlin.test.Test + +class CodeBlockTest : MarkdownTest() { + @Test + fun testJavaCodeBlock() { + assertDocumentFormat( + """ + <code language=java> + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + + } + </code> + """.trimIndent(), + """ + ```java + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + + } + ``` + """.trimIndent() + ) + } + @Test + fun testAutoStripCommonIndent() { + assertDocumentFormat( + """ + <code language=java> + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + } + </code> + """.trimIndent(), + """ + ```java + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + } + ``` + """.trimIndent() + ) + } + + @Test + fun testNonStrippedBlock() { + assertDocumentFormat( + """ + <code language=java> + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + } + </code> + """.trimIndent(), + """ + ```java notrim + public class ObjectControllerFactoryManagerProvider<T extends ObjectControllerFactoryManager<T>> { + public void doTheThing() {} + } + ``` + """.trimIndent() + ) + } +}
\ No newline at end of file diff --git a/src/test/kotlin/moe/nea/blog/md/test/MarkdownTest.kt b/src/test/kotlin/moe/nea/blog/md/test/MarkdownTest.kt new file mode 100644 index 0000000..3e01acc --- /dev/null +++ b/src/test/kotlin/moe/nea/blog/md/test/MarkdownTest.kt @@ -0,0 +1,36 @@ +package moe.nea.blog.md.test + +import moe.nea.blog.md.Document +import moe.nea.blog.md.MarkdownFormat +import moe.nea.blog.md.MarkdownParser +import java.io.ByteArrayOutputStream +import java.io.PrintStream +import kotlin.test.assertEquals + +abstract class MarkdownTest { + + fun parseInline(string: String): MarkdownFormat { + val parser = MarkdownParser(string) + parser.addDefaultParsers() + return parser.parseInlineText(string) + } + fun parseDoc(string: String): Document { + val parser = MarkdownParser(string) + parser.addDefaultParsers() + return parser.readDocument() + } + + fun assertDocumentFormat(expected: String, source: String) { + val format = parseDoc(source) + val baos = ByteArrayOutputStream() + format.debugFormat(PrintStream(baos)) + assertEquals(expected.trim(), baos.toByteArray().decodeToString().trim()) + } + + fun assertInlineFormat(expected: String, source: String) { + val format = parseInline(source) + val baos = ByteArrayOutputStream() + format.debugFormat(PrintStream(baos)) + assertEquals(expected, baos.toByteArray().decodeToString()) + } +}
\ No newline at end of file diff --git a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt index 5cd8cb4..673dafb 100644 --- a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt +++ b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt @@ -1,26 +1,8 @@ package moe.nea.blog.md.test -import moe.nea.blog.md.MarkdownFormat -import moe.nea.blog.md.MarkdownParser -import java.io.ByteArrayOutputStream -import java.io.PrintStream import kotlin.test.Test -import kotlin.test.assertEquals -class TestItalics { - - fun parseInline(string: String): MarkdownFormat { - val parser = MarkdownParser(string) - parser.addDefaultParsers() - return parser.parseInlineText(string) - } - - fun assertInlineFormat(expected: String, source: String) { - val format = parseInline(source) - val baos = ByteArrayOutputStream() - format.debugFormat(PrintStream(baos)) - assertEquals(expected, baos.toByteArray().decodeToString()) - } +class TestItalics : MarkdownTest() { @Test fun testDoubleStartItalicsInBold() { |