package org.jetbrains.dokka.renderers
import org.jetbrains.dokka.htmlEscape
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.resolvers.LocationProvider
import java.io.File
open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvider) : DefaultRenderer(fileWriter, locationProvider) {
override fun buildList(node: ContentList, pageContext: PageNode): String = if (node.ordered) {
"
${buildListItems(node.children, pageContext)}
"
} else {
"${buildListItems(node.children, pageContext)}
"
}
protected open fun buildListItems(items: List, pageContext: PageNode) =
"\n${items.joinToString("\n\n\n") { it.build(pageContext) }}\n"
override fun buildResource(node: ContentEmbeddedResource, pageContext: PageNode): String { // TODO: extension point there
val imageExtensions = setOf("png", "jpg", "jpeg", "gif", "bmp", "tif", "webp", "svg")
return if (File(node.address).extension.toLowerCase() in imageExtensions) {
val imgAttrs = node.extras.filterIsInstance().joinAttr()
""""""
} else {
println("Unrecognized resource type: $node")
""
}
}
override fun buildTable(node: ContentTable, pageContext: PageNode): String {
val tableHeader =
"""
|
| ${node.header.joinToString(" | \n |
\n\n") { it.build(pageContext) }} |
|
|""".trimMargin()
return """```
|$tableHeader
|
|
|${node.children.joinToString(" | \n |
\n\n") { it.build(pageContext) }} |
|
|
|
""".trimMargin()
}
override fun buildHeader(level: Int, text: String): String = "$text\n"
override fun buildNewLine(): String = "
"
override fun buildLink(text: String, address: String): String = "${text.htmlEscape()}"
override fun buildCode(code: List, language: String, pageContext: PageNode): String = "$code
"
override fun buildText(textNode: ContentText): String = super.buildText(textNode).htmlEscape()
override fun renderPage(page: PageNode) {
val pageText = buildStartHtml(page) + buildPageContent(page) + buildEndHtml()
fileWriter.write(locationProvider.resolve(page), pageText, "")
}
override fun buildSupportFiles() { // TODO copy file instead of reading
fileWriter.write(
"style.css",
javaClass.getResourceAsStream("/dokka/styles/style.css").reader().readText()
)
}
protected open fun buildStartHtml(page: PageNode) = """
|
|
|${page.name}
|
|
|
|""".trimMargin()
protected open fun buildEndHtml() =
"""
|
|
|
""".trimMargin()
protected open fun List.joinAttr() = this.joinToString(" ") { it.key + "=" + it.value }
}