blob: 49a7c96b85d1a9837bc0b9cedd9f2a500b210c45 [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)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010013 abstract public fun appendParagraph(to: StringBuilder, text: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040014 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
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010031 public abstract fun formatStrikethrough(text: String): String
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040032 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040033 public abstract fun formatCode(code: String): String
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020034 public abstract fun formatList(text: String): String
35 public abstract fun formatListItem(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040036 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
37
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040038 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
39 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040040 }
41
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040042 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030043 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040044 when (content) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010045 is ContentText -> append(formatText(content.text))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040046 is ContentSymbol -> append(formatSymbol(content.text))
47 is ContentKeyword -> append(formatKeyword(content.text))
48 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040049 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010050 is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children)))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040051 is ContentCode -> append(formatCode(formatText(location, content.children)))
52 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020053 is ContentList -> append(formatList(formatText(location, content.children)))
54 is ContentListItem -> append(formatListItem(formatText(location, content.children)))
55
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040056 is ContentNodeLink -> {
57 val linkTo = locationService.relativeLocation(location, content.node, extension)
58 val linkText = formatText(location, content.children)
59 append(formatLink(linkText, linkTo))
60 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040061 is ContentExternalLink -> {
62 val linkText = formatText(location, content.children)
63 append(formatLink(linkText, content.href))
64 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040065 is ContentParagraph -> {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010066 appendParagraph(this, formatText(location, content.children))
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040067 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040068 is ContentBlockCode -> {
69 appendBlockCode(this, formatText(location, content.children))
70 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040071 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030072 }
73 }.toString()
74 }
75
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040076 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
77
78 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
79 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
80 }
81
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040082 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010083 val described = nodes.filter { it.hasDescription() }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040084 if (described.any()) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010085 val single = described.size() == 1
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040086 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040087 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040088 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040089 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040090 }
Ilya Ryzhenkov49077362014-10-14 19:53:13 +040091 appendLine(to, formatText(location, node.content.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040092 appendLine(to)
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040093 for ((label, section) in node.content.sections) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010094 if (!isDescriptionSection(label, node)) continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040095 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040096 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040097 }
98 }
99 }
100 }
101
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100102 private fun DocumentationNode.hasDescription() =
103 content.description != ContentEmpty || content.sections.any { isDescriptionSection(it.key, this) }
104
105 private fun isDescriptionSection(label: String, node: DocumentationNode): Boolean {
106 if (label.startsWith("$"))
107 return false
108 if (node.members.any { it.name == label })
109 return false
110 return true
111 }
112
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400113 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400114 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400115 formatText(location, node.summary)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400116 }
117
118 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400119 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400120 appendBlockCode(to, formatText(location, languageService.render(it)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +0100121 val deprecation = it.deprecation
122 if (deprecation != null) {
123 val deprecationParameter = deprecation.details(DocumentationNode.Kind.Parameter).firstOrNull()
124 val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull()
125 if (deprecationValue != null) {
126 to.append(formatStrong("Deprecated: "))
Dmitry Jemerov23af5e22015-01-12 15:57:04 +0100127 appendLine(to, formatText(deprecationValue.name.trim("\"")))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +0100128 } else {
129 appendLine(to, formatStrong("Deprecated"))
130 }
131 }
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400132 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400133 appendLine(to, summary)
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300134 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400135 }
136 }
137
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400138 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400139 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400140 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400141 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400142 appendSummary(location, to, items)
143 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400144 }
145 }
146
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400147 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400148 if (nodes.any()) {
149 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400150
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400151 val children = nodes.sortBy { it.name }
152 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400153
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400154 appendTable(to) {
155 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400156 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400157 appendTableRow(to) {
158 appendTableCell(to) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100159 to.append(formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400160 }
161 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400162 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400163 for ((summary, items) in breakdownBySummary) {
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100164 val signatureTexts = items map { signature ->
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100165 val signature = languageService.render(signature)
166 val signatureAsCode = ContentCode()
167 signatureAsCode.append(signature)
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100168 formatText(location, signatureAsCode)
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400169 }
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100170 signatureTexts.subList(0, signatureTexts.size()-1).forEach {
171 appendLine(to, it)
172 }
173 to.append(signatureTexts.last())
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400174 if (!summary.isEmpty()) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100175 to.append(summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400176 }
177 }
178 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400179 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400180 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400181 }
182 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400183 }
184 }
185
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400186 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
187 val breakdownByLocation = nodes.groupBy { node ->
188 formatBreadcrumbs(node.path.map { link(node, it) })
189 }
190
191 for ((breadcrumbs, items) in breakdownByLocation) {
192 appendLine(to, breadcrumbs)
193 appendLine(to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100194 appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400195 }
196
197 for (node in nodes) {
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100198 if (node.kind == DocumentationNode.Kind.ExternalClass) {
199 appendSection(location, "Extensions for ${node.name}", node.members, node, to)
200 continue
201 }
202
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400203 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
204 appendSection(location, "Types", node.members.filter {
205 it.kind in setOf(
206 DocumentationNode.Kind.Class,
207 DocumentationNode.Kind.Interface,
208 DocumentationNode.Kind.Enum,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100209 DocumentationNode.Kind.Object,
210 DocumentationNode.Kind.AnnotationClass)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400211 }, node, to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100212 appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400213 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
214 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
215 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100216 appendSection(location, "Class Object Properties", node.members(DocumentationNode.Kind.ClassObjectProperty), node, to)
217 appendSection(location, "Class Object Functions", node.members(DocumentationNode.Kind.ClassObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400218 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100219 appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400220 appendSection(location, "Other members", node.members.filter {
221 it.kind !in setOf(
222 DocumentationNode.Kind.Class,
223 DocumentationNode.Kind.Interface,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100224 DocumentationNode.Kind.Enum,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400225 DocumentationNode.Kind.Object,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100226 DocumentationNode.Kind.AnnotationClass,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400227 DocumentationNode.Kind.Constructor,
228 DocumentationNode.Kind.Property,
229 DocumentationNode.Kind.Package,
230 DocumentationNode.Kind.Function,
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100231 DocumentationNode.Kind.PropertyAccessor,
232 DocumentationNode.Kind.ClassObjectProperty,
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100233 DocumentationNode.Kind.ClassObjectFunction,
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100234 DocumentationNode.Kind.ExternalClass,
235 DocumentationNode.Kind.EnumItem
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400236 )
237 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400238 appendSection(location, "Extensions", node.extensions, node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100239 appendSection(location, "Inheritors",
240 node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400241 appendSection(location, "Links", node.links, node, to)
242
243 }
244 }
245
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400246 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
247 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
248
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400249 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400250 for (node in nodes) {
251 appendOutlineHeader(to, node)
252 if (node.members.any()) {
253 appendOutlineChildren(to, node.members)
254 }
255 }
256 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400257}