diff options
author | Kamil Doległo <kamilok1965@interia.pl> | 2019-11-13 15:56:57 +0100 |
---|---|---|
committer | Kamil Doległo <kamilok1965@interia.pl> | 2019-11-13 15:56:57 +0100 |
commit | 1be2870b600cdbbe6aebb9f64c0226b26e42ca5a (patch) | |
tree | c6ac346cf530049e1a377c868f06eb2b7d6ae8b5 /core/src/main/kotlin/renderers | |
parent | 3873d13730d3c07d73ced9b02a0ef51f2cc510b1 (diff) | |
download | dokka-1be2870b600cdbbe6aebb9f64c0226b26e42ca5a.tar.gz dokka-1be2870b600cdbbe6aebb9f64c0226b26e42ca5a.tar.bz2 dokka-1be2870b600cdbbe6aebb9f64c0226b26e42ca5a.zip |
Fix DefaultRenderer and HtmlRenderer
Diffstat (limited to 'core/src/main/kotlin/renderers')
-rw-r--r-- | core/src/main/kotlin/renderers/DefaultRenderer.kt | 68 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/HtmlRenderer.kt | 71 |
2 files changed, 92 insertions, 47 deletions
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<ContentNode>, pageContext: PageNode): String = buildHeader(level, content.joinToString { it.build(pageContext) }) - protected open fun buildGroup(children: List<ContentNode>, pageContext: PageNode): String = children.joinToString { it.build(pageContext) } - protected open fun buildComment(parts: List<ContentNode>, pageContext: PageNode): String = parts.joinToString { it.build(pageContext) } - protected open fun buildSymbol(parts: List<ContentNode>, pageContext: PageNode): String = parts.joinToString(separator = "") { it.build(pageContext) } - protected open fun buildBlock(name: String, content: List<ContentNode>, 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<ContentNode>, pageContext: PageNode): String = + nodes.joinToString(" ") { it.build(pageContext) } + + protected open fun buildCode(code: List<ContentNode>, 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<ContentNode>, pageContext: PageNode): String = "<p>" + parts.joinToString("<br>") { it.build(pageContext) } + "</p>" + override fun buildList(node: ContentList, pageContext: PageNode): String = if (node.ordered) { + "<ol>${buildListItems(node.children, pageContext)}</ol>" + } else { + "<ul>${buildListItems(node.children, pageContext)}</ul>" + } + + protected open fun buildListItems(items: List<ContentNode>, pageContext: PageNode) = + "<li>\n${items.joinToString("\n</li>\n<li>\n") { it.build(pageContext) }}\n</li>" + + 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<HTMLSimpleAttr>().joinAttr() + """<img src="${node.address}" alt="${node.altText}" $imgAttrs>""" + } else { + println("Unrecognized resource type: $node") + "" + } + } + + override fun buildTable(node: ContentTable, pageContext: PageNode): String { + val tableHeader = + """<thead> + |<tr> + |<th> ${node.header.joinToString("<th>\n</tr>\n<tr>\n<th>") { it.build(pageContext) }} </th> + |</tr> + |</thead>""".trimMargin() - override fun buildSymbol(parts: List<ContentNode>, pageContext: PageNode): String = "<code>${super.buildSymbol(parts, pageContext)}</code>" + return """<table>``` + |$tableHeader + |<tbody> + |<tr> + |<td>${node.children.joinToString("<td>\n</tr>\n<tr>\n<td>") { it.build(pageContext) }}</td> + |</tr> + |</tbody> + |</table>""".trimMargin() + } override fun buildHeader(level: Int, text: String): String = "<h$level>$text</h$level>\n" @@ -20,30 +51,20 @@ open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvid override fun buildLink(text: String, address: String): String = "<a href=\"$address\">${text.htmlEscape()}</a>" - override fun buildCode(code: String): String = "<code>$code</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<ContentNode>, language: String, pageContext: PageNode): String = "<code>$code</code>" - override fun buildGroup(children: List<ContentNode>, pageContext: PageNode): String = children.joinToString("</td>\n<td>\n") { it.build(pageContext) } - - override fun buildBlock(name: String, content: List<ContentNode>, pageContext: PageNode): String = - buildHeader(3, name) + "<table>\n<tr>\n<td>\n" + - content.joinToString("</td>\n</tr>\n<tr>\n<td>") { "(" + it.dci.platformDataList.map { it.platformName }.joinToString() + ") " + it.build(pageContext) } + "</td></tr>\n</table>" + 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) = """<!DOCTYPE html> @@ -61,4 +82,6 @@ open class HtmlRenderer(fileWriter: FileWriter, locationProvider: LocationProvid |</body> |</html> """.trimMargin() + + protected open fun List<HTMLMetadata>.joinAttr() = this.joinToString(" ") { it.key + "=" + it.value } }
\ No newline at end of file |