Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 1 | package org.jetbrains.dokka |
| 2 | |
| 3 | import java.util.LinkedHashMap |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 4 | |
| 5 | public data class FormatLink(val text: String, val location: Location) |
| 6 | |
| 7 | public abstract class StructuredFormatService(val locationService: LocationService, |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 8 | val languageService: LanguageService) : FormatService { |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 9 | |
| 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 Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 13 | abstract public fun appendParagraph(to: StringBuilder, text: String) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 14 | abstract public fun appendLine(to: StringBuilder, text: String) |
| 15 | public abstract fun appendLine(to: StringBuilder) |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 16 | |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 17 | 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 Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 22 | |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 23 | public abstract fun formatText(text: String): String |
Ilya Ryzhenkov | 7c6da4b | 2014-10-03 19:09:31 +0400 | [diff] [blame] | 24 | public abstract fun formatSymbol(text: String): String |
| 25 | public abstract fun formatKeyword(text: String): String |
| 26 | public abstract fun formatIdentifier(text: String): String |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 27 | public abstract fun formatLink(text: String, location: Location): String |
Ilya Ryzhenkov | 71cd87e | 2014-10-03 22:51:44 +0400 | [diff] [blame] | 28 | public abstract fun formatLink(text: String, href: String): String |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 29 | public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.location) |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 30 | public abstract fun formatStrong(text: String): String |
| 31 | public abstract fun formatEmphasis(text: String): String |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 32 | public abstract fun formatCode(code: String): String |
Ilya Ryzhenkov | 1839949 | 2014-12-22 09:50:17 +0200 | [diff] [blame] | 33 | public abstract fun formatList(text: String): String |
| 34 | public abstract fun formatListItem(text: String): String |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 35 | public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String |
| 36 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 37 | open fun formatText(location: Location, nodes: Iterable<ContentNode>): String { |
| 38 | return nodes.map { formatText(location, it) }.join("") |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 39 | } |
| 40 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 41 | open fun formatText(location: Location, content: ContentNode): String { |
Ilya Ryzhenkov | 455d74a | 2014-09-19 22:25:27 +0300 | [diff] [blame] | 42 | return StringBuilder { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 43 | when (content) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 44 | is ContentText -> append(formatText(content.text)) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 45 | is ContentSymbol -> append(formatSymbol(content.text)) |
| 46 | is ContentKeyword -> append(formatKeyword(content.text)) |
| 47 | is ContentIdentifier -> append(formatIdentifier(content.text)) |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 48 | 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 Ryzhenkov | 1839949 | 2014-12-22 09:50:17 +0200 | [diff] [blame] | 51 | is ContentList -> append(formatList(formatText(location, content.children))) |
| 52 | is ContentListItem -> append(formatListItem(formatText(location, content.children))) |
| 53 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 54 | 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 Ryzhenkov | 71cd87e | 2014-10-03 22:51:44 +0400 | [diff] [blame] | 59 | is ContentExternalLink -> { |
| 60 | val linkText = formatText(location, content.children) |
| 61 | append(formatLink(linkText, content.href)) |
| 62 | } |
Ilya Ryzhenkov | ad14ea9 | 2014-10-13 18:22:02 +0400 | [diff] [blame] | 63 | is ContentParagraph -> { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 64 | appendParagraph(this, formatText(location, content.children)) |
Ilya Ryzhenkov | ad14ea9 | 2014-10-13 18:22:02 +0400 | [diff] [blame] | 65 | } |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 66 | is ContentBlockCode -> { |
| 67 | appendBlockCode(this, formatText(location, content.children)) |
| 68 | } |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 69 | else -> append(formatText(location, content.children)) |
Ilya Ryzhenkov | 455d74a | 2014-09-19 22:25:27 +0300 | [diff] [blame] | 70 | } |
| 71 | }.toString() |
| 72 | } |
| 73 | |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 74 | 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 Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 80 | fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 81 | val described = nodes.filter { it.hasDescription() } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 82 | if (described.any()) { |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 83 | val single = described.size() == 1 |
Ilya Ryzhenkov | fb41c69 | 2014-07-15 18:23:15 +0400 | [diff] [blame] | 84 | appendHeader(to, "Description", 3) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 85 | for (node in described) { |
Ilya Ryzhenkov | fb41c69 | 2014-07-15 18:23:15 +0400 | [diff] [blame] | 86 | if (!single) { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 87 | appendBlockCode(to, formatText(location, languageService.render(node))) |
Ilya Ryzhenkov | fb41c69 | 2014-07-15 18:23:15 +0400 | [diff] [blame] | 88 | } |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 89 | appendLine(to, formatText(location, node.content.description)) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 90 | appendLine(to) |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 91 | for ((label, section) in node.content.sections) { |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 92 | if (!isDescriptionSection(label, node)) continue |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 93 | appendLine(to, formatStrong(formatText(label))) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 94 | appendLine(to, formatText(location, section)) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 100 | 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 Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 111 | fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 112 | val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node -> |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 113 | formatText(location, node.summary) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | for ((summary, items) in breakdownBySummary) { |
Ilya Ryzhenkov | 7c6da4b | 2014-10-03 19:09:31 +0400 | [diff] [blame] | 117 | items.forEach { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 118 | appendBlockCode(to, formatText(location, languageService.render(it))) |
Ilya Ryzhenkov | 7c6da4b | 2014-10-03 19:09:31 +0400 | [diff] [blame] | 119 | } |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 120 | appendLine(to, summary) |
Ilya Ryzhenkov | 455d74a | 2014-09-19 22:25:27 +0300 | [diff] [blame] | 121 | appendLine(to) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 125 | fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Ilya Ryzhenkov | bd494a8 | 2014-08-21 19:53:36 +0400 | [diff] [blame] | 126 | val breakdownByName = nodes.groupBy { node -> node.name } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 127 | for ((name, items) in breakdownByName) { |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 128 | appendHeader(to, formatText(name)) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 129 | appendSummary(location, to, items) |
| 130 | appendDescription(location, to, items) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 134 | private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) { |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 135 | if (nodes.any()) { |
| 136 | appendHeader(to, caption, 3) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 137 | |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 138 | val children = nodes.sortBy { it.name } |
| 139 | val membersMap = children.groupBy { link(node, it) } |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 140 | |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 141 | appendTable(to) { |
| 142 | appendTableBody(to) { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 143 | for ((memberLocation, members) in membersMap) { |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 144 | appendTableRow(to) { |
| 145 | appendTableCell(to) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 146 | to.append(formatLink(memberLocation)) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 147 | } |
| 148 | appendTableCell(to) { |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 149 | val breakdownBySummary = members.groupBy { formatText(location, it.summary) } |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 150 | for ((summary, items) in breakdownBySummary) { |
Dmitry Jemerov | 3fc3e33 | 2014-12-30 15:35:00 +0100 | [diff] [blame^] | 151 | val signatureTexts = items map { signature -> |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 152 | val signature = languageService.render(signature) |
| 153 | val signatureAsCode = ContentCode() |
| 154 | signatureAsCode.append(signature) |
Dmitry Jemerov | 3fc3e33 | 2014-12-30 15:35:00 +0100 | [diff] [blame^] | 155 | formatText(location, signatureAsCode) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 156 | } |
Dmitry Jemerov | 3fc3e33 | 2014-12-30 15:35:00 +0100 | [diff] [blame^] | 157 | signatureTexts.subList(0, signatureTexts.size()-1).forEach { |
| 158 | appendLine(to, it) |
| 159 | } |
| 160 | to.append(signatureTexts.last()) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 161 | if (!summary.isEmpty()) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 162 | to.append(summary) |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
Ilya Ryzhenkov | e8447fd | 2014-07-15 16:37:50 +0400 | [diff] [blame] | 166 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 167 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 168 | } |
| 169 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 173 | override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
| 174 | val breakdownByLocation = nodes.groupBy { node -> |
| 175 | formatBreadcrumbs(node.path.map { link(node, it) }) |
| 176 | } |
| 177 | |
| 178 | for ((breadcrumbs, items) in breakdownByLocation) { |
| 179 | appendLine(to, breadcrumbs) |
| 180 | appendLine(to) |
| 181 | appendLocation(location, to, items) |
| 182 | } |
| 183 | |
| 184 | for (node in nodes) { |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 185 | appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to) |
| 186 | appendSection(location, "Types", node.members.filter { |
| 187 | it.kind in setOf( |
| 188 | DocumentationNode.Kind.Class, |
| 189 | DocumentationNode.Kind.Interface, |
| 190 | DocumentationNode.Kind.Enum, |
| 191 | DocumentationNode.Kind.Object) |
| 192 | }, node, to) |
| 193 | appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to) |
| 194 | appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to) |
| 195 | appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to) |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 196 | appendSection(location, "Class Object Properties", node.members(DocumentationNode.Kind.ClassObjectProperty), node, to) |
| 197 | appendSection(location, "Class Object Functions", node.members(DocumentationNode.Kind.ClassObjectFunction), node, to) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 198 | appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to) |
| 199 | appendSection(location, "Other members", node.members.filter { |
| 200 | it.kind !in setOf( |
| 201 | DocumentationNode.Kind.Class, |
| 202 | DocumentationNode.Kind.Interface, |
| 203 | DocumentationNode.Kind.Object, |
| 204 | DocumentationNode.Kind.Constructor, |
| 205 | DocumentationNode.Kind.Property, |
| 206 | DocumentationNode.Kind.Package, |
| 207 | DocumentationNode.Kind.Function, |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 208 | DocumentationNode.Kind.PropertyAccessor, |
| 209 | DocumentationNode.Kind.ClassObjectProperty, |
| 210 | DocumentationNode.Kind.ClassObjectFunction |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 211 | ) |
| 212 | }, node, to) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 213 | appendSection(location, "Extensions", node.extensions, node, to) |
| 214 | appendSection(location, "Inheritors", node.inheritors, node, to) |
| 215 | appendSection(location, "Links", node.links, node, to) |
| 216 | |
| 217 | } |
| 218 | } |
| 219 | |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 220 | abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode) |
| 221 | abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>) |
| 222 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 223 | public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 224 | for (node in nodes) { |
| 225 | appendOutlineHeader(to, node) |
| 226 | if (node.members.any()) { |
| 227 | appendOutlineChildren(to, node.members) |
| 228 | } |
| 229 | } |
| 230 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 231 | } |