diff options
Diffstat (limited to 'src/Formats/MarkdownFormatService.kt')
-rw-r--r-- | src/Formats/MarkdownFormatService.kt | 109 |
1 files changed, 81 insertions, 28 deletions
diff --git a/src/Formats/MarkdownFormatService.kt b/src/Formats/MarkdownFormatService.kt index 9626f62c..ae1500f2 100644 --- a/src/Formats/MarkdownFormatService.kt +++ b/src/Formats/MarkdownFormatService.kt @@ -1,45 +1,98 @@ package org.jetbrains.dokka import org.jetbrains.dokka.DocumentationNode.Kind +import java.util.LinkedHashMap public class MarkdownFormatService(val locationService: LocationService, val signatureGenerator: SignatureGenerator) : FormatService { override val extension: String = "md" - override fun format(node: DocumentationNode, to: StringBuilder) { + override fun format(nodes: Iterable<DocumentationNode>, to: StringBuilder) { with (to) { - appendln(node.path.map { "[${it.name}](${locationService.relativeLocation(node, it, extension)})" }.joinToString(" / ")) - appendln() - appendln("# ${node.name}") - appendln(node.doc.summary) - appendln("```") - appendln(signatureGenerator.render(node)) - appendln("```") - appendln(node.doc.description) - appendln() - for (section in node.doc.sections) { - append("##### ") - append(section.label) - appendln() - append(section.text) + 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) } - if (node.members.any()) { - appendln("### Members") - appendln("| Name | Signature | Summary |") - appendln("|------|-----------|---------|") - for (member in node.members.sortBy { it.name }) { - val relativePath = locationService.relativeLocation(node, member, extension) - val displayName = when (member.kind) { - Kind.Constructor -> "*.init*" - else -> signatureGenerator.renderName(member).htmlEscape() + 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("|") } - append("|[${displayName}](${relativePath})") - append("|`${signatureGenerator.render(member)}`") - append("|${member.doc.summary} ") - appendln("|") + } + + } + } + } + + + private fun StringBuilder.formatLocation(nodes: Iterable<DocumentationNode>) { + val breakdown = nodes.groupByTo(LinkedHashMap()) { node -> + node.name + } + for ((name, items) in breakdown) { + appendln("# ${name}") + formatSummary(items) + } + } + + 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("```") + } + + 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() } } } } + } |