blob: b974dcf83eb10797d594f0786c6185a25c7433ee [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
28 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.location)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040029 public abstract fun formatBold(text: String): String
30 public abstract fun formatCode(code: String): String
31 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
32
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040033 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
34 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040035 }
36
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040037 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030038 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040039 when (content) {
40 is ContentText -> append(content.text)
41 is ContentSymbol -> append(formatSymbol(content.text))
42 is ContentKeyword -> append(formatKeyword(content.text))
43 is ContentIdentifier -> append(formatIdentifier(content.text))
44 is ContentEmphasis -> append(formatBold(formatText(location, content.children)))
45 is ContentNodeLink -> {
46 val linkTo = locationService.relativeLocation(location, content.node, extension)
47 val linkText = formatText(location, content.children)
48 append(formatLink(linkText, linkTo))
49 }
50 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030051 }
52 }.toString()
53 }
54
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040055 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
56
57 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
58 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
59 }
60
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040061 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030062 val described = nodes.filter { !it.doc.isEmpty }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040063 if (described.any()) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040064 val single = described.size == 1
65 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040066 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040067 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040068 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040069 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040070 appendLine(to, formatText(location,node.doc.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040071 appendLine(to)
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040072 for ((label, section) in node.doc.sections) {
73 if (label.startsWith("$"))
74 continue
75 appendLine(to, formatBold(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040076 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040077 appendLine(to)
78 }
79 }
80 }
81 }
82
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040083 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040084 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
85 node.doc.summary
86 }
87
88 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040089 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040090 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040091 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040092 appendLine(to, formatText(location, summary))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030093 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040094 }
95 }
96
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040097 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +040098 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040099 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400100 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400101 appendSummary(location, to, items)
102 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400103 }
104 }
105
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400106 private fun StructuredFormatService.appendSection(location : Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400107 if (nodes.any()) {
108 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400109
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400110 val children = nodes.sortBy { it.name }
111 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400112
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400113 appendTable(to) {
114 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400115 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400116 appendTableRow(to) {
117 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400118 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400119 }
120 appendTableCell(to) {
121 val breakdownBySummary = members.groupBy { it.doc.summary }
122 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400123 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400124 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400125 }
126
127 if (!summary.isEmpty()) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400128 appendText(to, formatText(location, summary))
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400129 }
130 }
131 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400132 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400133 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400134 }
135 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400136 }
137 }
138
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400139 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
140 val breakdownByLocation = nodes.groupBy { node ->
141 formatBreadcrumbs(node.path.map { link(node, it) })
142 }
143
144 for ((breadcrumbs, items) in breakdownByLocation) {
145 appendLine(to, breadcrumbs)
146 appendLine(to)
147 appendLocation(location, to, items)
148 }
149
150 for (node in nodes) {
151 appendSection(location, "Members", node.members, node, to)
152 appendSection(location, "Extensions", node.extensions, node, to)
153 appendSection(location, "Inheritors", node.inheritors, node, to)
154 appendSection(location, "Links", node.links, node, to)
155
156 }
157 }
158
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400159 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
160 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
161
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400162 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400163 for (node in nodes) {
164 appendOutlineHeader(to, node)
165 if (node.members.any()) {
166 appendOutlineChildren(to, node.members)
167 }
168 }
169 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400170}