blob: 4285f8c9322ccfd078f085f90fcae67fbb61ba2b [file] [log] [blame]
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04001package org.jetbrains.dokka
2
3import java.util.LinkedHashMap
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04004
5public data class FormatLink(val text: String, val location: Location)
6
7public abstract class StructuredFormatService(val locationService: LocationService,
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +04008 val languageService: LanguageService) : FormatService {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04009
10 abstract public fun appendBlockCode(to: StringBuilder, line: String)
11 abstract public fun appendBlockCode(to: StringBuilder, lines: Iterable<String>)
12 abstract public fun appendHeader(to: StringBuilder, text: String, level: Int = 1)
13 abstract public fun appendText(to: StringBuilder, text: String)
14 abstract public fun appendLine(to: StringBuilder, text: String)
15 public abstract fun appendLine(to: StringBuilder)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040016
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040017 public abstract fun appendTable(to: StringBuilder, body: () -> Unit)
18 public abstract fun appendTableHeader(to: StringBuilder, body: () -> Unit)
19 public abstract fun appendTableBody(to: StringBuilder, body: () -> Unit)
20 public abstract fun appendTableRow(to: StringBuilder, body: () -> Unit)
21 public abstract fun appendTableCell(to: StringBuilder, body: () -> Unit)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040022
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040023 public abstract fun formatText(text: String): String
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040024 public abstract fun formatSymbol(text: String): String
25 public abstract fun formatKeyword(text: String): String
26 public abstract fun formatIdentifier(text: String): String
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040027 public abstract fun formatLink(text: String, location: Location): String
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040028 public abstract fun formatLink(text: String, href: String): String
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040029 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.location)
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040030 public abstract fun formatStrong(text: String): String
31 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040032 public abstract fun formatCode(code: String): String
33 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
34
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040035 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
36 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040037 }
38
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040039 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030040 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040041 when (content) {
42 is ContentText -> append(content.text)
43 is ContentSymbol -> append(formatSymbol(content.text))
44 is ContentKeyword -> append(formatKeyword(content.text))
45 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040046 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
47 is ContentCode -> append(formatCode(formatText(location, content.children)))
48 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040049 is ContentNodeLink -> {
50 val linkTo = locationService.relativeLocation(location, content.node, extension)
51 val linkText = formatText(location, content.children)
52 append(formatLink(linkText, linkTo))
53 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040054 is ContentExternalLink -> {
55 val linkText = formatText(location, content.children)
56 append(formatLink(linkText, content.href))
57 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040058 is ContentParagraph -> {
59 appendText(this, formatText(location, content.children))
60 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040061 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030062 }
63 }.toString()
64 }
65
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040066 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
67
68 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
69 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
70 }
71
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040072 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030073 val described = nodes.filter { !it.doc.isEmpty }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040074 if (described.any()) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040075 val single = described.size == 1
76 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040077 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040078 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040079 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040080 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040081 appendLine(to, formatText(location,node.doc.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040082 appendLine(to)
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040083 for ((label, section) in node.doc.sections) {
84 if (label.startsWith("$"))
85 continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040086 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040087 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040088 }
89 }
90 }
91 }
92
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040093 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040094 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
95 node.doc.summary
96 }
97
98 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040099 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400100 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400101 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400102 appendLine(to, formatText(location, summary))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300103 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400104 }
105 }
106
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400107 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400108 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400109 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400110 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400111 appendSummary(location, to, items)
112 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400113 }
114 }
115
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400116 private fun StructuredFormatService.appendSection(location : Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400117 if (nodes.any()) {
118 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400119
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400120 val children = nodes.sortBy { it.name }
121 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400122
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400123 appendTable(to) {
124 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400125 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400126 appendTableRow(to) {
127 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400128 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400129 }
130 appendTableCell(to) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400131 val breakdownBySummary = members.groupBy { formatText(location, it.doc.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400132 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400133 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400134 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400135 }
136
137 if (!summary.isEmpty()) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400138 appendText(to, summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400139 }
140 }
141 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400142 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400143 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400144 }
145 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400146 }
147 }
148
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400149 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
150 val breakdownByLocation = nodes.groupBy { node ->
151 formatBreadcrumbs(node.path.map { link(node, it) })
152 }
153
154 for ((breadcrumbs, items) in breakdownByLocation) {
155 appendLine(to, breadcrumbs)
156 appendLine(to)
157 appendLocation(location, to, items)
158 }
159
160 for (node in nodes) {
161 appendSection(location, "Members", node.members, node, to)
162 appendSection(location, "Extensions", node.extensions, node, to)
163 appendSection(location, "Inheritors", node.inheritors, node, to)
164 appendSection(location, "Links", node.links, node, to)
165
166 }
167 }
168
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400169 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
170 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
171
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400172 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400173 for (node in nodes) {
174 appendOutlineHeader(to, node)
175 if (node.members.any()) {
176 appendOutlineChildren(to, node.members)
177 }
178 }
179 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400180}