aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Markdown
diff options
context:
space:
mode:
authorDmitry Jemerov <yole@jetbrains.com>2015-12-03 16:22:11 +0100
committerDmitry Jemerov <yole@jetbrains.com>2015-12-03 16:22:49 +0100
commit39631054c58df5841ea268b7002b820ec55f6e0a (patch)
treecefedd8411c859243bd181568e16fcdd372a38c8 /core/src/main/kotlin/Markdown
parent797cb4732c53bf1e3b2091add8cf731fc436607f (diff)
downloaddokka-39631054c58df5841ea268b7002b820ec55f6e0a.tar.gz
dokka-39631054c58df5841ea268b7002b820ec55f6e0a.tar.bz2
dokka-39631054c58df5841ea268b7002b820ec55f6e0a.zip
restructure Dokka build to use Gradle for everything except for the Maven plugin
Diffstat (limited to 'core/src/main/kotlin/Markdown')
-rw-r--r--core/src/main/kotlin/Markdown/MarkdownProcessor.kt50
1 files changed, 50 insertions, 0 deletions
diff --git a/core/src/main/kotlin/Markdown/MarkdownProcessor.kt b/core/src/main/kotlin/Markdown/MarkdownProcessor.kt
new file mode 100644
index 00000000..99caddc4
--- /dev/null
+++ b/core/src/main/kotlin/Markdown/MarkdownProcessor.kt
@@ -0,0 +1,50 @@
+package org.jetbrains.dokka
+
+import org.intellij.markdown.IElementType
+import org.intellij.markdown.MarkdownElementTypes
+import org.intellij.markdown.ast.ASTNode
+import org.intellij.markdown.ast.LeafASTNode
+import org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor
+import org.intellij.markdown.parser.MarkdownParser
+
+class MarkdownNode(val node: ASTNode, val parent: MarkdownNode?, val markdown: String) {
+ val children: List<MarkdownNode> = node.children.map { MarkdownNode(it, this, markdown) }
+ val type: IElementType get() = node.type
+ val text: String get() = markdown.substring(node.startOffset, node.endOffset)
+ fun child(type: IElementType): MarkdownNode? = children.firstOrNull { it.type == type }
+
+ override fun toString(): String = StringBuilder().apply { presentTo(this) }.toString()
+}
+
+fun MarkdownNode.visit(action: (MarkdownNode, () -> Unit) -> Unit) {
+ action(this) {
+ for (child in children) {
+ child.visit(action)
+ }
+ }
+}
+
+public fun MarkdownNode.toTestString(): String {
+ val sb = StringBuilder()
+ var level = 0
+ visit { node, visitChildren ->
+ sb.append(" ".repeat(level * 2))
+ node.presentTo(sb)
+ level++
+ visitChildren()
+ level--
+ }
+ return sb.toString()
+}
+
+private fun MarkdownNode.presentTo(sb: StringBuilder) {
+ sb.append(type.toString())
+ sb.append(":" + text.replace("\n", "\u23CE"))
+ sb.appendln()
+}
+
+fun parseMarkdown(markdown: String): MarkdownNode {
+ if (markdown.isEmpty())
+ return MarkdownNode(LeafASTNode(MarkdownElementTypes.MARKDOWN_FILE, 0, 0), null, markdown)
+ return MarkdownNode(MarkdownParser(CommonMarkFlavourDescriptor()).buildMarkdownTreeFromString(markdown), null, markdown)
+}