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
|
package org.jetbrains.dokka.base.renderers.html
import kotlinx.html.*
import kotlinx.html.stream.createHTML
import org.jetbrains.dokka.base.renderers.DefaultRenderer
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.Function
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import java.io.File
open class HtmlRenderer(
context: DokkaContext
) : DefaultRenderer<FlowContent>(context) {
private val pageList = mutableListOf<String>()
override val preprocessors = listOf(
RootCreator,
SearchPageInstaller,
ResourceInstaller,
NavigationPageInstaller,
StyleAndScriptsAppender
)
override fun FlowContent.wrapGroup(
node: ContentGroup,
pageContext: ContentPage,
childrenCallback: FlowContent.() -> Unit
) = when {
node.style.contains(TextStyle.Paragraph) -> p { childrenCallback() }
node.style.contains(TextStyle.Block) -> div { childrenCallback() }
else -> childrenCallback()
}
override fun FlowContent.buildPlatformDependent(content: PlatformHintedContent, pageContext: ContentPage) {
val distinct = content.platforms.map {
it to createHTML(prettyPrint = false).div {
buildContentNode(content.inner, pageContext, it)
}.drop(5).dropLast(6) // TODO: Find a way to do it without arbitrary trims
}.groupBy(Pair<PlatformData, String>::second, Pair<PlatformData, String>::first)
if (distinct.size == 1)
consumer.onTagContentUnsafe { +distinct.keys.single() }
else
distinct.forEach { text, platforms ->
consumer.onTagContentUnsafe { +platforms.joinToString(prefix = "$text [", postfix = "]") { it.name } }
}
}
override fun FlowContent.buildList(
node: ContentList,
pageContext: ContentPage,
platformRestriction: PlatformData?
) = if (node.ordered) ol { buildListItems(node.children, pageContext, platformRestriction) }
else ul { buildListItems(node.children, pageContext, platformRestriction) }
open fun OL.buildListItems(
items: List<ContentNode>,
pageContext: ContentPage,
platformRestriction: PlatformData? = null
) {
items.forEach {
if (it is ContentList)
buildList(it, pageContext)
else
li { it.build(this, pageContext, platformRestriction) }
}
}
open fun UL.buildListItems(
items: List<ContentNode>,
pageContext: ContentPage,
platformRestriction: PlatformData? = null
) {
items.forEach {
if (it is ContentList)
buildList(it, pageContext)
else
li { it.build(this, pageContext) }
}
}
override fun FlowContent.buildResource(
node: ContentEmbeddedResource,
pageContext: ContentPage
) { // TODO: extension point there
val imageExtensions = setOf("png", "jpg", "jpeg", "gif", "bmp", "tif", "webp", "svg")
return if (File(node.address).extension.toLowerCase() in imageExtensions) {
//TODO: add imgAttrs parsing
val imgAttrs = node.extra.allOfType<SimpleAttr>().joinAttr()
img(src = node.address, alt = node.altText)
} else {
println("Unrecognized resource type: $node")
}
}
override fun FlowContent.buildTable(
node: ContentTable,
pageContext: ContentPage,
platformRestriction: PlatformData?
) {
table {
thead {
node.header.forEach {
tr {
it.children.forEach {
th {
it.build(this@table, pageContext, platformRestriction)
}
}
}
}
}
tbody {
node.children.forEach {
tr {
it.children.forEach {
td {
it.build(this, pageContext, platformRestriction)
}
}
}
}
}
}
}
override fun FlowContent.buildHeader(level: Int, content: FlowContent.() -> Unit) {
when (level) {
1 -> h1(block = content)
2 -> h2(block = content)
3 -> h3(block = content)
4 -> h4(block = content)
5 -> h5(block = content)
else -> h6(block = content)
}
}
override fun FlowContent.buildNavigation(page: PageNode) =
locationProvider.ancestors(page).asReversed().forEach { node ->
text("/")
if (node.isNavigable) buildLink(node, page)
else text(node.name)
}
private fun FlowContent.buildLink(to: PageNode, from: PageNode) =
buildLink(locationProvider.resolve(to, from)) {
text(to.name)
}
fun FlowContent.buildLink(
to: DRI,
platforms: List<PlatformData>,
from: PageNode? = null,
block: FlowContent.() -> Unit
) = buildLink(locationProvider.resolve(to, platforms, from), block)
override fun buildError(node: ContentNode) {
context.logger.error("Unknown ContentNode type: $node")
}
override fun FlowContent.buildNewLine() {
br()
}
override fun FlowContent.buildLink(address: String, content: FlowContent.() -> Unit) =
a(href = address, block = content)
override fun FlowContent.buildCode(
code: List<ContentNode>,
language: String,
pageContext: ContentPage
) {
span(classes = "code") {
val iterator = code.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
+((element as? ContentText)?.text
?: run { context.logger.error("Cannot cast $element as ContentText!"); "" })
if (iterator.hasNext()) {
buildNewLine()
}
}
}
}
override fun renderPage(page: PageNode) {
super.renderPage(page)
if (page is ContentPage) {
pageList.add(
"""{ "name": "${page.name}", ${if (page is ClasslikePageNode) "\"class\": \"${page.name}\"," else ""} "location": "${locationProvider.resolve(
page
)}" }"""
)
}
}
override fun FlowContent.buildText(textNode: ContentText) {
text(textNode.text)
}
override fun render(root: RootPageNode) {
super.render(root)
outputWriter.write("scripts/pages", "var pages = [\n${pageList.joinToString(",\n")}\n]", ".js")
}
private fun PageNode.root(path: String) = locationProvider.resolveRoot(this) + path
override fun buildPage(page: ContentPage, content: (FlowContent, ContentPage) -> Unit): String =
buildHtml(page, page.embeddedResources) {
attributes["pageIds"] = page.dri.toList()[0].toString()
content(this, page)
}
open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit) =
createHTML().html {
head {
title(page.name)
with(resources) {
filter { it.substringBefore('?').substringAfterLast('.') == "css" }
.forEach { link(rel = LinkRel.stylesheet, href = page.root(it)) }
filter { it.substringBefore('?').substringAfterLast('.') == "js" }
.forEach { script(type = ScriptType.textJavaScript, src = page.root(it)) { async = true } }
}
script { unsafe { +"""var pathToRoot = "${locationProvider.resolveRoot(page)}";""" } }
}
body {
div {
id = "navigation"
div {
id = "searchBar"
form(action = page.root("-search.html"), method = FormMethod.get) {
id = "searchForm"
input(type = InputType.search, name = "query")
input(type = InputType.submit) { value = "Search" }
}
}
div {
id = "sideMenu"
}
}
div {
id = "content"
content()
}
}
}
}
fun List<SimpleAttr>.joinAttr() = joinToString(" ") { it.extraKey + "=" + it.extraValue }
private fun PageNode.pageKind() = when (this) {
is PackagePageNode -> "package"
is ClasslikePageNode -> "class"
is MemberPageNode -> when (this.documentable) {
is Function -> "function"
else -> "other"
}
else -> "other"
}
private val PageNode.isNavigable: Boolean
get() = this !is RendererSpecificPage || strategy != RenderingStrategy.DoNothing
|