blob: 280aa6b5165a08f012807cd77c0dbc0511904923 [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
33 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
34
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040035 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
36 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040037 }
38
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040039 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030040 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040041 when (content) {
42 is ContentText -> append(content.text)
43 is ContentSymbol -> append(formatSymbol(content.text))
44 is ContentKeyword -> append(formatKeyword(content.text))
45 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040046 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
47 is ContentCode -> append(formatCode(formatText(location, content.children)))
48 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040049 is ContentNodeLink -> {
50 val linkTo = locationService.relativeLocation(location, content.node, extension)
51 val linkText = formatText(location, content.children)
52 append(formatLink(linkText, linkTo))
53 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040054 is ContentExternalLink -> {
55 val linkText = formatText(location, content.children)
56 append(formatLink(linkText, content.href))
57 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040058 is ContentParagraph -> {
59 appendText(this, formatText(location, content.children))
60 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040061 is ContentBlockCode -> {
62 appendBlockCode(this, formatText(location, content.children))
63 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040064 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030065 }
66 }.toString()
67 }
68
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040069 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
70
71 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
72 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
73 }
74
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040075 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040076 val described = nodes.filter { !it.content.isEmpty }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040077 if (described.any()) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040078 val single = described.size == 1
79 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040080 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040081 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040082 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040083 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040084 appendLine(to, formatText(location,node.content.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040085 appendLine(to)
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040086 for ((label, section) in node.content.sections) {
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040087 if (label.startsWith("$"))
88 continue
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +040089 if (node.members.any { it.name == label })
90 continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040091 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040092 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040093 }
94 }
95 }
96 }
97
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040098 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040099 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400100 formatText(location, node.summary)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400101 }
102
103 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400104 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400105 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400106 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400107 appendLine(to, summary)
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300108 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400109 }
110 }
111
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400112 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400113 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400114 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400115 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400116 appendSummary(location, to, items)
117 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400118 }
119 }
120
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400121 private fun StructuredFormatService.appendSection(location : Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400122 if (nodes.any()) {
123 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400124
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400125 val children = nodes.sortBy { it.name }
126 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400127
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400128 appendTable(to) {
129 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400130 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400131 appendTableRow(to) {
132 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400133 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400134 }
135 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400136 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400137 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400138 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400139 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400140 }
141
142 if (!summary.isEmpty()) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400143 appendText(to, summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400144 }
145 }
146 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400147 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400148 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400149 }
150 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400151 }
152 }
153
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400154 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
155 val breakdownByLocation = nodes.groupBy { node ->
156 formatBreadcrumbs(node.path.map { link(node, it) })
157 }
158
159 for ((breadcrumbs, items) in breakdownByLocation) {
160 appendLine(to, breadcrumbs)
161 appendLine(to)
162 appendLocation(location, to, items)
163 }
164
165 for (node in nodes) {
166 appendSection(location, "Members", node.members, node, to)
167 appendSection(location, "Extensions", node.extensions, node, to)
168 appendSection(location, "Inheritors", node.inheritors, node, to)
169 appendSection(location, "Links", node.links, node, to)
170
171 }
172 }
173
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400174 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
175 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
176
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400177 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400178 for (node in nodes) {
179 appendOutlineHeader(to, node)
180 if (node.members.any()) {
181 appendOutlineChildren(to, node.members)
182 }
183 }
184 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400185}