summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-03-24 22:21:12 +0100
committerLinnea Gräf <nea@nea.moe>2024-03-24 22:23:15 +0100
commite47481aa60c19abcb246386bf48a83ecccd238e4 (patch)
treea63f2e712c721e55db4f5ba14a00492daa655b44 /src/main
parentea62594ea8f70649f5c44612fd3e53dfc35d9e88 (diff)
downloadblog-infra-e47481aa60c19abcb246386bf48a83ecccd238e4.tar.gz
blog-infra-e47481aa60c19abcb246386bf48a83ecccd238e4.tar.bz2
blog-infra-e47481aa60c19abcb246386bf48a83ecccd238e4.zip
Add prefix ignoring line processor
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt10
-rw-r--r--src/main/kotlin/moe/nea/blog/md/ListParser.kt19
2 files changed, 17 insertions, 12 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt b/src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt
index cb9d81f..7938cbe 100644
--- a/src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt
+++ b/src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt
@@ -1,5 +1,13 @@
package moe.nea.blog.md
-interface LinePreProcessor {
+fun interface LinePreProcessor {
fun preprocess(lineIndex: Int, line: String): String?
+
+ companion object {
+ fun whileIgnoringFirst(indent: Int, wrapped: LinePreProcessor): LinePreProcessor {
+ return LinePreProcessor { lineIndex, line ->
+ line.take(indent) + wrapped.preprocess(lineIndex, line.drop(indent))
+ }
+ }
+ }
}
diff --git a/src/main/kotlin/moe/nea/blog/md/ListParser.kt b/src/main/kotlin/moe/nea/blog/md/ListParser.kt
index 7d1e63b..8175c1a 100644
--- a/src/main/kotlin/moe/nea/blog/md/ListParser.kt
+++ b/src/main/kotlin/moe/nea/blog/md/ListParser.kt
@@ -13,18 +13,15 @@ object ListParser : BlockParser {
val prefix = line.substring(0, indentSize + 2)
var bypassLineIndex = parser.getLineIndex()
val indentDepth = parser.getIndent()
- val processor = object : LinePreProcessor {
- override fun preprocess(lineIndex: Int, line: String): String? {
- if (line.substring(indentDepth).startsWith(prefix)) {
- if (bypassLineIndex == lineIndex) {
- return line.substring(0, indentDepth) +
- " ".repeat(indentSize + 2) +
- line.substring(indentDepth + indentSize + 2)
- } else {
- return null
- }
+ val processor = LinePreProcessor.whileIgnoringFirst(indentDepth) { lineIndex, line ->
+ if (line.startsWith(prefix)) {
+ if (bypassLineIndex == lineIndex) {
+ " ".repeat(indentSize + 2) + line.substring(indentSize + 2)
+ } else {
+ null
}
- return line
+ } else {
+ line
}
}
parser.pushPreProcessor(processor)