summaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-03-24 15:24:05 +0100
committerLinnea Gräf <nea@nea.moe>2024-03-24 15:24:05 +0100
commitbe66c61d729fe642e3dd3ed2214990f9f2273c5e (patch)
tree9a360bc61aaedd6b69518914b1c76ed0cd512ea3 /src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt
parentc57016dc5a1f502b637c2f77bcf724165387ef83 (diff)
downloadblog-infra-be66c61d729fe642e3dd3ed2214990f9f2273c5e.tar.gz
blog-infra-be66c61d729fe642e3dd3ed2214990f9f2273c5e.tar.bz2
blog-infra-be66c61d729fe642e3dd3ed2214990f9f2273c5e.zip
Fix whitespace trimming
Diffstat (limited to 'src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt')
-rw-r--r--src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt
index 86b92d2..f8095ad 100644
--- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt
+++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt
@@ -71,11 +71,39 @@ class MarkdownParser(source: String) {
lastToken = tok
remaining = next
}
- return collapseInlineFormat(seq)
+ return collapseInlineFormat(seq, true)
}
- fun collapseInlineFormat(sequence: List<MarkdownFormat>): MarkdownFormat {
- return FormatSequence(sequence)
+ private fun expandMarkdownFormats(sequence: List<MarkdownFormat>): List<MarkdownFormat> {
+ val elongated = mutableListOf<MarkdownFormat>()
+ for (markdownFormat in sequence) {
+ if (markdownFormat is FormatSequence) {
+ elongated.addAll(expandMarkdownFormats(markdownFormat.list))
+ } else {
+ elongated.add(markdownFormat)
+ }
+ }
+ return elongated
+ }
+
+ private fun collapseMarkdownFormats(
+ sequence: List<MarkdownFormat>,
+ trimWhitespace: Boolean
+ ): MutableList<MarkdownFormat> {
+ val shortened = mutableListOf<MarkdownFormat>()
+ var last: MarkdownFormat = if (trimWhitespace) Whitespace() else Begin()
+ for (format in sequence) {
+ if (format is Whitespace && last is Whitespace) {
+ continue
+ }
+ last = format
+ shortened.add(format)
+ }
+ return shortened
+ }
+
+ fun collapseInlineFormat(sequence: List<MarkdownFormat>, trimWhitespace: Boolean): MarkdownFormat {
+ return FormatSequence(collapseMarkdownFormats(expandMarkdownFormats(sequence), trimWhitespace))
}
fun readDocument(): Document {