aboutsummaryrefslogtreecommitdiff
path: root/src/Formats/MarkdownFormatService.kt
blob: ba7d8f7b087df68d9f040fdd0b3b7c8b572c5805 (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
package org.jetbrains.dokka


public open class MarkdownFormatService(locationService: LocationService,
                                        signatureGenerator: LanguageService)
: StructuredFormatService(locationService, signatureGenerator, "md") {
    override public fun formatBreadcrumbs(items: Iterable<FormatLink>): String {
        return items.map { formatLink(it) }.joinToString(" / ")
    }

    override public fun formatText(text: String): String {
        return text.htmlEscape()
    }

    override fun formatSymbol(text: String): String {
        return text.htmlEscape()
    }

    override fun formatKeyword(text: String): String {
        return text.htmlEscape()
    }
    override fun formatIdentifier(text: String, kind: IdentifierKind): String {
        return text.htmlEscape()
    }

    override public fun formatCode(code: String): String {
        return "`$code`"
    }

    override public fun formatUnorderedList(text: String): String = text + "\n"
    override public fun formatOrderedList(text: String): String = text + "\n"

    override fun formatListItem(text: String, kind: ListKind): String {
        val itemText = if (text.endsWith("\n")) text else text + "\n"
        return if (kind == ListKind.Unordered) "* $itemText" else "1. $itemText"
    }

    override public fun formatStrong(text: String): String {
        return "**$text**"
    }

    override fun formatEmphasis(text: String): String {
        return "*$text*"
    }

    override fun formatStrikethrough(text: String): String {
        return "~~$text~~"
    }

    override fun formatLink(text: String, href: String): String {
        return "[$text]($href)"
    }

    override public fun appendLine(to: StringBuilder) {
        to.appendln()
    }

    override public fun appendLine(to: StringBuilder, text: String) {
        to.appendln(text)
    }

    override fun appendAnchor(to: StringBuilder, anchor: String) {
        // no anchors in Markdown
    }

    override public fun appendParagraph(to: StringBuilder, text: String) {
        to.appendln()
        to.appendln(text)
        to.appendln()
    }

    override public fun appendHeader(to: StringBuilder, text: String, level: Int) {
        appendLine(to)
        appendLine(to, "${"#".repeat(level)} $text")
        appendLine(to)
    }

    override public fun appendBlockCode(to: StringBuilder, line: String, language: String) {
        appendLine(to)
        to.appendln("``` ${language}")
        to.appendln(line)
        to.appendln("```")
        appendLine(to)
    }

    override fun appendTable(to: StringBuilder, body: () -> Unit) {
        to.appendln()
        body()
        to.appendln()
    }

    override fun appendTableHeader(to: StringBuilder, body: () -> Unit) {
        body()
    }

    override fun appendTableBody(to: StringBuilder, body: () -> Unit) {
        body()
    }

    override fun appendTableRow(to: StringBuilder, body: () -> Unit) {
        to.append("|")
        body()
        to.appendln()
    }

    override fun appendTableCell(to: StringBuilder, body: () -> Unit) {
        to.append(" ")
        body()
        to.append(" |")
    }

    override fun formatNonBreakingSpace(): String = "&nbsp;"
}