diff options
Diffstat (limited to 'core/src/main/kotlin/renderers/DefaultRenderer.kt')
-rw-r--r-- | core/src/main/kotlin/renderers/DefaultRenderer.kt | 67 |
1 files changed, 36 insertions, 31 deletions
diff --git a/core/src/main/kotlin/renderers/DefaultRenderer.kt b/core/src/main/kotlin/renderers/DefaultRenderer.kt index ae065305..e2ebf8b3 100644 --- a/core/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/core/src/main/kotlin/renderers/DefaultRenderer.kt @@ -4,62 +4,67 @@ import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.resolvers.LocationProvider -abstract class DefaultRenderer( +abstract class DefaultRenderer<T>( protected val fileWriter: FileWriter, protected val locationProvider: LocationProvider, protected val context: DokkaContext ) : Renderer { - protected abstract fun buildHeader(level: Int, text: String): String - protected abstract fun buildLink(text: String, address: String): String - 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 abstract fun T.buildHeader(level: Int, content: T.() -> Unit) + protected abstract fun T.buildLink(address: String, content: T.() -> Unit) + protected abstract fun T.buildList(node: ContentList, pageContext: PageNode) + protected abstract fun T.buildNewLine() + protected abstract fun T.buildResource(node: ContentEmbeddedResource, pageContext: PageNode) + protected abstract fun T.buildTable(node: ContentTable, pageContext: PageNode) + protected abstract fun T.buildText(textNode: ContentText) + protected abstract fun T.buildNavigation(page: PageNode) - protected open fun buildText(textNode: ContentText): String = textNode.text + protected abstract fun buildPage(page: PageNode, content: (T, PageNode) -> Unit): String + protected abstract fun buildError(node: ContentNode) - 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 T.buildGroup(node: ContentGroup, pageContext: PageNode){ + node.children.forEach { it.build(this, pageContext) } + } - protected open fun buildCode(code: List<ContentNode>, language: String, pageContext: PageNode): String = - code.joinToString { it.build(pageContext) } + protected open fun T.buildLinkText(nodes: List<ContentNode>, pageContext: PageNode) { + nodes.forEach { it.build(this, pageContext) } + } - protected open fun buildHeader(node: ContentHeader, pageContext: PageNode): String = - buildHeader(node.level, node.children.joinToString { it.build(pageContext) }) + protected open fun T.buildCode(code: List<ContentNode>, language: String, pageContext: PageNode) { + code.forEach { it.build(this, pageContext) } + } - protected open fun buildNavigation(page: PageNode): String = - locationProvider.ancestors(page).fold("") { acc, node -> "$acc/${buildLink( - node.name, - locationProvider.resolve(node, page) - )}" } + protected open fun T.buildHeader(node: ContentHeader, pageContext: PageNode) { + buildHeader(node.level) { node.children.forEach { it.build(this, pageContext) } } + } - protected open fun ContentNode.build(pageContext: PageNode): String = buildContentNode(this, pageContext) + protected open fun ContentNode.build(builder: T, pageContext: PageNode) = builder.buildContentNode(this, pageContext) - protected open fun buildContentNode(node: ContentNode, pageContext: PageNode): String = + protected open fun T.buildContentNode(node: ContentNode, pageContext: PageNode) { 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) + locationProvider.resolve(node.address, node.platforms.toList(), pageContext)) { + buildLinkText(node.children, pageContext) + } + is ContentResolvedLink -> buildLink(node.address) {buildLinkText(node.children, pageContext)} 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") } + else -> buildError(node) } + } - protected open fun buildPageContent(page: PageNode): String = - buildNavigation(page) + page.content.build(page) + protected open fun buildPageContent(context: T, page: PageNode) { + context.buildNavigation(page) + page.content.build(context, page) + } protected open fun renderPage(page: PageNode) = - fileWriter.write(locationProvider.resolve(page), buildPageContent(page), "") + fileWriter.write(locationProvider.resolve(page), buildPage(page, ::buildPageContent), "") protected open fun renderPages(root: PageNode) { renderPage(root) |