blob: 60d5b7f191fa09a5b7532b49279bca6d6e039f14 [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>) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010081 val described = nodes.filter { it.hasDescription() }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040082 if (described.any()) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010083 val single = described.size() == 1
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040084 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) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010092 if (!isDescriptionSection(label, node)) continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040093 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040094 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040095 }
96 }
97 }
98 }
99
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100100 private fun DocumentationNode.hasDescription() =
101 content.description != ContentEmpty || content.sections.any { isDescriptionSection(it.key, this) }
102
103 private fun isDescriptionSection(label: String, node: DocumentationNode): Boolean {
104 if (label.startsWith("$"))
105 return false
106 if (node.members.any { it.name == label })
107 return false
108 return true
109 }
110
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400111 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400112 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400113 formatText(location, node.summary)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400114 }
115
116 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400117 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400118 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400119 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400120 appendLine(to, summary)
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300121 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400122 }
123 }
124
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400125 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400126 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400127 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400128 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400129 appendSummary(location, to, items)
130 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400131 }
132 }
133
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400134 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400135 if (nodes.any()) {
136 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400137
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400138 val children = nodes.sortBy { it.name }
139 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400140
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400141 appendTable(to) {
142 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400143 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400144 appendTableRow(to) {
145 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400146 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400147 }
148 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400149 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400150 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400151 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400152 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400153 }
154
155 if (!summary.isEmpty()) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400156 appendText(to, summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400157 }
158 }
159 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400160 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400161 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400162 }
163 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400164 }
165 }
166
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400167 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
168 val breakdownByLocation = nodes.groupBy { node ->
169 formatBreadcrumbs(node.path.map { link(node, it) })
170 }
171
172 for ((breadcrumbs, items) in breakdownByLocation) {
173 appendLine(to, breadcrumbs)
174 appendLine(to)
175 appendLocation(location, to, items)
176 }
177
178 for (node in nodes) {
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400179 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
180 appendSection(location, "Types", node.members.filter {
181 it.kind in setOf(
182 DocumentationNode.Kind.Class,
183 DocumentationNode.Kind.Interface,
184 DocumentationNode.Kind.Enum,
185 DocumentationNode.Kind.Object)
186 }, node, to)
187 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
188 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
189 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100190 appendSection(location, "Class Object Properties", node.members(DocumentationNode.Kind.ClassObjectProperty), node, to)
191 appendSection(location, "Class Object Functions", node.members(DocumentationNode.Kind.ClassObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400192 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
193 appendSection(location, "Other members", node.members.filter {
194 it.kind !in setOf(
195 DocumentationNode.Kind.Class,
196 DocumentationNode.Kind.Interface,
197 DocumentationNode.Kind.Object,
198 DocumentationNode.Kind.Constructor,
199 DocumentationNode.Kind.Property,
200 DocumentationNode.Kind.Package,
201 DocumentationNode.Kind.Function,
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100202 DocumentationNode.Kind.PropertyAccessor,
203 DocumentationNode.Kind.ClassObjectProperty,
204 DocumentationNode.Kind.ClassObjectFunction
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400205 )
206 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400207 appendSection(location, "Extensions", node.extensions, node, to)
208 appendSection(location, "Inheritors", node.inheritors, node, to)
209 appendSection(location, "Links", node.links, node, to)
210
211 }
212 }
213
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400214 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
215 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
216
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400217 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400218 for (node in nodes) {
219 appendOutlineHeader(to, node)
220 if (node.members.any()) {
221 appendOutlineChildren(to, node.members)
222 }
223 }
224 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400225}