diff options
Diffstat (limited to 'core/src/main/kotlin/renderers')
-rw-r--r-- | core/src/main/kotlin/renderers/DefaultRenderer.kt | 61 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/FileWriter.kt | 32 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/HtmlRenderer.kt | 51 | ||||
-rw-r--r-- | core/src/main/kotlin/renderers/Renderer.kt | 14 |
4 files changed, 158 insertions, 0 deletions
diff --git a/core/src/main/kotlin/renderers/DefaultRenderer.kt b/core/src/main/kotlin/renderers/DefaultRenderer.kt new file mode 100644 index 00000000..b2647d22 --- /dev/null +++ b/core/src/main/kotlin/renderers/DefaultRenderer.kt @@ -0,0 +1,61 @@ +package org.jetbrains.dokka.renderers + +import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.resolvers.LocationProvider + +abstract class DefaultRenderer(val outputDir: String, 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(): String // TODO + protected open fun buildText(text: String): String = text + protected open fun buildGroup(children: List<ContentNode>): String = children.joinToString { it.build() } + protected open fun buildComment(parts: List<ContentNode>): String = parts.joinToString { it.build() } + protected open fun buildSymbol(parts: List<ContentNode>): String = parts.joinToString { it.build() } + protected open fun buildBlock(name: String, content: List<ContentNode>) = buildHeader(2, name) + content.joinToString("\n") { it.build() } + + protected open fun ContentNode.build(): String = + when(this) { + is ContentText -> buildText(this.text) + is ContentComment -> buildComment(this.parts) + is ContentSymbol -> buildSymbol(this.parts) + is ContentCode -> buildCode(this.code) + is ContentBlock -> buildBlock(this.name, this.children) + is ContentLink -> buildLink(this.text, locationProvider.resolve(this.address, this.platforms)) + is ContentGroup -> buildGroup(this.children) + else -> "" + } + + protected open fun buildPageContent(page: PageNode): String = + buildHeader(1, page.name) + page.content.joinToString("\n") { it.build() } + + protected open fun renderPage(page: PageNode) = fileWriter.write(locationProvider.resolve(page), buildPageContent(page)) + + protected open fun renderPages(root: PageNode) { + renderPage(root) + root.children.forEach { renderPages(it) } + } + + protected open fun renderPackageList(root: PageNode) = + getPackageNamesAndPlatforms(root) + .keys + .joinToString("\n") + .also { fileWriter.write("package-list", it, "") } + + protected open fun getPackageNamesAndPlatforms(root: PageNode): Map<String, List<PlatformData>> = + root.children + .map(::getPackageNamesAndPlatforms) + .fold(emptyMap<String, List<PlatformData>>()) { e, acc -> acc + e } + + if (root is PackagePageNode) { + mapOf(root.name to root.platforms()) + } else { + emptyMap() + } + + override fun render(root: PageNode) { + renderPackageList(root) + renderPages(root) + } +} diff --git a/core/src/main/kotlin/renderers/FileWriter.kt b/core/src/main/kotlin/renderers/FileWriter.kt new file mode 100644 index 00000000..319926dd --- /dev/null +++ b/core/src/main/kotlin/renderers/FileWriter.kt @@ -0,0 +1,32 @@ +package org.jetbrains.dokka.renderers + +import java.io.File +import java.io.IOException +import java.nio.file.Paths + +class FileWriter(val root: String, val extension: String){ + private val createdFiles: MutableMap<String, Int> = mutableMapOf() + + fun write(path: String, text: String, ext: String = extension){ + if (createdFiles.getOrDefault(path, 0) > 0) { + println("ERROR. An attempt to write $root/$path several times!") + return + } + + try { + println("Writing $root/$path$ext") + val dir = Paths.get(root, path.dropLastWhile { it != '/' }).toFile() + dir.mkdirsOrFail() + Paths.get(root, "$path$ext").toFile().writeText(text) + } catch (e : Throwable) { + println("Failed to write $this. ${e.message}") + e.printStackTrace() + } + } + + private fun File.mkdirsOrFail() { + if (!mkdirs() && !exists()) { + throw IOException("Failed to create directory $this") + } + } +}
\ No newline at end of file diff --git a/core/src/main/kotlin/renderers/HtmlRenderer.kt b/core/src/main/kotlin/renderers/HtmlRenderer.kt new file mode 100644 index 00000000..3a6e1576 --- /dev/null +++ b/core/src/main/kotlin/renderers/HtmlRenderer.kt @@ -0,0 +1,51 @@ +package org.jetbrains.dokka.renderers + +import org.jetbrains.dokka.pages.ContentLink +import org.jetbrains.dokka.pages.ContentNode +import org.jetbrains.dokka.pages.PageNode +import org.jetbrains.dokka.resolvers.LocationProvider + +open class HtmlRenderer(outputDir: String, fileWriter: FileWriter, locationProvider: LocationProvider): DefaultRenderer(outputDir, fileWriter, locationProvider) { + + override fun buildComment(parts: List<ContentNode>): String = "<p>${super.buildComment(parts)}</p>" + + override fun buildSymbol(parts: List<ContentNode>): String = "<code>${super.buildSymbol(parts)}</code>" + + override fun buildHeader(level: Int, text: String): String = "<h$level>$text</h$level>\n" + + override fun buildNewLine(): String = "<br/>" + + override fun buildLink(text: String, address: String): String = "<a href=\"$address\">$text</a>" + + override fun buildCode(code: String): String = "<code>$code</code>" + + override fun buildNavigation(): String = "" // TODO implement + + override fun buildGroup(children: List<ContentNode>): String = "<tr>\n" + + "<td>" + children.find { it is ContentLink }?.build() + "</td>\n" + + "<td>" + children.filterNot { it is ContentLink }.joinToString("\n") { it.build() } + "</td>\n" + + "</tr>\n" + + override fun buildBlock(name: String, content: List<ContentNode>): String = + buildHeader(2, name) + "<table>\n" + content.joinToString("\n") { it.build() } + "</table>" + + override fun renderPage(page: PageNode) { + val pageText = buildStartHtml(page) + buildPageContent(page) + buildEndHtml() + fileWriter.write(locationProvider.resolve(page), pageText) + } + + protected open fun buildStartHtml(page: PageNode) = """<!DOCTYPE html> + |<html> + |<head> + |<title>${page.name}</title> + |</head> + |<body> + |""".trimMargin() + + protected open fun buildEndHtml() = + """ + | + |</body> + |</html> + """.trimMargin() +}
\ No newline at end of file diff --git a/core/src/main/kotlin/renderers/Renderer.kt b/core/src/main/kotlin/renderers/Renderer.kt new file mode 100644 index 00000000..24e01cdb --- /dev/null +++ b/core/src/main/kotlin/renderers/Renderer.kt @@ -0,0 +1,14 @@ +package org.jetbrains.dokka.renderers + +import org.jetbrains.dokka.pages.PageNode + +interface Renderer { + fun render(root: PageNode) +} + +//class Renderers{ +// abstract class Renderer(resolvers: List[Resolver]) +// class HtmlRenderer(list) +// fun toHtml() = { +// new HtmlRenderer(a, b,c) +//}
\ No newline at end of file |