diff options
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/Image.kt | 11 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/ImageParser.kt | 21 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 5 |
3 files changed, 35 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/blog/md/Image.kt b/src/main/kotlin/moe/nea/blog/md/Image.kt new file mode 100644 index 0000000..3bebf84 --- /dev/null +++ b/src/main/kotlin/moe/nea/blog/md/Image.kt @@ -0,0 +1,11 @@ +package moe.nea.blog.md + +import java.io.PrintStream + +data class Image(val source: String, val alt: MarkdownFormat?) : MarkdownFormat { + override fun debugFormat(printStream: PrintStream) { + printStream.print("<image source=$source>") + alt?.debugFormat(printStream) + printStream.print("</image>") + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/blog/md/ImageParser.kt b/src/main/kotlin/moe/nea/blog/md/ImageParser.kt new file mode 100644 index 0000000..da53ca0 --- /dev/null +++ b/src/main/kotlin/moe/nea/blog/md/ImageParser.kt @@ -0,0 +1,21 @@ +package moe.nea.blog.md + +object ImageParser : InlineParser { + override fun detect(lookback: MarkdownFormat, rest: String): Boolean { + return rest.startsWith("![") + } + + override val specialSyntax: Set<Char> + get() = "!()[]".toSet() + + override fun parse(parser: MarkdownParser, text: String): Pair<MarkdownFormat, String> { + val (label, rest) = parser.parseInlineTextUntil( + text.substring(2), + Begin() + ) { _, remaining -> remaining.startsWith("]") } // TODO: better begin + require(rest.startsWith("](")) + val urlEndIndex = rest.indexOf(')') + val url = rest.substring(2, urlEndIndex) + return Pair(Image(url.trim(), parser.collapseInlineFormat(label, false)), rest.substring(urlEndIndex + 1)) + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt index aaac6a1..5ee984f 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -89,12 +89,12 @@ class MarkdownParser(source: String) { var remaining = text var lastToken: MarkdownFormat = initialLookback while (remaining.isNotEmpty()) { + if (breakout(lastToken, remaining)) + break val (tok, next) = parseInlineTextOnce(lastToken, remaining) seq.add(tok) lastToken = tok remaining = next - if (breakout(tok, next)) - break } return seq to remaining } @@ -172,6 +172,7 @@ class MarkdownParser(source: String) { blockParsers.add(ListParser) inlineParsers.add(ItalicsParser) inlineParsers.add(LinkParser) + inlineParsers.add(ImageParser) } fun getLineIndex(): Int { |