blob: 5287e688add2b0ad53721bc7d1fdebe30c8e943c [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 Ryzhenkov455d74a2014-09-19 22:25:27 +030076 val described = nodes.filter { !it.doc.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 Ryzhenkovd6fd0452014-10-03 20:20:02 +040084 appendLine(to, formatText(location,node.doc.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040085 appendLine(to)
Ilya Ryzhenkovf7bab782014-09-25 22:20:58 +040086 for ((label, section) in node.doc.sections) {
87 if (label.startsWith("$"))
88 continue
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040089 appendLine(to, formatStrong(formatText(label)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040090 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040091 }
92 }
93 }
94 }
95
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040096 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040097 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
98 node.doc.summary
99 }
100
101 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400102 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400103 appendBlockCode(to, formatText(location, languageService.render(it)))
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400104 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400105 appendLine(to, formatText(location, summary))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300106 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400107 }
108 }
109
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400110 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400111 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400112 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400113 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400114 appendSummary(location, to, items)
115 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400116 }
117 }
118
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400119 private fun StructuredFormatService.appendSection(location : Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400120 if (nodes.any()) {
121 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400122
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400123 val children = nodes.sortBy { it.name }
124 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400125
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400126 appendTable(to) {
127 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400128 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400129 appendTableRow(to) {
130 appendTableCell(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400131 appendText(to, formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400132 }
133 appendTableCell(to) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400134 val breakdownBySummary = members.groupBy { formatText(location, it.doc.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400135 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400136 for (signature in items) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400137 appendBlockCode(to, formatText(location, languageService.render(signature)))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400138 }
139
140 if (!summary.isEmpty()) {
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +0400141 appendText(to, summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400142 }
143 }
144 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400145 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400146 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400147 }
148 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400149 }
150 }
151
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400152 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
153 val breakdownByLocation = nodes.groupBy { node ->
154 formatBreadcrumbs(node.path.map { link(node, it) })
155 }
156
157 for ((breadcrumbs, items) in breakdownByLocation) {
158 appendLine(to, breadcrumbs)
159 appendLine(to)
160 appendLocation(location, to, items)
161 }
162
163 for (node in nodes) {
164 appendSection(location, "Members", node.members, node, to)
165 appendSection(location, "Extensions", node.extensions, node, to)
166 appendSection(location, "Inheritors", node.inheritors, node, to)
167 appendSection(location, "Links", node.links, node, to)
168
169 }
170 }
171
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400172 abstract public fun appendOutlineHeader(to: StringBuilder, node: DocumentationNode)
173 abstract public fun appendOutlineChildren(to: StringBuilder, nodes: Iterable<DocumentationNode>)
174
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400175 public override fun appendOutline(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +0400176 for (node in nodes) {
177 appendOutlineHeader(to, node)
178 if (node.members.any()) {
179 appendOutlineChildren(to, node.members)
180 }
181 }
182 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400183}