package org.jetbrains.dokka.renderers import org.jetbrains.dokka.utilities.htmlEscape import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.resolvers.LocationProvider import java.io.File import java.net.URL open class HtmlRenderer( fileWriter: FileWriter, locationProvider: LocationProvider, context: DokkaContext ) : DefaultRenderer(fileWriter, locationProvider, context) { override fun buildList(node: ContentList, pageContext: PageNode): String = if (node.ordered) { "
    ${buildListItems(node.children, pageContext)}
" } else { "" } protected open fun buildListItems(items: List, pageContext: PageNode) = items.joinToString("") { if (it is ContentList) buildList( it, pageContext ) else "
  • \n${it.build(pageContext)}\n
  • \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() """${node.altText}""" } 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 | | |\n\n\n | | |
    ${node.children.joinToString("
    \n") { it.buildTableRow(pageContext) }}
    """.trimMargin() } protected open fun ContentGroup.buildTableRow(pageContext: PageNode) = children.joinToString("\n\n") { it.build(pageContext) } override fun buildHeader(level: Int, text: String): String = "$text\n" override fun buildNewLine(): String = "
    " override fun buildLink(text: String, address: String): String = "$text" override fun buildCode(code: List, language: String, pageContext: PageNode): String = buildNewLine() + "${code.joinToString("") { (it as ContentText).text + buildNewLine() }}" 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 buildScripts(page: PageNode) = page.embeddedResources.filter { URL(it).path.substringAfterLast('.') == "js" } .joinToString(separator = "") { """""" + "\n" } protected open fun buildStartHtml(page: PageNode) = """ | | |${page.name} | |${buildScripts(page)} | | |""".trimMargin() protected open fun buildEndHtml() = """ | | | """.trimMargin() protected open fun List.joinAttr() = this.joinToString(" ") { it.key + "=" + it.value } }