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/HtmlRenderer.kt | |
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/HtmlRenderer.kt')
-rw-r--r-- | core/src/main/kotlin/renderers/HtmlRenderer.kt | 71 |
1 files changed, 47 insertions, 24 deletions
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 |