diff options
Diffstat (limited to 'src/Formats/MarkdownFormatService.kt')
-rw-r--r-- | src/Formats/MarkdownFormatService.kt | 123 |
1 files changed, 40 insertions, 83 deletions
diff --git a/src/Formats/MarkdownFormatService.kt b/src/Formats/MarkdownFormatService.kt index 0fcfa444..2a91a4a3 100644 --- a/src/Formats/MarkdownFormatService.kt +++ b/src/Formats/MarkdownFormatService.kt @@ -1,98 +1,55 @@ package org.jetbrains.dokka -import org.jetbrains.dokka.DocumentationNode.Kind -import java.util.LinkedHashMap -public open class MarkdownFormatService(val locationService: LocationService, - val signatureGenerator: SignatureGenerator) : FormatService { +public open class MarkdownFormatService(locationService: LocationService, signatureGenerator: SignatureGenerator) +: StructuredFormatService(locationService, signatureGenerator) { + override val extension: String = "md" - override fun format(nodes: Iterable<DocumentationNode>, to: StringBuilder) { - with (to) { - val breakdown = nodes.groupByTo(LinkedHashMap()) { node -> - node.path.map { "[${it.name}](${locationService.relativeLocation(node, it, extension)})" }.joinToString(" / ") - } - for ((path, items) in breakdown) { - appendln(path) - appendln() - formatLocation(items) - } - for (node in nodes) { - if (node.members.any()) { - appendln("## Members") - appendln("| Name | Summary |") - appendln("|------|---------|") - val children = node.members.sortBy { it.name } - val membersMap = children.groupByTo(LinkedHashMap()) { locationService.relativeLocation(node, it, extension) } - for ((location, members) in membersMap) { - val mainMember = members.first() - val displayName = when (mainMember.kind) { - Kind.Constructor -> "*.init*" - else -> signatureGenerator.renderName(mainMember).htmlEscape() - } - append("|[${displayName}](${location})|") - append(members.groupByTo(LinkedHashMap()) { it.doc.summary }.map { group -> - val (summary, items) = group - StringBuilder { - if (!summary.isEmpty()) { - append("${summary}<br>") - } - for (item in items) { - append(" `${signatureGenerator.render(item)}`<br>") - } - }.toString() - }.joinToString("<br>")) - appendln("|") - } - } + override public fun formatBreadcrumbs(items: Iterable<FormatLink>): String { + return items.map { formatLink(it) }.joinToString(" / ") + } + + override public fun formatCode(code: String): String { + return "`$code`" + } - } - } + override public fun formatBold(text: String): String { + return "**$text**" } + override public fun formatLink(link: FormatLink): String { + return "[${link.text}](${link.location.path})" + } - private fun StringBuilder.formatLocation(nodes: Iterable<DocumentationNode>) { - val breakdown = nodes.groupByTo(LinkedHashMap()) { node -> - node.name - } - for ((name, items) in breakdown) { - appendln("# ${name}") - formatSummary(items) - } + override public fun appendLine(to: StringBuilder) { + to.appendln() } - private fun StringBuilder.formatSummary(nodes: Iterable<DocumentationNode>) { - val breakdown = nodes.groupByTo(LinkedHashMap()) { node -> - node.doc.summary - } - for ((summary, items) in breakdown) { - appendln(summary) - appendln("```") - for (item in items) - appendln(signatureGenerator.render(item)) - appendln("```") - } + override public fun appendLine(to: StringBuilder, text: String) { + to.appendln(text) + } - val described = nodes.filter { it.doc.hasDescription } - if (described.any()) { - appendln("## Description") - for (node in described) { - appendln("```") - appendln(signatureGenerator.render(node)) - appendln("```") - appendln(node.doc.description) - appendln() - for (section in node.doc.sections) { - append("**") - append(section.label) - append("**") - appendln() - append(section.text) - appendln() - appendln() - } - } - } + override public fun appendText(to: StringBuilder, text: String) { + to.append(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, "```") + for (line in lines) + appendLine(to, line) + appendLine(to, "```") + } + + override public fun appendBlockCode(to: StringBuilder, line: String) { + appendLine(to, "```") + appendLine(to, line) + appendLine(to, "```") + } } |