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
|
package org.jetbrains.dokka.renderers
import kotlinx.html.*
import kotlinx.html.stream.createHTML
import org.jetbrains.dokka.model.Function
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import java.io.File
open class HtmlRenderer(
outputWriter: OutputWriter,
context: DokkaContext
) : DefaultRenderer<FlowContent>(outputWriter, context) {
private val pageList = mutableListOf<String>()
private var idCounter = 0
get() = ++field
private fun FlowContent.buildSideMenu(context: PageNode, node: PageNode) {
val children = node.children.filter { it !is MemberPageNode }
val submenuId = if (children.isNotEmpty()) "nav$idCounter" else null
div("sideMenuPart") {
submenuId?.also { id = it }
div("overview") {
buildLink(node, context)
submenuId?.also {
span("navButton") {
onClick = """document.getElementById("$it").classList.toggle("hidden");"""
span("navButtonContent")
}
}
}
children.forEach { buildSideMenu(context, it) }
}
}
override val preprocessors = listOf(RootCreator, SearchPageInstaller, ResourceInstaller, StyleAndScriptsAppender)
override fun FlowContent.buildList(node: ContentList, pageContext: ContentPage) =
if (node.ordered) ol {
buildListItems(node.children, pageContext)
}
else ul {
buildListItems(node.children, pageContext)
}
protected open fun OL.buildListItems(items: List<ContentNode>, pageContext: ContentPage) {
items.forEach {
if (it is ContentList)
buildList(it, pageContext)
else
li { it.build(this, pageContext) }
}
}
protected open fun UL.buildListItems(items: List<ContentNode>, pageContext: ContentPage) {
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.extras.filterIsInstance<HTMLSimpleAttr>().joinAttr()
img(src = node.address, alt = node.altText)
} else {
println("Unrecognized resource type: $node")
}
}
override fun FlowContent.buildTable(node: ContentTable, pageContext: ContentPage) {
table {
thead {
node.header.forEach {
tr {
it.children.forEach {
th {
it.build(this@table, pageContext)
}
}
}
}
}
tbody {
node.children.forEach {
tr {
it.children.forEach {
td {
it.build(this, pageContext)
}
}
}
}
}
}
}
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)
else -> h5(block = content)
}
}
override fun FlowContent.buildNavigation(page: PageNode) =
locationProvider.ancestors(page).forEach { node ->
text("/")
buildLink(node, page)
}
private fun FlowContent.buildLink(to: PageNode, from: PageNode) =
buildLink(locationProvider.resolve(to, from)) {
text(to.name)
}
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) {
buildNewLine()
code.forEach {
+((it as? ContentText)?.text ?: run { context.logger.error("Cannot cast $it as ContentText!"); "" })
buildNewLine()
}
}
override fun renderPage(page: PageNode) {
super.renderPage(page)
if (page is ContentPage) {
pageList.add(
"""{ "name": "${page.name}", ${if (page is ClassPageNode) "\"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) { content(this, page) }
open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit) =
createHTML().html {
head {
title(page.name)
with(resources) {
filter { it.substringAfterLast('.') == "css" }
.forEach { link(rel = LinkRel.stylesheet, href = page.root(it)) }
filter { it.substringAfterLast('.') == "js" }
.forEach { script(type = ScriptType.textJavaScript, src = it) { async = true } }
}
}
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<HTMLMetadata>.joinAttr() = joinToString(" ") { it.key + "=" + it.value }
private fun PageNode.pageKind() = when (this) {
is PackagePageNode -> "package"
is ClassPageNode -> "class"
is MemberPageNode -> when (this.documentable) {
is Function -> "function"
else -> "other"
}
else -> "other"
}
|