aboutsummaryrefslogtreecommitdiff
path: root/src/Kotlin/ContentBuilder.kt
diff options
context:
space:
mode:
authorDmitry Jemerov <intelliyole@gmail.com>2015-01-30 19:04:34 +0100
committerDmitry Jemerov <intelliyole@gmail.com>2015-01-30 19:04:34 +0100
commitb5286f576f470ad1665a7b1acc9ea276bc1a0c18 (patch)
tree5cf3800e961b9da84b2db03707603865496f5ed0 /src/Kotlin/ContentBuilder.kt
parentb55b258574a01a02f906f5f12646ecacfc640e20 (diff)
parent0fac1d925b74f24002a4e1538088ce66c4b02cb9 (diff)
downloaddokka-b5286f576f470ad1665a7b1acc9ea276bc1a0c18.tar.gz
dokka-b5286f576f470ad1665a7b1acc9ea276bc1a0c18.tar.bz2
dokka-b5286f576f470ad1665a7b1acc9ea276bc1a0c18.zip
Merge pull request #24 from orangy/sections-rewrite
Use KDoc PSI instead of custom Markdown extensions to parse KDoc into sections
Diffstat (limited to 'src/Kotlin/ContentBuilder.kt')
-rw-r--r--src/Kotlin/ContentBuilder.kt48
1 files changed, 18 insertions, 30 deletions
diff --git a/src/Kotlin/ContentBuilder.kt b/src/Kotlin/ContentBuilder.kt
index c943a505..35beec54 100644
--- a/src/Kotlin/ContentBuilder.kt
+++ b/src/Kotlin/ContentBuilder.kt
@@ -9,10 +9,16 @@ import org.intellij.markdown.*
import org.jetbrains.kotlin.psi.JetDeclarationWithBody
import org.jetbrains.kotlin.psi.JetBlockExpression
-public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: DeclarationDescriptor): Content {
+public fun DocumentationBuilder.buildContent(tree: MarkdownNode): Content {
+ val result = Content()
+ buildContentTo(tree, result)
+ return result
+}
+
+public fun DocumentationBuilder.buildContentTo(tree: MarkdownNode, target: ContentNode) {
// println(tree.toTestString())
val nodeStack = ArrayDeque<ContentNode>()
- nodeStack.push(Content())
+ nodeStack.push(target)
tree.visit {(node, processChildren) ->
val parent = nodeStack.peek()!!
@@ -47,22 +53,6 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec
processChildren()
parent.append(nodeStack.pop())
}
- MarkdownTokenTypes.SECTION_ID -> {
- if (parent !is ContentSection) {
- val text = node.text.trimLeading("$").trim("{", "}")
- val name = text.substringBefore(" ")
- val params = text.substringAfter(" ")
- when (name) {
- "code" -> parent.append(functionBody(descriptor, params))
- }
- }
- }
- MarkdownElementTypes.SECTION -> {
- val label = node.child(MarkdownTokenTypes.SECTION_ID)?.let { it.text.trimLeading("$").trim("{", "}") } ?: ""
- nodeStack.push(ContentSection(label))
- processChildren()
- parent.append(nodeStack.pop())
- }
MarkdownElementTypes.INLINE_LINK -> {
val label = node.child(MarkdownElementTypes.LINK_TEXT)?.child(MarkdownTokenTypes.TEXT)
val destination = node.child(MarkdownElementTypes.LINK_DESTINATION)
@@ -88,7 +78,7 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec
}
MarkdownTokenTypes.WHITE_SPACE,
MarkdownTokenTypes.EOL -> {
- if (nodeStack.peek() is ContentParagraph && node.parent?.children?.last() != node) {
+ if (keepWhitespace(nodeStack.peek()) && node.parent?.children?.last() != node) {
nodeStack.push(ContentText(node.text))
processChildren()
parent.append(nodeStack.pop())
@@ -105,10 +95,7 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec
parent.append(nodeStack.pop())
}
MarkdownTokenTypes.COLON -> {
- // TODO fix markdown parser
- if (!isColonAfterSectionLabel(node)) {
- parent.append(ContentText(node.text))
- }
+ parent.append(ContentText(node.text))
}
MarkdownTokenTypes.DOUBLE_QUOTE,
MarkdownTokenTypes.LT,
@@ -120,9 +107,16 @@ public fun DocumentationBuilder.buildContent(tree: MarkdownNode, descriptor: Dec
}
}
}
- return nodeStack.pop() as Content
}
+private fun keepWhitespace(node: ContentNode) = node is ContentParagraph || node is ContentSection
+
+public fun DocumentationBuilder.buildInlineContentTo(tree: MarkdownNode, target: ContentNode) {
+ val inlineContent = tree.children.singleOrNull { it.type == MarkdownElementTypes.PARAGRAPH }?.children ?: listOf(tree)
+ inlineContent.forEach {
+ buildContentTo(it, target)
+ }
+}
fun DocumentationBuilder.functionBody(descriptor: DeclarationDescriptor, functionName: String): ContentNode {
val scope = getResolutionScope(descriptor)
@@ -176,9 +170,3 @@ private fun DocumentationBuilder.resolveInScope(functionName: String, scope: Jet
return symbol
}
-
-private fun isColonAfterSectionLabel(node: MarkdownNode): Boolean {
- val parent = node.parent
- return parent != null && parent.type == MarkdownElementTypes.SECTION && parent.children.size() >= 2 &&
- node == parent.children[1];
-}