From 9513802ef7637a469c72a2dedd68d5bf9215ba4d Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 24 Mar 2024 22:36:36 +0100 Subject: Add block quotes --- src/main/kotlin/moe/nea/blog/md/BlockQuote.kt | 14 +++++++++ .../kotlin/moe/nea/blog/md/BlockQuoteParser.kt | 33 ++++++++++++++++++++++ src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 1 + 3 files changed, 48 insertions(+) create mode 100644 src/main/kotlin/moe/nea/blog/md/BlockQuote.kt create mode 100644 src/main/kotlin/moe/nea/blog/md/BlockQuoteParser.kt (limited to 'src/main/kotlin') 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("") + inner.debugFormat(indent + 2, printStream) + printStream.indent(indent) + printStream.println("") + } +} \ 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() + 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) -- cgit