summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-05-02 23:32:11 +0200
committerLinnea Gräf <nea@nea.moe>2024-05-02 23:42:15 +0200
commit7e24b934bbd424669a1c935894dd8799d4db0348 (patch)
treeea17a94215768ed6dea8a306a0bd58dfd936fb5f /src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt
parentebe41fbbd8b537b3008c5c52d2c9a496694ea281 (diff)
downloadblog-infra-7e24b934bbd424669a1c935894dd8799d4db0348.tar.gz
blog-infra-7e24b934bbd424669a1c935894dd8799d4db0348.tar.bz2
blog-infra-7e24b934bbd424669a1c935894dd8799d4db0348.zip
Add note blocks and an integration test
Diffstat (limited to 'src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt')
-rw-r--r--src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt b/src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt
new file mode 100644
index 0000000..a1f977f
--- /dev/null
+++ b/src/main/kotlin/moe/nea/blog/md/ext/NoteBlocks.kt
@@ -0,0 +1,57 @@
+package moe.nea.blog.md.ext
+
+import moe.nea.blog.gen.DefHtmlFragmentGenerator
+import moe.nea.blog.gen.HtmlDsl
+import moe.nea.blog.gen.MD2HtmlGenerator
+import moe.nea.blog.md.BlockParser
+import moe.nea.blog.md.MarkdownBlock
+import moe.nea.blog.md.MarkdownParser
+import moe.nea.blog.util.indent
+import moe.nea.blog.util.indentSize
+import java.io.PrintStream
+
+data class NoteBlock(val noteType: String, val child: MarkdownBlock) : MarkdownBlock {
+ override fun debugFormat(indent: Int, printStream: PrintStream) {
+ printStream.indent(indent)
+ printStream.println("<note type=\"$noteType\">")
+
+ child.debugFormat(indent + 2, printStream)
+
+ printStream.indent(indent)
+ printStream.println("</note>")
+ }
+}
+
+object NoteBlockGenerator : DefHtmlFragmentGenerator<NoteBlock>() {
+ override fun HtmlDsl.makeHtml(generator: MD2HtmlGenerator, node: NoteBlock) {
+ element("div", mapOf("class" to "note-${node.noteType}"), generator.generateHtml(node.child))
+ }
+}
+
+object NoteBlockParser : BlockParser {
+ override fun detect(line: String): Boolean {
+ return line.startsWith("!!!")
+ }
+
+ override fun parse(parser: MarkdownParser): MarkdownBlock {
+ val line = parser.consumeLine()!!
+ val noteType = line.substring(3).trim()
+ while ((parser.peekLine() ?: error("Unterminated !!! note")).isBlank()) {
+ parser.consumeLine()
+ }
+ val indent = parser.peekLine()!!.indentSize()!!
+ require(indent > 0) { "!!! note not indented" }
+ parser.pushIndent(indent)
+ val list = mutableListOf<MarkdownBlock>()
+ while (true) {
+ val block = parser.readChildBlock() ?: break
+ list.add(block)
+ }
+ parser.popIndent()
+ return NoteBlock(noteType, parser.mergeBlocks(list))
+ }
+
+ override val prio: Int
+ get() = 10
+
+}