From 1be2870b600cdbbe6aebb9f64c0226b26e42ca5a Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Wed, 13 Nov 2019 15:56:57 +0100 Subject: Fix DefaultRenderer and HtmlRenderer --- core/src/main/kotlin/renderers/DefaultRenderer.kt | 68 ++++++++++++++-------- core/src/main/kotlin/renderers/HtmlRenderer.kt | 71 +++++++++++++++-------- 2 files changed, 92 insertions(+), 47 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/renderers/DefaultRenderer.kt b/core/src/main/kotlin/renderers/DefaultRenderer.kt index e7c8d074..1acaaf4f 100644 --- a/core/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/core/src/main/kotlin/renderers/DefaultRenderer.kt @@ -3,39 +3,61 @@ package org.jetbrains.dokka.renderers import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.resolvers.LocationProvider -abstract class DefaultRenderer(val fileWriter: FileWriter, val locationProvider: LocationProvider): Renderer { +abstract class DefaultRenderer(val fileWriter: FileWriter, val locationProvider: LocationProvider) : Renderer { protected abstract fun buildHeader(level: Int, text: String): String - protected abstract fun buildNewLine(): String protected abstract fun buildLink(text: String, address: String): String - protected abstract fun buildCode(code: String): String - protected abstract fun buildNavigation(page: PageNode): String - protected open fun buildText(text: String): String = text - protected open fun buildHeader(level: Int, content: List, pageContext: PageNode): String = buildHeader(level, content.joinToString { it.build(pageContext) }) - protected open fun buildGroup(children: List, pageContext: PageNode): String = children.joinToString { it.build(pageContext) } - protected open fun buildComment(parts: List, pageContext: PageNode): String = parts.joinToString { it.build(pageContext) } - protected open fun buildSymbol(parts: List, pageContext: PageNode): String = parts.joinToString(separator = "") { it.build(pageContext) } - protected open fun buildBlock(name: String, content: List, pageContext: PageNode) = buildHeader(3, name) + content.joinToString("\n") { it.build(pageContext) } + protected abstract fun buildList(node: ContentList, pageContext: PageNode): String + protected abstract fun buildNewLine(): String + protected abstract fun buildResource(node: ContentEmbeddedResource, pageContext: PageNode): String + protected abstract fun buildTable(node: ContentTable, pageContext: PageNode): String + + protected open fun buildText(textNode: ContentText): String = textNode.text + + protected open fun buildGroup(node: ContentGroup, pageContext: PageNode): String = node.children.joinToString("") { it.build(pageContext) } + + protected open fun buildLinkText(nodes: List, pageContext: PageNode): String = + nodes.joinToString(" ") { it.build(pageContext) } + + protected open fun buildCode(code: List, language: String, pageContext: PageNode): String = + code.joinToString { it.build(pageContext) } + + protected open fun buildHeader(node: ContentHeader, pageContext: PageNode): String = + buildHeader(node.level, node.children.joinToString { it.build(pageContext) }) + + protected open fun buildNavigation(page: PageNode): String { + fun buildNavigationWithContext(page: PageNode, context: PageNode): String = + page.parent?.let { buildNavigationWithContext(it, context) }.orEmpty() + "/" + buildLink( + page.name, + locationProvider.resolve(page, context) + ) + return buildNavigationWithContext(page, page) + } protected open fun ContentNode.build(pageContext: PageNode): String = buildContentNode(this, pageContext) protected open fun buildContentNode(node: ContentNode, pageContext: PageNode): String = - when(node) { - is ContentText -> buildText(node.text) - is ContentComment -> buildComment(node.parts, pageContext) - is ContentSymbol -> buildSymbol(node.parts, pageContext) - is ContentCode -> buildCode(node.code) - is ContentBlock -> buildBlock(node.name, node.children, pageContext) - is ContentLink -> buildLink(node.text, locationProvider.resolve(node.address, node.dci.platformDataList, pageContext)) - is ContentGroup -> buildGroup(node.children, pageContext) - is ContentHeader -> buildHeader(node.level, node.items, pageContext) - is ContentStyle -> node.items.joinToString(separator = "\n") { buildContentNode(it, pageContext) } - else -> "" + when (node) { + is ContentText -> buildText(node) + is ContentHeader -> buildHeader(node, pageContext) + is ContentCode -> buildCode(node.children, node.language, pageContext) + is ContentDRILink -> buildLink( + buildLinkText(node.children, pageContext), + locationProvider.resolve(node.address, node.platforms.toList(), pageContext) + ) + is ContentResolvedLink -> buildLink(buildLinkText(node.children, pageContext), node.address) + is ContentEmbeddedResource -> buildResource(node, pageContext) + is ContentList -> buildList(node, pageContext) + is ContentTable -> buildTable(node, pageContext) + is ContentGroup -> buildGroup(node, pageContext) + else -> "".also { println("Unrecognized ContentNode: $node") } } - protected open fun buildPageContent(page: PageNode): String = buildNavigation(page) + page.content.joinToString("\n") { it.build(page) } + protected open fun buildPageContent(page: PageNode): String = + buildNavigation(page) + page.content.joinToString("\n") { it.build(page) } - protected open fun renderPage(page: PageNode) = fileWriter.write(locationProvider.resolve(page), buildPageContent(page), "") + protected open fun renderPage(page: PageNode) = + fileWriter.write(locationProvider.resolve(page), buildPageContent(page), "") protected open fun renderPages(root: PageNode) { renderPage(root) diff --git a/core/src/main/kotlin/renderers/HtmlRenderer.kt b/core/src/main/kotlin/renderers/HtmlRenderer.kt index e0b5b7dc..adc3dd5a 100644 --- a/core/src/main/kotlin/renderers/HtmlRenderer.kt +++ b/core/src/main/kotlin/renderers/HtmlRenderer.kt @@ -1,18 +1,49 @@ package org.jetbrains.dokka.renderers import org.jetbrains.dokka.htmlEscape -import org.jetbrains.dokka.pages.ContentLink -import org.jetbrains.dokka.pages.ContentNode -import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.resolvers.LocationProvider import java.io.File -import java.io.InputStreamReader -open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvider): DefaultRenderer(fileWriter, locationProvider) { +open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvider) : DefaultRenderer(fileWriter, locationProvider) { - override fun buildComment(parts: List, pageContext: PageNode): String = "

" + parts.joinToString("
") { it.build(pageContext) } + "

" + 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() + """${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() - override fun buildSymbol(parts: List, pageContext: PageNode): String = "${super.buildSymbol(parts, pageContext)}" + return """``` + |$tableHeader + | + | + |\n\n + | + | + |
    ${node.children.joinToString("\n
    ") { it.build(pageContext) }}
    """.trimMargin() + } override fun buildHeader(level: Int, text: String): String = "$text\n" @@ -20,30 +51,20 @@ open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvid override fun buildLink(text: String, address: String): String = "${text.htmlEscape()}" - override fun buildCode(code: String): String = "$code" - - override fun buildText(text: String) = super.buildText(text).htmlEscape() - - override fun buildNavigation(page: PageNode): String { - fun buildNavigationWithContext(page: PageNode, context: PageNode): String = - page.parent?.let { buildNavigationWithContext(it, context) }.orEmpty() + "/" + buildLink(page.name, locationProvider.resolve(page, context)) - return buildNavigationWithContext(page, page) - } + override fun buildCode(code: List, language: String, pageContext: PageNode): String = "$code" - override fun buildGroup(children: List, pageContext: PageNode): String = children.joinToString("\n\n") { it.build(pageContext) } - - override fun buildBlock(name: String, content: List, pageContext: PageNode): String = - buildHeader(3, name) + "\n\n\n\n\n\n
    \n" + - content.joinToString("
    ") { "(" + it.dci.platformDataList.map { it.platformName }.joinToString() + ") " + it.build(pageContext) } + "
    " + 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() { - fileWriter.write("style.css", - InputStreamReader(javaClass.getResourceAsStream("/dokka/styles/style.css")).readText()) + 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) = """ @@ -61,4 +82,6 @@ open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvid | | """.trimMargin() + + protected open fun List.joinAttr() = this.joinToString(" ") { it.key + "=" + it.value } } \ No newline at end of file -- cgit