From e47481aa60c19abcb246386bf48a83ecccd238e4 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 24 Mar 2024 22:21:12 +0100 Subject: Add prefix ignoring line processor --- src/main/kotlin/moe/nea/blog/md/LinePreProcessor.kt | 10 +++++++++- src/main/kotlin/moe/nea/blog/md/ListParser.kt | 19 ++++++++----------- 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) -- cgit