aboutsummaryrefslogtreecommitdiff
path: root/src/Markdown/MarkdownProcessor.kt
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-09-29 20:54:59 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-09-29 20:54:59 +0400
commit778e2b3f7ff62971e18a49d81a8825e5dd894c2e (patch)
treef7fb9506800262ecabb9050ffee4a97e39812ccb /src/Markdown/MarkdownProcessor.kt
parent2e3dc238275073a5c7a2e5a14c79337d12492dad (diff)
downloaddokka-778e2b3f7ff62971e18a49d81a8825e5dd894c2e.tar.gz
dokka-778e2b3f7ff62971e18a49d81a8825e5dd894c2e.tar.bz2
dokka-778e2b3f7ff62971e18a49d81a8825e5dd894c2e.zip
Extract content model, make doc model independent from descriptors, parse doccomments with custom parser, some tests failing due to hanging new lines.
Diffstat (limited to 'src/Markdown/MarkdownProcessor.kt')
-rw-r--r--src/Markdown/MarkdownProcessor.kt62
1 files changed, 37 insertions, 25 deletions
diff --git a/src/Markdown/MarkdownProcessor.kt b/src/Markdown/MarkdownProcessor.kt
index fe6e8436..19d5a8fb 100644
--- a/src/Markdown/MarkdownProcessor.kt
+++ b/src/Markdown/MarkdownProcessor.kt
@@ -9,12 +9,11 @@ import com.intellij.lang.LighterASTNode
import com.intellij.util.diff.FlyweightCapableTreeStructure
import com.intellij.openapi.util.Ref
import org.jetbrains.markdown.lexer.MarkdownLexer
+import com.intellij.psi.tree.IElementType
-public class MarkdownProcessor {
- class object {
- val EXPR_LANGUAGE = object : Language("MARKDOWN") {}
- val DOCUMENT = IFileElementType("DOCUMENT", EXPR_LANGUAGE);
- }
+public object MarkdownProcessor {
+ val EXPR_LANGUAGE = object : Language("MARKDOWN") {}
+ val DOCUMENT = IFileElementType("DOCUMENT", EXPR_LANGUAGE);
public fun parse(markdown: String): MarkdownTree {
val parser = MarkdownParser()
@@ -30,6 +29,28 @@ public class MarkdownTree(private val text: String, private val structure: Flywe
visit(structure.getRoot(), action)
}
+ fun findChildByType(node: LighterASTNode, findType: IElementType) : LighterASTNode? {
+ val ref = Ref.create<Array<LighterASTNode>?>()
+ val count = structure.getChildren(node, ref)
+ val children = ref.get()
+ if (children != null) {
+ for (index in 0..count - 1) {
+ val child = children[index]
+ val nodeType = child.getTokenType()
+ if (nodeType == findType)
+ return child
+ val nestedChild = findChildByType(child, findType)
+ if (nestedChild != null)
+ return nestedChild
+ }
+ }
+ return null
+ }
+
+ fun getNodeText(node: LighterASTNode) : String {
+ return text.substring(node.getStartOffset(), node.getEndOffset())
+ }
+
fun visit(node: LighterASTNode, action: (LighterASTNode, String, visitChildren: () -> Unit) -> Unit) {
action(node, text) {
val ref = Ref.create<Array<LighterASTNode>?>()
@@ -46,7 +67,7 @@ public class MarkdownTree(private val text: String, private val structure: Flywe
}
-public fun MarkdownTree.dump(): String {
+public fun MarkdownTree.toTestString(): String {
val sb = StringBuilder()
var level = 0
visit {(node, text, visitChildren) ->
@@ -64,29 +85,25 @@ public fun MarkdownTree.dump(): String {
public fun MarkdownTree.toHtml(): String {
val sb = StringBuilder()
- var level = 0
visit {(node, text, processChildren) ->
val nodeType = node.getTokenType()
val nodeText = text.substring(node.getStartOffset(), node.getEndOffset())
- val indent = " ".repeat(level * 2)
when (nodeType) {
MarkdownElementTypes.BULLET_LIST -> {
- sb.appendln("$indent<ul>")
- level++
+ sb.appendln("<ul>")
processChildren()
- level--
- sb.appendln("$indent</ul>")
+ sb.appendln("</ul>")
}
MarkdownElementTypes.HORIZONTAL_RULE -> {
- sb.appendln("$indent<hr/>")
+ sb.appendln("<hr/>")
}
MarkdownElementTypes.ORDERED_LIST -> {
- sb.appendln("$indent<ol>")
+ sb.appendln("<ol>")
processChildren()
- sb.appendln("$indent</ol>")
+ sb.appendln("</ol>")
}
MarkdownElementTypes.LIST_BLOCK -> {
- sb.append("$indent<li>")
+ sb.append("<li>")
processChildren()
sb.appendln("</li>")
}
@@ -110,14 +127,9 @@ public fun MarkdownTree.toHtml(): String {
sb.appendln()
}
MarkdownElementTypes.PARA -> {
- sb.appendln("$indent<p>")
- processChildren()
- sb.appendln("$indent</p>")
- }
- MarkdownElementTypes.VERBATIM -> {
- sb.appendln("$indent<pre><code>")
+ sb.append("<p>")
processChildren()
- sb.appendln("$indent</code></pre>")
+ sb.appendln("</p>")
}
else -> {
processChildren()
@@ -129,8 +141,8 @@ public fun MarkdownTree.toHtml(): String {
fun markdownToHtml(markdown: String): String {
- val markdownTree = MarkdownProcessor().parse(markdown)
- val ast = markdownTree.dump()
+ val markdownTree = MarkdownProcessor.parse(markdown)
+ val ast = markdownTree.toTestString()
return markdownTree.toHtml()
}