aboutsummaryrefslogtreecommitdiff
path: root/src/Kotlin/ContentBuilder.kt
blob: 78bd7eafaab091ceb34d618571e18570b037316f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.jetbrains.dokka

import org.jetbrains.markdown.MarkdownElementTypes
import java.util.ArrayDeque

public fun MarkdownTree.toContent(): Content {
    val nodeStack = ArrayDeque<ContentNode>()
    nodeStack.push(Content())

    visit {(node, text, processChildren) ->
        val parent = nodeStack.peek()!!
        val nodeType = node.getTokenType()
        val nodeText = getNodeText(node)
        when (nodeType) {
            MarkdownElementTypes.BULLET_LIST -> {
                nodeStack.push(ContentList())
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.ORDERED_LIST -> {
                nodeStack.push(ContentList()) // TODO: add list kind
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.HORIZONTAL_RULE -> {
            }
            MarkdownElementTypes.LIST_BLOCK -> {
                nodeStack.push(ContentBlock())
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.EMPH -> {
                nodeStack.push(ContentEmphasis())
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.STRONG -> {
                nodeStack.push(ContentStrong())
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.ANONYMOUS_SECTION -> {
                nodeStack.push(ContentSection(""))
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.NAMED_SECTION -> {
                val label = findChildByType(node, MarkdownElementTypes.SECTION_NAME)?.let { getNodeText(it) } ?: ""
                nodeStack.push(ContentSection(label))
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.PLAIN_TEXT -> {
                nodeStack.push(ContentText(nodeText))
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.END_LINE -> {
                nodeStack.push(ContentText(nodeText))
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            MarkdownElementTypes.BLANK_LINE -> {
                processChildren()
            }
            MarkdownElementTypes.PARA -> {
                nodeStack.push(ContentBlock())
                processChildren()
                parent.children.add(nodeStack.pop())
            }
            else -> {
                processChildren()
            }
        }
    }
    return nodeStack.pop() as Content
}