diff options
Diffstat (limited to 'src/main/kotlin/moe/nea')
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/BlogPost.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt | 8 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 34 |
3 files changed, 36 insertions, 8 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/BlogPost.kt b/src/main/kotlin/moe/nea/blog/md/BlogPost.kt index 25ec13f..8343b85 100644 --- a/src/main/kotlin/moe/nea/blog/md/BlogPost.kt +++ b/src/main/kotlin/moe/nea/blog/md/BlogPost.kt @@ -8,4 +8,4 @@ fun main() { parser.addDefaultParsers() val doc = parser.readDocument() doc.debugFormat(System.out) -}
\ No newline at end of file +} diff --git a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt index afecf79..30b5c38 100644 --- a/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/ItalicsParser.kt @@ -23,7 +23,7 @@ object ItalicsParser : InlineParser { val firstSequence = mutableListOf<MarkdownFormat>() var remainingText = text.substring(firstStarCount) var lastToken: MarkdownFormat = Begin() // TODO: dedicated begin token - while (true) { + while (remainingText.isNotEmpty()) { val (element, next) = parser.parseInlineTextOnce(lastToken, remainingText) remainingText = next lastToken = element @@ -42,7 +42,7 @@ object ItalicsParser : InlineParser { if (secondStarCount < 1) error("Invalid italics/bold sequence") remainingText = remainingText.substring(secondStarCount) - var firstElement = parser.collapseInlineFormat(firstSequence) + var firstElement = parser.collapseInlineFormat(firstSequence, false) if (secondStarCount == 2) firstElement = Bold(firstElement) if (secondStarCount == 1) @@ -57,7 +57,7 @@ object ItalicsParser : InlineParser { val secondSequence = mutableListOf<MarkdownFormat>() lastToken = Begin() - while (true) { + while (remainingText.isNotEmpty()) { val (element, next) = parser.parseInlineTextOnce(lastToken, remainingText) remainingText = next lastToken = element @@ -79,7 +79,7 @@ object ItalicsParser : InlineParser { if (thirdStarCount != firstStarCount - secondStarCount) { error("Invalid italics/bold sequence") } - var secondElement = parser.collapseInlineFormat(secondSequence) + var secondElement = parser.collapseInlineFormat(secondSequence, false) var combined: MarkdownFormat = FormatSequence(firstElement, secondElement) if (thirdStarCount == 1) combined = Italics(combined) 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 { |