aboutsummaryrefslogtreecommitdiff
path: root/src/Markdown/MarkdownProcessor.kt
blob: 05c4a7ecf03c483b1083c67773cbeb12a3e67817 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package org.jetbrains.dokka

import org.intellij.markdown.*
import org.intellij.markdown.ast.*
import org.intellij.markdown.parser.*
import org.intellij.markdown.parser.dialects.KDocMarkerProcessor

class MarkdownNode(val node: ASTNode, val parent: MarkdownNode?, val markdown: String) {
    val children: List<MarkdownNode> = node.children.map { MarkdownNode(it, this, markdown) }
    val endOffset: Int get() = node.endOffset
    val startOffset: Int get() = node.startOffset
    val type: IElementType get() = node.type
    val text: String get() = markdown.substring(startOffset, endOffset)
    fun child(type: IElementType): MarkdownNode? = children.firstOrNull { it.type == type }

    override fun toString(): String = present()
}

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.present() = StringBuilder { presentTo(this) }.toString()
private fun MarkdownNode.presentTo(sb: StringBuilder) {
    sb.append(type.toString())
    sb.append(":" + text.replace("\n", "\u23CE"))
    sb.appendln()
}

public fun MarkdownNode.toHtml(): String {
    val sb = StringBuilder()
    visit {(node, processChildren) ->
        val nodeType = node.type
        val nodeText = node.text
        when (nodeType) {
            MarkdownElementTypes.UNORDERED_LIST -> {
                sb.appendln("<ul>")
                processChildren()
                sb.appendln("</ul>")
            }
            MarkdownElementTypes.ORDERED_LIST -> {
                sb.appendln("<ol>")
                processChildren()
                sb.appendln("</ol>")
            }
            MarkdownElementTypes.LIST_ITEM -> {
                sb.append("<li>")
                processChildren()
                sb.appendln("</li>")
            }
            MarkdownElementTypes.EMPH -> {
                sb.append("<em>")
                processChildren()
                sb.append("</em>")
            }
            MarkdownElementTypes.STRONG -> {
                sb.append("<strong>")
                processChildren()
                sb.append("</strong>")
            }
            MarkdownElementTypes.ATX_1 -> {
                sb.append("<h1>")
                processChildren()
                sb.append("</h1>")
            }
            MarkdownElementTypes.ATX_2 -> {
                sb.append("<h2>")
                processChildren()
                sb.append("</h2>")
            }
            MarkdownElementTypes.ATX_3 -> {
                sb.append("<h3>")
                processChildren()
                sb.append("</h3>")
            }
            MarkdownElementTypes.ATX_4 -> {
                sb.append("<h4>")
                processChildren()
                sb.append("</h4>")
            }
            MarkdownElementTypes.ATX_5 -> {
                sb.append("<h5>")
                processChildren()
                sb.append("</h5>")
            }
            MarkdownElementTypes.ATX_6 -> {
                sb.append("<h6>")
                processChildren()
                sb.append("</h6>")
            }
            MarkdownElementTypes.BLOCK_QUOTE -> {
                sb.append("<blockquote>")
                processChildren()
                sb.append("</blockquote>")
            }
            MarkdownElementTypes.PARAGRAPH -> {
                sb.append("<p>")
                processChildren()
                sb.appendln("</p>")
            }
            MarkdownTokenTypes.CODE -> {
                sb.append("<pre><code>")
                sb.append(nodeText)
                sb.append("</code><pre>")
            }
            MarkdownTokenTypes.TEXT -> {
                sb.append(nodeText)
            }
            else -> {
                processChildren()
            }
        }
    }
    return sb.toString()
}

fun parseMarkdown(markdown: String): MarkdownNode {
    if (markdown.isEmpty())
        return MarkdownNode(LeafASTNode(MarkdownElementTypes.MARKDOWN_FILE, 0, 0), null, markdown)
    return MarkdownNode(MarkdownParser(KDocMarkerProcessor.Factory()).buildMarkdownTreeFromString(markdown), null, markdown)
}

fun markdownToHtml(markdown: String): String {

    val tree = MarkdownParser(KDocMarkerProcessor.Factory()).buildMarkdownTreeFromString(markdown)
    val markdownTree = MarkdownNode(tree, null, markdown)
    val ast = markdownTree.toTestString()
    return markdownTree.toHtml()
}