diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-24 22:36:36 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-24 22:36:36 +0100 |
commit | 9513802ef7637a469c72a2dedd68d5bf9215ba4d (patch) | |
tree | 0b433dc2e35a2dbd138d4075c0b5f9fb2db528d3 | |
parent | e47481aa60c19abcb246386bf48a83ecccd238e4 (diff) | |
download | blog-infra-9513802ef7637a469c72a2dedd68d5bf9215ba4d.tar.gz blog-infra-9513802ef7637a469c72a2dedd68d5bf9215ba4d.tar.bz2 blog-infra-9513802ef7637a469c72a2dedd68d5bf9215ba4d.zip |
Add block quotes
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/BlockQuote.kt | 14 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/BlockQuoteParser.kt | 33 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 1 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/BlockQuoteTest.kt | 45 |
4 files changed, 93 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/BlockQuote.kt b/src/main/kotlin/moe/nea/blog/md/BlockQuote.kt new file mode 100644 index 0000000..ba3824f --- /dev/null +++ b/src/main/kotlin/moe/nea/blog/md/BlockQuote.kt @@ -0,0 +1,14 @@ +package moe.nea.blog.md + +import moe.nea.blog.util.indent +import java.io.PrintStream + +class BlockQuote(val inner: MarkdownBlock) : MarkdownBlock { + override fun debugFormat(indent: Int, printStream: PrintStream) { + printStream.indent(indent) + printStream.println("<quote>") + inner.debugFormat(indent + 2, printStream) + printStream.indent(indent) + printStream.println("</quote>") + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/blog/md/BlockQuoteParser.kt b/src/main/kotlin/moe/nea/blog/md/BlockQuoteParser.kt new file mode 100644 index 0000000..c37e215 --- /dev/null +++ b/src/main/kotlin/moe/nea/blog/md/BlockQuoteParser.kt @@ -0,0 +1,33 @@ +package moe.nea.blog.md + +object BlockQuoteParser : BlockParser { + override fun detect(line: String): Boolean { + return line.startsWith("> ") + } + + override fun parse(parser: MarkdownParser): MarkdownBlock { + parser.pushPreProcessor( + LinePreProcessor.whileIgnoringFirst( + parser.getIndent() + ) { _, line -> + if (!line.startsWith("> ") && line != ">") { + null + } else { + " " + line.substring(2) + } + } + ) + parser.pushIndent(2) + val blocks = mutableListOf<MarkdownBlock>() + while (true) { + val child = parser.readChildBlock() ?: break + blocks.add(child) + } + parser.popPreProcessor() + parser.popIndent() + return BlockQuote(parser.mergeBlocks(blocks)) + } + + override val prio: Int + get() = 10 +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt index 5ee984f..11aff14 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -170,6 +170,7 @@ class MarkdownParser(source: String) { blockParsers.add(CodeBlockParser) blockParsers.add(HeaderParser) blockParsers.add(ListParser) + blockParsers.add(BlockQuoteParser) inlineParsers.add(ItalicsParser) inlineParsers.add(LinkParser) inlineParsers.add(ImageParser) diff --git a/src/test/kotlin/moe/nea/blog/md/test/BlockQuoteTest.kt b/src/test/kotlin/moe/nea/blog/md/test/BlockQuoteTest.kt new file mode 100644 index 0000000..635dd88 --- /dev/null +++ b/src/test/kotlin/moe/nea/blog/md/test/BlockQuoteTest.kt @@ -0,0 +1,45 @@ +package moe.nea.blog.md.test + +import kotlin.test.Test + +class BlockQuoteTest : MarkdownTest() { + @Test + fun testNestedBlockQuote() { + assertDocumentFormat( + """ + <quote> + hi + <quote> + hello + </quote> + </quote> + """.trimIndent(), + """ + > hi + > > hello + """.trimIndent() + ) + } + + @Test + fun testCodeInBlock() { + assertDocumentFormat( + """ + <quote> + hi + <code language=kotlin> + fun helloWorld(): Unit = + "Hello, World!".let(::println) + </code> + </quote> + """.trimIndent(), + """ + > hi + > ```kotlin + > fun helloWorld(): Unit = + > "Hello, World!".let(::println) + > ``` + """.trimIndent() + ) + } +}
\ No newline at end of file |