diff options
author | Linnea Gräf <nea@nea.moe> | 2024-03-24 16:56:28 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-03-24 16:56:28 +0100 |
commit | 4f7a626cdda9beef3ac058427594b7295045a7e2 (patch) | |
tree | d1254d9527b5e0177d1cf049df19a35c7859eaf4 | |
parent | 1cc009627643c6a3636472491825ae0d4a751616 (diff) | |
download | blog-infra-4f7a626cdda9beef3ac058427594b7295045a7e2.tar.gz blog-infra-4f7a626cdda9beef3ac058427594b7295045a7e2.tar.bz2 blog-infra-4f7a626cdda9beef3ac058427594b7295045a7e2.zip |
Add link parser
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/BlogPost.kt | 11 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/LinkParser.kt | 21 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/blog/md/MarkdownParser.kt | 1 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/LinkTest.kt | 16 | ||||
-rw-r--r-- | src/test/kotlin/moe/nea/blog/md/test/TestItalics.kt | 9 |
5 files changed, 47 insertions, 11 deletions
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<Char> + get() = "()[]".toSet() + + override fun parse(parser: MarkdownParser, text: String): Pair<MarkdownFormat, String> { + 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("<link target=https://nea.moe>meee</link>", "[meee]( https://nea.moe )") + } + + @Test + fun testFormattingInLink() { + assertInlineFormat("<link target=https://nea.moe><i>meee</i></link>", "[*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("<i><b>both</b> just italics</i>", "***both** just italics*") } + + @Test + fun otherMarkdownInItalics() { + assertInlineFormat( + "<i><link target=https://nea.moe><b>test</b></link></i>", + "*[**test**](https://nea.moe)*" + ) + } + @Test fun testDoubleStartRestartingItalics() { assertInlineFormat( |