blob: d2e2436baaffe0190a5b316201f2188c16406354 [file] [log] [blame]
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04001package org.jetbrains.dokka
2
3import java.util.LinkedHashMap
Dmitry Jemerov0c584d02015-01-12 17:23:07 +01004import org.jetbrains.dokka.LanguageService.RenderMode
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04005
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +01006public data class FormatLink(val text: String, val href: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04007
Dmitry Jemerovea1f4cc2015-02-19 19:51:01 +01008public abstract class StructuredFormatService(locationService: LocationService,
9 val languageService: LanguageService,
10 override val extension: String) : FormatService {
11 val locationService: LocationService = locationService.withExtension(extension)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040012
13 abstract public fun appendBlockCode(to: StringBuilder, line: String)
14 abstract public fun appendBlockCode(to: StringBuilder, lines: Iterable<String>)
15 abstract public fun appendHeader(to: StringBuilder, text: String, level: Int = 1)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010016 abstract public fun appendParagraph(to: StringBuilder, text: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040017 abstract public fun appendLine(to: StringBuilder, text: String)
18 public abstract fun appendLine(to: StringBuilder)
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +010019 public abstract fun appendAnchor(to: StringBuilder, anchor: String)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040020
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040021 public abstract fun appendTable(to: StringBuilder, body: () -> Unit)
22 public abstract fun appendTableHeader(to: StringBuilder, body: () -> Unit)
23 public abstract fun appendTableBody(to: StringBuilder, body: () -> Unit)
24 public abstract fun appendTableRow(to: StringBuilder, body: () -> Unit)
25 public abstract fun appendTableCell(to: StringBuilder, body: () -> Unit)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040026
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040027 public abstract fun formatText(text: String): String
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040028 public abstract fun formatSymbol(text: String): String
29 public abstract fun formatKeyword(text: String): String
30 public abstract fun formatIdentifier(text: String): String
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040031 public abstract fun formatLink(text: String, href: String): String
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +010032 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href)
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040033 public abstract fun formatStrong(text: String): String
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010034 public abstract fun formatStrikethrough(text: String): String
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040035 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040036 public abstract fun formatCode(code: String): String
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020037 public abstract fun formatList(text: String): String
38 public abstract fun formatListItem(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040039 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
40
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040041 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
42 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040043 }
44
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040045 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030046 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040047 when (content) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010048 is ContentText -> append(formatText(content.text))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040049 is ContentSymbol -> append(formatSymbol(content.text))
50 is ContentKeyword -> append(formatKeyword(content.text))
51 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040052 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010053 is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children)))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040054 is ContentCode -> append(formatCode(formatText(location, content.children)))
55 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020056 is ContentList -> append(formatList(formatText(location, content.children)))
57 is ContentListItem -> append(formatListItem(formatText(location, content.children)))
58
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040059 is ContentNodeLink -> {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +010060 val linkTo = locationHref(location, content.node)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040061 val linkText = formatText(location, content.children)
62 append(formatLink(linkText, linkTo))
63 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040064 is ContentExternalLink -> {
65 val linkText = formatText(location, content.children)
66 append(formatLink(linkText, content.href))
67 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040068 is ContentParagraph -> {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010069 appendParagraph(this, formatText(location, content.children))
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040070 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040071 is ContentBlockCode -> {
72 appendBlockCode(this, formatText(location, content.children))
73 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010074 is ContentBlock -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030075 }
76 }.toString()
77 }
78
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040079 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
80
81 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
Dmitry Jemerovea1f4cc2015-02-19 19:51:01 +010082 return FormatLink(to.name, locationService.relativePathToLocation(from, to))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040083 }
84
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +010085 fun locationHref(from: Location, to: DocumentationNode): String {
86 val topLevelPage = to.references(DocumentationReference.Kind.TopLevelPage).singleOrNull()?.to
87 if (topLevelPage != null) {
88 return from.relativePathTo(locationService.location(topLevelPage), to.name)
89 }
90 return from.relativePathTo(locationService.location(to))
91 }
92
Dmitry Jemerove1a38842015-02-10 18:55:12 +010093 fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) {
94 val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content }
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +010095
Dmitry Jemerove1a38842015-02-10 18:55:12 +010096 for ((summary, items) in breakdownBySummary) {
97 items.forEach {
Dmitry Jemerovebbf2652015-02-10 19:35:27 +010098 to.append(formatCode(formatText(location, languageService.render(it))))
99 it.appendSourceLink(to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100100 it.appendOverrides(to)
101 it.appendDeprecation(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400102 }
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100103 // All items have exactly the same documentation, so we can use any item to render it
104 val item = items.first()
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100105 to.append(formatText(location, item.content.summary))
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100106 appendDescription(location, to, item)
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100107 appendLine(to)
108 appendLine(to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100109 }
110 }
111
112 fun appendDescription(location: Location, to: StringBuilder, node: DocumentationNode) {
113 if (node.content.description != ContentEmpty) {
114 appendHeader(to, "Description", 3)
115 appendLine(to, formatText(location, node.content.description))
116 appendLine(to)
117 }
118 node.content.getSectionsWithSubjects().forEach {
119 appendSectionWithSubject(it.getKey(), location, it.getValue(), to)
120 }
121
122 for (section in node.content.sections.filter { it.subjectName == null }) {
123 appendLine(to, formatStrong(formatText(section.tag)))
124 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400125 }
126 }
127
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100128 fun Content.getSectionsWithSubjects(): Map<String, List<ContentSection>> =
129 sections.filter { it.subjectName != null }.groupBy { it.tag }
130
131 fun appendSectionWithSubject(title: String, location: Location, subjectSections: List<ContentSection>, to: StringBuilder) {
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100132 appendHeader(to, title, 3)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100133 subjectSections.forEach {
134 val subjectName = it.subjectName
135 if (subjectName != null) {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100136 appendAnchor(to, subjectName)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100137 to.append(formatCode(subjectName)).append(" - ")
138 to.append(formatText(location, it))
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100139 appendLine(to)
140 }
141 }
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100142 }
143
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100144 private fun DocumentationNode.appendOverrides(to: StringBuilder) {
145 overrides.forEach {
146 to.append("Overrides ")
Dmitry Jemerovea1f4cc2015-02-19 19:51:01 +0100147 val location = locationService.relativePathToLocation(this, it)
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100148 appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location)))
149 }
150 }
151
152 private fun DocumentationNode.appendDeprecation(to: StringBuilder) {
153 if (deprecation != null) {
154 val deprecationParameter = deprecation!!.details(DocumentationNode.Kind.Parameter).firstOrNull()
155 val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull()
156 if (deprecationValue != null) {
157 to.append(formatStrong("Deprecated: "))
158 appendLine(to, formatText(deprecationValue.name.trim("\"")))
159 } else {
160 appendLine(to, formatStrong("Deprecated"))
161 }
162 }
163 }
164
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100165 private fun DocumentationNode.appendSourceLink(to: StringBuilder) {
166 val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull()
167 if (sourceUrl != null) {
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100168 to.append(" ")
169 appendLine(to, formatLink("(source)", sourceUrl.name))
170 } else {
171 appendLine(to)
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100172 }
173 }
174
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400175 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400176 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400177 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400178 appendHeader(to, formatText(name))
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100179 appendDocumentation(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400180 }
181 }
182
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400183 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400184 if (nodes.any()) {
185 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400186
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400187 val children = nodes.sortBy { it.name }
188 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400189
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400190 appendTable(to) {
191 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400192 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400193 appendTableRow(to) {
194 appendTableCell(to) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100195 to.append(formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400196 }
197 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400198 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400199 for ((summary, items) in breakdownBySummary) {
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100200 val signatureTexts = items map { signature ->
Dmitry Jemerov0c584d02015-01-12 17:23:07 +0100201 val signature = languageService.render(signature, RenderMode.SUMMARY)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100202 val signatureAsCode = ContentCode()
203 signatureAsCode.append(signature)
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100204 formatText(location, signatureAsCode)
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400205 }
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100206 signatureTexts.subList(0, signatureTexts.size()-1).forEach {
207 appendLine(to, it)
208 }
209 to.append(signatureTexts.last())
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400210 if (!summary.isEmpty()) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100211 to.append(summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400212 }
213 }
214 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400215 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400216 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400217 }
218 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400219 }
220 }
221
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400222 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
223 val breakdownByLocation = nodes.groupBy { node ->
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100224 formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400225 }
226
227 for ((breadcrumbs, items) in breakdownByLocation) {
228 appendLine(to, breadcrumbs)
229 appendLine(to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100230 appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400231 }
232
233 for (node in nodes) {
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100234 if (node.kind == DocumentationNode.Kind.ExternalClass) {
235 appendSection(location, "Extensions for ${node.name}", node.members, node, to)
236 continue
237 }
238
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400239 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
240 appendSection(location, "Types", node.members.filter {
241 it.kind in setOf(
242 DocumentationNode.Kind.Class,
243 DocumentationNode.Kind.Interface,
244 DocumentationNode.Kind.Enum,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100245 DocumentationNode.Kind.Object,
246 DocumentationNode.Kind.AnnotationClass)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400247 }, node, to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100248 appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400249 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
250 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
251 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerov2822a3e2015-02-17 12:32:17 +0100252 appendSection(location, "Default Object Properties", node.members(DocumentationNode.Kind.DefaultObjectProperty), node, to)
253 appendSection(location, "Default Object Functions", node.members(DocumentationNode.Kind.DefaultObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400254 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100255 appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400256 appendSection(location, "Other members", node.members.filter {
257 it.kind !in setOf(
258 DocumentationNode.Kind.Class,
259 DocumentationNode.Kind.Interface,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100260 DocumentationNode.Kind.Enum,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400261 DocumentationNode.Kind.Object,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100262 DocumentationNode.Kind.AnnotationClass,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400263 DocumentationNode.Kind.Constructor,
264 DocumentationNode.Kind.Property,
265 DocumentationNode.Kind.Package,
266 DocumentationNode.Kind.Function,
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100267 DocumentationNode.Kind.PropertyAccessor,
Dmitry Jemerov2822a3e2015-02-17 12:32:17 +0100268 DocumentationNode.Kind.DefaultObjectProperty,
269 DocumentationNode.Kind.DefaultObjectFunction,
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100270 DocumentationNode.Kind.ExternalClass,
271 DocumentationNode.Kind.EnumItem
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400272 )
273 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400274 appendSection(location, "Extensions", node.extensions, node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100275 appendSection(location, "Inheritors",
276 node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400277 appendSection(location, "Links", node.links, node, to)
278
279 }
280 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400281}