aboutsummaryrefslogtreecommitdiff
path: root/plugins/all-module-page/src/main/kotlin
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-09-23 19:48:14 +0200
committerBłażej Kardyś <bkardys@virtuslab.com>2020-11-27 03:15:02 +0100
commit52f64c664573567259f8f678d32924f86b4f147c (patch)
tree0882ce51f477acc71412b36307495a0e0ba97359 /plugins/all-module-page/src/main/kotlin
parent80b6d1824960205e1c1d57c0c51e913d3c2360db (diff)
downloaddokka-52f64c664573567259f8f678d32924f86b4f147c.tar.gz
dokka-52f64c664573567259f8f678d32924f86b4f147c.tar.bz2
dokka-52f64c664573567259f8f678d32924f86b4f147c.zip
Add navigation fragments for templating
Diffstat (limited to 'plugins/all-module-page/src/main/kotlin')
-rw-r--r--plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt46
-rw-r--r--plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt11
2 files changed, 51 insertions, 6 deletions
diff --git a/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt b/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt
index 7786ca18..705e6678 100644
--- a/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt
+++ b/plugins/all-module-page/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
+import org.jetbrains.dokka.base.templating.AddToNavigationCommand
import org.jetbrains.dokka.base.templating.Command
import org.jetbrains.dokka.base.templating.ResolveLinkCommand
import org.jetbrains.dokka.base.templating.parseJson
@@ -14,8 +15,11 @@ import org.jsoup.nodes.Element
import org.jsoup.parser.Tag
import java.io.File
import java.nio.file.Files
+import java.util.concurrent.ConcurrentHashMap
+
+class DirectiveBasedTemplateProcessingStrategy(private val context: DokkaContext) : TemplateProcessingStrategy {
+ private val navigationFragments = ConcurrentHashMap<String, Element>()
-class DirectiveBasedTemplateProcessingStrategy(context: DokkaContext) : TemplateProcessingStrategy {
override suspend fun process(input: File, output: File): Unit = coroutineScope {
if (input.extension == "html") {
launch {
@@ -23,8 +27,10 @@ class DirectiveBasedTemplateProcessingStrategy(context: DokkaContext) : Template
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)
+ when (command) {
+ is ResolveLinkCommand -> resolveLink(it, command)
+ is AddToNavigationCommand -> navigationFragments[command.moduleName] = it
+ else -> context.logger.warn("Unknown templating command $command")
}
}
withContext(IO) { Files.writeString(output.toPath(), document.outerHtml()) }
@@ -36,6 +42,40 @@ class DirectiveBasedTemplateProcessingStrategy(context: DokkaContext) : Template
}
}
+ override suspend fun finish(output: File) {
+ val attributes = Attributes().apply {
+ put("class", "sideMenu")
+ }
+ val node = Element(Tag.valueOf("div"), "", attributes)
+ navigationFragments.entries.sortedBy { it.key }.forEach { (moduleName, command) ->
+ command.select("a").forEach { a ->
+ a.attr("href")?.also { a.attr("href", "${moduleName}/${it}") }
+ }
+ command.childNodes().toList().forEachIndexed { index, child ->
+ if (index == 0) {
+ child.attr("id", "$moduleName-nav-submenu")
+ }
+ node.appendChild(child)
+ }
+ }
+
+ withContext(IO) {
+ Files.writeString(output.resolve("navigation.html").toPath(), node.outerHtml())
+ }
+
+ node.select("a").forEach { a ->
+ a.attr("href")?.also { a.attr("href", "../${it}") }
+ }
+ navigationFragments.keys.forEach {
+ withContext(IO) {
+ Files.writeString(
+ output.resolve(it).resolve("navigation.html").toPath(),
+ node.outerHtml()
+ )
+ }
+ }
+ }
+
private fun resolveLink(it: Element, command: ResolveLinkCommand) {
val attributes = Attributes().apply {
put("href", "") // TODO: resolve
diff --git a/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt b/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt
index 4c247d94..cd144046 100644
--- a/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt
+++ b/plugins/all-module-page/src/main/kotlin/templates/TemplateProcessor.kt
@@ -5,6 +5,7 @@ import org.jetbrains.dokka.plugability.DokkaContext
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
+import kotlin.coroutines.coroutineContext
interface TemplateProcessor {
fun process()
@@ -12,6 +13,7 @@ interface TemplateProcessor {
interface TemplateProcessingStrategy {
suspend fun process(input: File, output: File)
+ suspend fun finish(output: File) {}
}
class DefaultTemplateProcessor(
@@ -19,11 +21,14 @@ class DefaultTemplateProcessor(
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))
+ coroutineScope {
+ context.configuration.modules.forEach {
+ launch {
+ it.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(it.relativePathToOutputDirectory))
+ }
}
}
+ strategy.finish(context.configuration.outputDir)
}
private suspend fun File.visit(target: File): Unit = coroutineScope {