blob: 08b7e55d2d55c411f8f42b2bfc63240ba9f7cd03 [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
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020033 public abstract fun formatList(text: String): String
34 public abstract fun formatListItem(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040035 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
36
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040037 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
38 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040039 }
40
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040041 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030042 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040043 when (content) {
44 is ContentText -> append(content.text)
45 is ContentSymbol -> append(formatSymbol(content.text))
46 is ContentKeyword -> append(formatKeyword(content.text))
47 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040048 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
49 is ContentCode -> append(formatCode(formatText(location, content.children)))
50 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020051 is ContentList -> append(formatList(formatText(location, content.children)))
52 is ContentListItem -> append(formatListItem(formatText(location, content.children)))
53
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040054 is ContentNodeLink -> {
55 val linkTo = locationService.relativeLocation(location, content.node, extension)
56 val linkText = formatText(location, content.children)
57 append(formatLink(linkText, linkTo))
58 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040059 is ContentExternalLink -> {
60 val linkText = formatText(location, content.children)
61 append(formatLink(linkText, content.href))
62 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040063 is ContentParagraph -> {
64 appendText(this, formatText(location, content.children))
65 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040066 is ContentBlockCode -> {
67 appendBlockCode(this, formatText(location, content.children))
68 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040069 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030070 }
71 }.toString()
72 }
73
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040074 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
75
76 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
77 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
78 }
79
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040080 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040081 val described = nodes.filter { !it.content.isEmpty }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040082 if (described.any()) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040083 val single = described.size == 1
84 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040085 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040086 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040087 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040088 }
Ilya Ryzhenkov49077362014-10-14 19:53:13 +040089 appendLine(to, formatText(location, node.content.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040090 appendLine(to)
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040091 for ((label, section) in node.content.sections) {
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040092 if (label.startsWith("$"))
93 continue
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040094 if (node.members.any { it.name == label })
95 continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040096 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040097 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040098 }
99 }
100 }
101 }
102
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400103 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400104 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400105 formatText(location, node.summary)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400106 }
107
108 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400109 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400110 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400111 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400112 appendLine(to, summary)
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300113 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400114 }
115 }
116
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400117 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400118 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400119 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400120 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400121 appendSummary(location, to, items)
122 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400123 }
124 }
125
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400126 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400127 if (nodes.any()) {
128 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400129
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400130 val children = nodes.sortBy { it.name }
131 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400132
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400133 appendTable(to) {
134 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400135 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400136 appendTableRow(to) {
137 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400138 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400139 }
140 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400141 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400142 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400143 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400144 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400145 }
146
147 if (!summary.isEmpty()) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400148 appendText(to, summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400149 }
150 }
151 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400152 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400153 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400154 }
155 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400156 }
157 }
158
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400159 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
160 val breakdownByLocation = nodes.groupBy { node ->
161 formatBreadcrumbs(node.path.map { link(node, it) })
162 }
163
164 for ((breadcrumbs, items) in breakdownByLocation) {
165 appendLine(to, breadcrumbs)
166 appendLine(to)
167 appendLocation(location, to, items)
168 }
169
170 for (node in nodes) {
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400171 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
172 appendSection(location, "Types", node.members.filter {
173 it.kind in setOf(
174 DocumentationNode.Kind.Class,
175 DocumentationNode.Kind.Interface,
176 DocumentationNode.Kind.Enum,
177 DocumentationNode.Kind.Object)
178 }, node, to)
179 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
180 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
181 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
182 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
183 appendSection(location, "Other members", node.members.filter {
184 it.kind !in setOf(
185 DocumentationNode.Kind.Class,
186 DocumentationNode.Kind.Interface,
187 DocumentationNode.Kind.Object,
188 DocumentationNode.Kind.Constructor,
189 DocumentationNode.Kind.Property,
190 DocumentationNode.Kind.Package,
191 DocumentationNode.Kind.Function,
192 DocumentationNode.Kind.PropertyAccessor
193 )
194 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400195 appendSection(location, "Extensions", node.extensions, node, to)
196 appendSection(location, "Inheritors", node.inheritors, node, to)
197 appendSection(location, "Links", node.links, node, to)
198
199 }
200 }
201
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400202 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
203 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
204
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400205 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400206 for (node in nodes) {
207 appendOutlineHeader(to, node)
208 if (node.members.any()) {
209 appendOutlineChildren(to, node.members)
210 }
211 }
212 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400213}