1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
package org.jetbrains.dokka.gfm
import org.jetbrains.dokka.CoreExtensions
import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.renderers.DefaultRenderer
import org.jetbrains.dokka.base.renderers.PackageListCreator
import org.jetbrains.dokka.base.renderers.RootCreator
import org.jetbrains.dokka.base.resolvers.local.DefaultLocationProvider
import org.jetbrains.dokka.base.resolvers.local.LocationProviderFactory
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query
import org.jetbrains.dokka.transformers.pages.PageTransformer
class GfmPlugin : DokkaPlugin() {
val gfmPreprocessors by extensionPoint<PageTransformer>()
val renderer by extending {
CoreExtensions.renderer providing { CommonmarkRenderer(it) } applyIf { format == "gfm" }
}
val locationProvider by extending {
plugin<DokkaBase>().locationProviderFactory providing { MarkdownLocationProviderFactory(it) } applyIf { format == "gfm" }
}
val rootCreator by extending {
gfmPreprocessors with RootCreator
}
val packageListCreator by extending {
gfmPreprocessors providing {
PackageListCreator(
it,
"gfm",
"md"
)
} order { after(rootCreator) }
}
}
open class CommonmarkRenderer(
context: DokkaContext
) : DefaultRenderer<StringBuilder>(context) {
override val preprocessors = context.plugin<GfmPlugin>().query { gfmPreprocessors }
override fun StringBuilder.wrapGroup(
node: ContentGroup,
pageContext: ContentPage,
childrenCallback: StringBuilder.() -> Unit
) {
return when {
node.hasStyle(TextStyle.Block) -> {
childrenCallback()
buildNewLine()
}
else -> childrenCallback()
}
}
override fun StringBuilder.buildHeader(level: Int, node: ContentHeader, content: StringBuilder.() -> Unit) {
buildParagraph()
append("#".repeat(level) + " ")
content()
buildNewLine()
}
override fun StringBuilder.buildLink(address: String, content: StringBuilder.() -> Unit) {
append("""<a href="$address">""")
content()
append("</a>")
}
override fun StringBuilder.buildList(
node: ContentList,
pageContext: ContentPage,
sourceSetRestriction: Set<DokkaSourceSet>?
) {
buildListLevel(node, pageContext)
}
private fun StringBuilder.buildListItem(items: List<ContentNode>, pageContext: ContentPage) {
items.forEach {
if (it is ContentList) {
buildList(it, pageContext)
} else {
append("<li>")
it.build(this, pageContext)
append("</li>")
}
}
}
private fun StringBuilder.buildListLevel(node: ContentList, pageContext: ContentPage) {
if (node.ordered) {
append("<ol>")
buildListItem(node.children, pageContext)
append("</ol>")
} else {
append("<ul>")
buildListItem(node.children, pageContext)
append("</ul>")
}
}
override fun StringBuilder.buildNewLine() {
append(" \n")
}
private fun StringBuilder.buildParagraph() {
append("\n\n")
}
override fun StringBuilder.buildPlatformDependent(
content: PlatformHintedContent,
pageContext: ContentPage,
sourceSetRestriction: Set<DokkaSourceSet>?
) {
buildPlatformDependentItem(content.inner, content.sourceSets, pageContext)
}
private fun StringBuilder.buildPlatformDependentItem(
content: ContentNode,
sourceSets: Set<DokkaSourceSet>,
pageContext: ContentPage,
) {
if(content is ContentGroup && content.children.firstOrNull{ it is ContentTable } != null){
buildContentNode(content, pageContext)
} else {
val distinct = sourceSets.map {
it to StringBuilder().apply { buildContentNode(content, pageContext, setOf(it)) }.toString()
}.groupBy(Pair<DokkaSourceSet, String>::second, Pair<DokkaSourceSet, String>::first)
distinct.filter { it.key.isNotBlank() }.forEach { (text, platforms) ->
append(
platforms.joinToString(
prefix = " [",
postfix = "] $text "
) { "${it.moduleName}/${it.sourceSetID}" })
buildNewLine()
}
}
}
override fun StringBuilder.buildResource(node: ContentEmbeddedResource, pageContext: ContentPage) {
append("Resource")
}
override fun StringBuilder.buildTable(
node: ContentTable,
pageContext: ContentPage,
sourceSetRestriction: Set<DokkaSourceSet>?
) {
if(node.dci.kind == ContentKind.Sample || node.dci.kind == ContentKind.Parameters){
node.sourceSets.forEach {sourcesetData ->
append("${sourcesetData.moduleName}/${sourcesetData.sourceSetID}")
buildNewLine()
buildTable(node.copy(children = node.children.filter { it.sourceSets.contains(sourcesetData) }, dci = node.dci.copy(kind = ContentKind.Main)), pageContext, sourceSetRestriction)
buildNewLine()
}
} else {
val size = node.header.size
if (node.header.isNotEmpty()) {
append("| ")
node.header.forEach {
it.children.forEach {
append(" ")
it.build(this, pageContext)
}
append("| ")
}
append("\n")
} else {
append("| ".repeat(size))
if (size > 0) append("|\n")
}
append("|---".repeat(size))
if (size > 0) append("|\n")
node.children.forEach {
val builder = StringBuilder()
it.children.forEach {
builder.append("| ")
it.build(builder, pageContext)
}
append(builder.toString().withEntersAsHtml())
append(" | ".repeat(size - it.children.size))
append("\n")
}
}
}
override fun StringBuilder.buildText(textNode: ContentText) {
val decorators = decorators(textNode.style)
append(decorators)
append(textNode.text)
append(decorators.reversed())
}
override fun StringBuilder.buildNavigation(page: PageNode) {
locationProvider.ancestors(page).asReversed().forEach { node ->
append("/")
if (node.isNavigable) buildLink(node, page)
else append(node.name)
}
buildParagraph()
}
override fun buildPage(page: ContentPage, content: (StringBuilder, ContentPage) -> Unit): String =
StringBuilder().apply {
content(this, page)
}.toString()
override fun buildError(node: ContentNode) {
context.logger.warn("Markdown renderer has encountered problem. The unmatched node is $node")
}
override fun StringBuilder.buildDivergentInstance(node: ContentDivergentInstance, pageContext: ContentPage) {
node.before?.let {
buildPlatformDependentItem(it, it.sourceSets, pageContext)
buildNewLine()
}
node.divergent.build(this, pageContext)
buildNewLine()
node.after?.let {
buildPlatformDependentItem(it, it.sourceSets, pageContext)
buildNewLine()
}
}
private fun decorators(styles: Set<Style>) = StringBuilder().apply {
styles.forEach {
when (it) {
TextStyle.Bold -> append("**")
TextStyle.Italic -> append("*")
TextStyle.Strong -> append("**")
TextStyle.Strikethrough -> append("~~")
else -> Unit
}
}
}.toString()
private val PageNode.isNavigable: Boolean
get() = this !is RendererSpecificPage || strategy != RenderingStrategy.DoNothing
private fun StringBuilder.buildLink(to: PageNode, from: PageNode) =
buildLink(locationProvider.resolve(to, from)) {
append(to.name)
}
override suspend fun renderPage(page: PageNode) {
val path by lazy { locationProvider.resolve(page, skipExtension = true) }
when (page) {
is ContentPage -> outputWriter.write(path, buildPage(page) { c, p -> buildPageContent(c, p) }, ".md")
is RendererSpecificPage -> when (val strategy = page.strategy) {
is RenderingStrategy.Copy -> outputWriter.writeResources(strategy.from, path)
is RenderingStrategy.Write -> outputWriter.write(path, strategy.text, "")
is RenderingStrategy.Callback -> outputWriter.write(path, strategy.instructions(this, page), ".md")
RenderingStrategy.DoNothing -> Unit
}
else -> throw AssertionError(
"Page ${page.name} cannot be rendered by renderer as it is not renderer specific nor contains content"
)
}
}
private fun String.withEntersAsHtml(): String = replace("\n", "<br>")
}
class MarkdownLocationProviderFactory(val context: DokkaContext) : LocationProviderFactory {
override fun getLocationProvider(pageNode: RootPageNode) = MarkdownLocationProvider(pageNode, context)
}
class MarkdownLocationProvider(
pageGraphRoot: RootPageNode,
dokkaContext: DokkaContext
) : DefaultLocationProvider(
pageGraphRoot,
dokkaContext
) {
override val extension = ".md"
}
|