diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/blog')
-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 |
3 files changed, 22 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) } } |