diff options
-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 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt | 16 |
4 files changed, 51 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 { diff --git a/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt b/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt index d476715..fbd1058 100644 --- a/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt +++ b/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt @@ -13,4 +13,20 @@ class LinkTest : MarkdownTest() { assertInlineFormat("<link target=https://nea.moe><i>meee</i></link>", "[*meee*]( https://nea.moe )") } + @Test + fun testBasicImage() { + assertInlineFormat( + "<image source=https://nea.moe/image.jxl>alt text <i>here</i></image>", + "![alt text *here*]( https://nea.moe/image.jxl )" + ) + } + + @Test + fun testImageLink() { + assertInlineFormat( + "<link target=https://nea.moe><image source=https://nea.moe/image.jxl></image></link>", + "[![]( https://nea.moe/image.jxl )](https://nea.moe)" + ) + } + }
\ No newline at end of file |