diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2020-09-22 17:29:21 +0200 |
---|---|---|
committer | Błażej Kardyś <bkardys@virtuslab.com> | 2020-11-27 03:15:02 +0100 |
commit | 80b6d1824960205e1c1d57c0c51e913d3c2360db (patch) | |
tree | ea40c9c6dc0bd24999312cfa49a01c43cb746926 /plugins/all-module-page/src/main | |
parent | c8a83153a88fe6f5b50b6f459295421f90a21583 (diff) | |
download | dokka-80b6d1824960205e1c1d57c0c51e913d3c2360db.tar.gz dokka-80b6d1824960205e1c1d57c0c51e913d3c2360db.tar.bz2 dokka-80b6d1824960205e1c1d57c0c51e913d3c2360db.zip |
Add templating commands to the html format
Diffstat (limited to 'plugins/all-module-page/src/main')
4 files changed, 104 insertions, 0 deletions
diff --git a/plugins/all-module-page/src/main/kotlin/AllModulesPageGeneration.kt b/plugins/all-module-page/src/main/kotlin/AllModulesPageGeneration.kt index 815cf160..f654514a 100644 --- a/plugins/all-module-page/src/main/kotlin/AllModulesPageGeneration.kt +++ b/plugins/all-module-page/src/main/kotlin/AllModulesPageGeneration.kt @@ -5,6 +5,8 @@ import org.jetbrains.dokka.Timer import org.jetbrains.dokka.generation.Generation import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.plugin +import org.jetbrains.dokka.plugability.querySingle class AllModulesPageGeneration(private val context: DokkaContext) : Generation { override fun Timer.generate() { @@ -16,6 +18,9 @@ class AllModulesPageGeneration(private val context: DokkaContext) : Generation { report("Rendering") render(transformedPages) + + report("Processing submodules") + context.plugin<AllModulesPagePlugin>().querySingle { templateProcessor }.process() } override val generationName = "index page for project" diff --git a/plugins/all-module-page/src/main/kotlin/AllModulesPagePlugin.kt b/plugins/all-module-page/src/main/kotlin/AllModulesPagePlugin.kt index 163f13ab..584de32f 100644 --- a/plugins/all-module-page/src/main/kotlin/AllModulesPagePlugin.kt +++ b/plugins/all-module-page/src/main/kotlin/AllModulesPagePlugin.kt @@ -1,10 +1,15 @@ package org.jetbrains.dokka.allModulesPage import org.jetbrains.dokka.CoreExtensions +import org.jetbrains.dokka.allModulesPage.templates.DefaultTemplateProcessor +import org.jetbrains.dokka.allModulesPage.templates.DirectiveBasedTemplateProcessingStrategy +import org.jetbrains.dokka.allModulesPage.templates.TemplateProcessor import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.plugability.DokkaPlugin class AllModulesPagePlugin : DokkaPlugin() { + val templateProcessor by extensionPoint<TemplateProcessor>() + val allModulePageCreators by extending { (CoreExtensions.allModulePageCreator providing ::MultimodulePageCreator) @@ -22,4 +27,8 @@ class AllModulesPagePlugin : DokkaPlugin() { providing ::AllModulesPageGeneration override CoreExtensions.singleGeneration) } + + val defaultTemplateProcessor by extending { + templateProcessor providing { DefaultTemplateProcessor(it, DirectiveBasedTemplateProcessingStrategy(it)) } + } }
\ No newline at end of file diff --git a/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt b/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt new file mode 100644 index 00000000..7786ca18 --- /dev/null +++ b/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt @@ -0,0 +1,49 @@ +package org.jetbrains.dokka.allModulesPage.templates + +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import org.jetbrains.dokka.base.templating.Command +import org.jetbrains.dokka.base.templating.ResolveLinkCommand +import org.jetbrains.dokka.base.templating.parseJson +import org.jetbrains.dokka.plugability.DokkaContext +import org.jsoup.Jsoup +import org.jsoup.nodes.Attributes +import org.jsoup.nodes.Element +import org.jsoup.parser.Tag +import java.io.File +import java.nio.file.Files + +class DirectiveBasedTemplateProcessingStrategy(context: DokkaContext) : TemplateProcessingStrategy { + override suspend fun process(input: File, output: File): Unit = coroutineScope { + if (input.extension == "html") { + launch { + val document = withContext(IO) { Jsoup.parse(input, "UTF-8") } + document.outputSettings().indentAmount(0).prettyPrint(false) + document.select("dokka-template-command").forEach { + val command = parseJson<Command>(it.attr("data")) + if (command is ResolveLinkCommand) { + resolveLink(it, command) + } + } + withContext(IO) { Files.writeString(output.toPath(), document.outerHtml()) } + } + } else { + launch(IO) { + Files.copy(input.toPath(), output.toPath()) + } + } + } + + private fun resolveLink(it: Element, command: ResolveLinkCommand) { + val attributes = Attributes().apply { + put("href", "") // TODO: resolve + } + val children = it.childNodes().toList() + val element = Element(Tag.valueOf("a"), "", attributes).apply { + children.forEach { ch -> appendChild(ch) } + } + it.replaceWith(element) + } +}
\ No newline at end of file diff --git a/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt b/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt new file mode 100644 index 00000000..4c247d94 --- /dev/null +++ b/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt @@ -0,0 +1,41 @@ +package org.jetbrains.dokka.allModulesPage.templates + +import kotlinx.coroutines.* +import org.jetbrains.dokka.plugability.DokkaContext +import java.io.File +import java.nio.file.Files +import java.nio.file.Path + +interface TemplateProcessor { + fun process() +} + +interface TemplateProcessingStrategy { + suspend fun process(input: File, output: File) +} + +class DefaultTemplateProcessor( + private val context: DokkaContext, + private val strategy: TemplateProcessingStrategy +): TemplateProcessor { + override fun process() = runBlocking(Dispatchers.Default) { + context.configuration.modules.forEach { + launch { + it.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(it.relativePathToOutputDirectory)) + } + } + } + + private suspend fun File.visit(target: File): Unit = coroutineScope { + val source = this@visit + if (source.isDirectory) { + target.mkdir() + source.list()?.forEach { + launch { source.resolve(it).visit(target.resolve(it)) } + } + } else { + strategy.process(source, target) + } + } +} + |