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


public open class MarkdownFormatService(locationService: LocationService, signatureGenerator: LanguageService)
: StructuredFormatService(locationService, signatureGenerator) {

    override val extension: String = "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 public fun formatCode(code: String): String {
        return "`$code`"
    }

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

    override public fun formatLink(text: String, location: Location): String {
        return "[${text}](${location.path})"
    }

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

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

    override public fun appendText(to: StringBuilder, text: String) {
        to.append(formatText(text))
    }

    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, lines: Iterable<String>) {
        appendLine(to)
        appendLine(to, "```")
        for (line in lines)
            to.appendln(line)
        appendLine(to, "```")
        appendLine(to)
    }

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

    var outlineLevel = 0
    override fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode) {
        val indent = "    ".repeat(outlineLevel)
        appendLine(to, "$indent- title: ${languageService.renderName(node)}")
        appendLine(to, "$indent  url: ${locationService.location(node).path}")
    }

    override fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>) {
        val indent = "    ".repeat(outlineLevel)
        appendLine(to, "$indent  content:")
        outlineLevel++
        for (node in nodes) {
            appendOutlineHeader(to, node)
            if (node.members.any()) {
                appendOutlineChildren(to, node.members)
            }
            appendLine(to)
        }
        outlineLevel--
    }
}