blob: 75b51ab8b2da94cafd39f818ce6bb716bddc7fe6 [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 Ryzhenkovd6fd0452014-10-03 20:20:02 +040058 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030059 }
60 }.toString()
61 }
62
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040063 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
64
65 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
66 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
67 }
68
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040069 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030070 val described = nodes.filter { !it.doc.isEmpty }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040071 if (described.any()) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040072 val single = described.size == 1
73 appendHeader(to, "Description", 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040074 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040075 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040076 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040077 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040078 appendLine(to, formatText(location,node.doc.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040079 appendLine(to)
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040080 for ((label, section) in node.doc.sections) {
81 if (label.startsWith("$"))
82 continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040083 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040084 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040085 appendLine(to)
86 }
87 }
88 }
89 }
90
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040091 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040092 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
93 node.doc.summary
94 }
95
96 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040097 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040098 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040099 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400100 appendLine(to, formatText(location, summary))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300101 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400102 }
103 }
104
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400105 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400106 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400107 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400108 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400109 appendSummary(location, to, items)
110 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400111 }
112 }
113
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400114 private fun StructuredFormatService.appendSection(location : Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400115 if (nodes.any()) {
116 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400117
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400118 val children = nodes.sortBy { it.name }
119 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400120
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400121 appendTable(to) {
122 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400123 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400124 appendTableRow(to) {
125 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400126 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400127 }
128 appendTableCell(to) {
129 val breakdownBySummary = members.groupBy { it.doc.summary }
130 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400131 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400132 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400133 }
134
135 if (!summary.isEmpty()) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400136 appendText(to, formatText(location, summary))
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400137 }
138 }
139 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400140 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400141 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400142 }
143 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400144 }
145 }
146
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400147 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
148 val breakdownByLocation = nodes.groupBy { node ->
149 formatBreadcrumbs(node.path.map { link(node, it) })
150 }
151
152 for ((breadcrumbs, items) in breakdownByLocation) {
153 appendLine(to, breadcrumbs)
154 appendLine(to)
155 appendLocation(location, to, items)
156 }
157
158 for (node in nodes) {
159 appendSection(location, "Members", node.members, node, to)
160 appendSection(location, "Extensions", node.extensions, node, to)
161 appendSection(location, "Inheritors", node.inheritors, node, to)
162 appendSection(location, "Links", node.links, node, to)
163
164 }
165 }
166
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400167 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
168 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
169
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400170 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400171 for (node in nodes) {
172 appendOutlineHeader(to, node)
173 if (node.members.any()) {
174 appendOutlineChildren(to, node.members)
175 }
176 }
177 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400178}