From 4f7a626cdda9beef3ac058427594b7295045a7e2 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 24 Mar 2024 16:56:28 +0100 Subject: Add link parser --- src/main/kotlin/moe/nea/blog/md/BlogPost.kt | 11 ----------- src/main/kotlin/moe/nea/blog/md/LinkParser.kt | 21 +++++++++++++++++++++ src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 1 + src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt | 16 ++++++++++++++++ src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt | 9 +++++++++ 5 files changed, 47 insertions(+), 11 deletions(-) delete mode 100644 src/main/kotlin/moe/nea/blog/md/BlogPost.kt create mode 100644 src/main/kotlin/moe/nea/blog/md/LinkParser.kt create mode 100644 src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt diff --git a/src/main/kotlin/moe/nea/blog/md/BlogPost.kt b/src/main/kotlin/moe/nea/blog/md/BlogPost.kt deleted file mode 100644 index 8343b85..0000000 --- a/src/main/kotlin/moe/nea/blog/md/BlogPost.kt +++ /dev/null @@ -1,11 +0,0 @@ -package moe.nea.blog.md - -import java.io.File - - -fun main() { - val parser = MarkdownParser(File("data/test.md").readText()) - parser.addDefaultParsers() - val doc = parser.readDocument() - doc.debugFormat(System.out) -} diff --git a/src/main/kotlin/moe/nea/blog/md/LinkParser.kt b/src/main/kotlin/moe/nea/blog/md/LinkParser.kt new file mode 100644 index 0000000..c247e6f --- /dev/null +++ b/src/main/kotlin/moe/nea/blog/md/LinkParser.kt @@ -0,0 +1,21 @@ +package moe.nea.blog.md + +object LinkParser : InlineParser { + override fun detect(lookback: MarkdownFormat, rest: String): Boolean { + return rest.startsWith("[") + } + + override val specialSyntax: Set + get() = "()[]".toSet() + + override fun parse(parser: MarkdownParser, text: String): Pair { + val (label, rest) = parser.parseInlineTextUntil( + text.substring(1), + Begin() + ) { lookback, remaining -> remaining.startsWith("]") } // TODO: better begin + require(rest.startsWith("](")) { TODO("unlabeled links") } + val urlEndIndex = rest.indexOf(')') + val url = rest.substring(2, urlEndIndex) + return Pair(Link(parser.collapseInlineFormat(label, false), url.trim()), 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 40c4c30..ad818dd 100644 --- a/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt +++ b/src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt @@ -140,6 +140,7 @@ class MarkdownParser(source: String) { blockParsers.add(CodeBlockParser) blockParsers.add(HeaderParser) inlineParsers.add(ItalicsParser) + inlineParsers.add(LinkParser) } } diff --git a/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt b/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt new file mode 100644 index 0000000..d476715 --- /dev/null +++ b/src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt @@ -0,0 +1,16 @@ +package moe.nea.blog.md.test + +import kotlin.test.Test + +class LinkTest : MarkdownTest() { + @Test + fun testPaddedLink() { + assertInlineFormat("meee", "[meee]( https://nea.moe )") + } + + @Test + fun testFormattingInLink() { + assertInlineFormat("meee", "[*meee*]( https://nea.moe )") + } + +} \ No newline at end of file diff --git a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt index 673dafb..f4c1669 100644 --- a/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt +++ b/src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt @@ -14,6 +14,15 @@ class TestItalics : MarkdownTest() { assertInlineFormat("both just italics", "***both** just italics*") } + + @Test + fun otherMarkdownInItalics() { + assertInlineFormat( + "test", + "*[**test**](https://nea.moe)*" + ) + } + @Test fun testDoubleStartRestartingItalics() { assertInlineFormat( -- cgit