aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating/src/main/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/templating/src/main/kotlin')
-rw-r--r--plugins/templating/src/main/kotlin/templates/AddToNavigationCommandHandler.kt56
-rw-r--r--plugins/templating/src/main/kotlin/templates/CommandHandler.kt11
-rw-r--r--plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt41
-rw-r--r--plugins/templating/src/main/kotlin/templates/FallbackTemplateProcessingStrategy.kt13
-rw-r--r--plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt68
-rw-r--r--plugins/templating/src/main/kotlin/templates/PathToRootSubstitutor.kt14
-rw-r--r--plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt60
-rw-r--r--plugins/templating/src/main/kotlin/templates/Substitutor.kt7
-rw-r--r--plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt56
-rw-r--r--plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt50
10 files changed, 376 insertions, 0 deletions
diff --git a/plugins/templating/src/main/kotlin/templates/AddToNavigationCommandHandler.kt b/plugins/templating/src/main/kotlin/templates/AddToNavigationCommandHandler.kt
new file mode 100644
index 00000000..3e7e1290
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/AddToNavigationCommandHandler.kt
@@ -0,0 +1,56 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.AddToNavigationCommand
+import org.jetbrains.dokka.base.templating.Command
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jsoup.nodes.Attributes
+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 AddToNavigationCommandHandler(val context: DokkaContext) : CommandHandler {
+ private val navigationFragments = ConcurrentHashMap<String, Element>()
+
+ override fun handleCommand(element: Element, command: Command, input: File, output: File) {
+ command as AddToNavigationCommand
+ context.configuration.modules.find { it.name == command.moduleName }
+ ?.relativePathToOutputDirectory
+ ?.relativeToOrSelf(context.configuration.outputDir)
+ ?.let { key -> navigationFragments[key.toString()] = element }
+ }
+
+ override fun canHandle(command: Command) = command is AddToNavigationCommand
+
+ override fun finish(output: File) {
+ if (navigationFragments.isNotEmpty()) {
+ 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)
+ }
+ }
+
+ Files.write(output.resolve("navigation.html").toPath(), listOf(node.outerHtml()))
+ node.select("a").forEach { a ->
+ a.attr("href")?.also { a.attr("href", "../${it}") }
+ }
+ navigationFragments.keys.forEach {
+ Files.write(
+ output.resolve(it).resolve("navigation.html").toPath(),
+ listOf(node.outerHtml())
+ )
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/CommandHandler.kt b/plugins/templating/src/main/kotlin/templates/CommandHandler.kt
new file mode 100644
index 00000000..d72092a1
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/CommandHandler.kt
@@ -0,0 +1,11 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.Command
+import org.jsoup.nodes.Element
+import java.io.File
+
+interface CommandHandler {
+ fun handleCommand(element: Element, command: Command, input: File, output: File)
+ fun canHandle(command: Command): Boolean
+ fun finish(output: File) {}
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt b/plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt
new file mode 100644
index 00000000..c3b9aa53
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt
@@ -0,0 +1,41 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.Command
+import org.jetbrains.dokka.base.templating.parseJson
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.query
+import org.jsoup.Jsoup
+import org.jsoup.nodes.Element
+import java.io.File
+import java.nio.file.Files
+
+class DirectiveBasedHtmlTemplateProcessingStrategy(private val context: DokkaContext) : TemplateProcessingStrategy {
+
+ private val directiveBasedCommandHandlers =
+ context.plugin<TemplatingPlugin>().query { directiveBasedCommandHandlers }
+
+ override fun process(input: File, output: File): Boolean =
+ if (input.isFile && input.extension == "html") {
+ val document = Jsoup.parse(input, "UTF-8")
+ document.outputSettings().indentAmount(0).prettyPrint(false)
+ document.select("dokka-template-command").forEach {
+ handleCommand(it, parseJson(it.attr("data")), input, output)
+ }
+ Files.write(output.toPath(), listOf(document.outerHtml()))
+ true
+ } else false
+
+ fun handleCommand(element: Element, command: Command, input: File, output: File) {
+ val handlers = directiveBasedCommandHandlers.filter { it.canHandle(command) }
+ if (handlers.isEmpty())
+ context.logger.warn("Unknown templating command $command")
+ else
+ handlers.forEach { it.handleCommand(element, command, input, output) }
+
+ }
+
+ override fun finish(output: File) {
+ directiveBasedCommandHandlers.forEach { it.finish(output) }
+ }
+}
diff --git a/plugins/templating/src/main/kotlin/templates/FallbackTemplateProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/FallbackTemplateProcessingStrategy.kt
new file mode 100644
index 00000000..4e88c318
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/FallbackTemplateProcessingStrategy.kt
@@ -0,0 +1,13 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.plugability.DokkaContext
+import java.io.File
+import java.nio.file.Files
+
+class FallbackTemplateProcessingStrategy(dokkaContext: DokkaContext) : TemplateProcessingStrategy {
+
+ override fun process(input: File, output: File): Boolean {
+ if(input != output) input.copyTo(output, overwrite = true)
+ return true
+ }
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
new file mode 100644
index 00000000..a2d55209
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt
@@ -0,0 +1,68 @@
+package org.jetbrains.dokka.allModulesPage.templates
+
+import org.jetbrains.dokka.base.renderers.html.SearchRecord
+import org.jetbrains.dokka.base.templating.AddToSearch
+import org.jetbrains.dokka.base.templating.parseJson
+import org.jetbrains.dokka.base.templating.toJsonString
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.templates.TemplateProcessingStrategy
+import java.io.File
+import java.util.concurrent.ConcurrentHashMap
+
+abstract class BaseJsonNavigationTemplateProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy {
+ abstract val navigationFileNameWithoutExtension: String
+ abstract val path: String
+
+ private val fragments = ConcurrentHashMap<String, List<SearchRecord>>()
+
+ open fun canProcess(file: File): Boolean =
+ file.extension == "json" && file.nameWithoutExtension == navigationFileNameWithoutExtension
+
+ override fun process(input: File, output: File): Boolean {
+ val canProcess = canProcess(input)
+ if (canProcess) {
+ runCatching { parseJson<AddToSearch>(input.readText()) }.getOrNull()?.let { command ->
+ context.configuration.modules.find { it.name == command.moduleName }?.relativePathToOutputDirectory
+ ?.relativeToOrSelf(context.configuration.outputDir)
+ ?.let { key ->
+ fragments[key.toString()] = command.elements
+ }
+ } ?: fallbackToCopy(input, output)
+ }
+ return canProcess
+ }
+
+ override fun finish(output: File) {
+ if (fragments.isNotEmpty()) {
+ val content = toJsonString(fragments.entries.flatMap { (moduleName, navigation) ->
+ navigation.map { it.withResolvedLocation(moduleName) }
+ })
+ output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
+
+ fragments.keys.forEach {
+ output.resolve(it).resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content)
+ }
+ }
+ }
+
+ private fun fallbackToCopy(input: File, output: File) {
+ context.logger.warn("Falling back to just copying file for ${input.name} even thought it should process it")
+ input.copyTo(output)
+ }
+
+ private fun SearchRecord.withResolvedLocation(moduleName: String): SearchRecord =
+ copy(location = "$moduleName/$location")
+
+}
+
+class NavigationSearchTemplateStrategy(val dokkaContext: DokkaContext) :
+ BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) {
+ override val navigationFileNameWithoutExtension: String = "navigation-pane"
+ override val path: String = "scripts"
+}
+
+class PagesSearchTemplateStrategy(val dokkaContext: DokkaContext) :
+ BaseJsonNavigationTemplateProcessingStrategy(dokkaContext) {
+ override val navigationFileNameWithoutExtension: String = "pages"
+ override val path: String = "scripts"
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/PathToRootSubstitutor.kt b/plugins/templating/src/main/kotlin/templates/PathToRootSubstitutor.kt
new file mode 100644
index 00000000..da81432e
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/PathToRootSubstitutor.kt
@@ -0,0 +1,14 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.PathToRootSubstitutionCommand
+import org.jetbrains.dokka.base.templating.SubstitutionCommand
+import org.jetbrains.dokka.plugability.DokkaContext
+import java.io.File
+
+class PathToRootSubstitutor(private val dokkaContext: DokkaContext) : Substitutor {
+
+ override fun trySubstitute(context: TemplatingContext<SubstitutionCommand>, match: MatchResult): String? =
+ if (context.command is PathToRootSubstitutionCommand) {
+ context.output.toPath().parent.relativize(dokkaContext.configuration.outputDir.toPath()).toString().split(File.separator).joinToString(separator = "/", postfix = "/") { it }
+ } else null
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt b/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt
new file mode 100644
index 00000000..c7b15137
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt
@@ -0,0 +1,60 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.Command
+import org.jetbrains.dokka.base.templating.SubstitutionCommand
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.query
+import org.jsoup.nodes.DataNode
+import org.jsoup.nodes.Element
+import org.jsoup.nodes.Node
+import org.jsoup.nodes.TextNode
+import java.io.File
+
+class SubstitutionCommandHandler(context: DokkaContext) : CommandHandler {
+
+ override fun handleCommand(element: Element, command: Command, input: File, output: File) {
+ command as SubstitutionCommand
+ substitute(element, TemplatingContext(input, output, element, command))
+ }
+
+ override fun canHandle(command: Command): Boolean = command is SubstitutionCommand
+
+ private val substitutors = context.plugin<TemplatingPlugin>().query { substitutor }
+
+ private fun findSubstitution(commandContext: TemplatingContext<SubstitutionCommand>, match: MatchResult): String =
+ substitutors.asSequence().mapNotNull { it.trySubstitute(commandContext, match) }.firstOrNull() ?: match.value
+
+ private fun substitute(element: Element, commandContext: TemplatingContext<SubstitutionCommand>) {
+ val regex = commandContext.command.pattern.toRegex()
+ element.children().forEach { it.traverseToSubstitute(regex, commandContext) }
+
+ val childrenCopy = element.children().toList()
+ val position = element.elementSiblingIndex()
+ val parent = element.parent()
+ element.remove()
+
+ parent.insertChildren(position, childrenCopy)
+ }
+
+ private fun Node.traverseToSubstitute(regex: Regex, commandContext: TemplatingContext<SubstitutionCommand>) {
+ when (this) {
+ is TextNode -> replaceWith(TextNode(wholeText.substitute(regex, commandContext)))
+ is DataNode -> replaceWith(DataNode(wholeData.substitute(regex, commandContext)))
+ is Element -> {
+ attributes().forEach { attr(it.key, it.value.substitute(regex, commandContext)) }
+ childNodes().forEach { it.traverseToSubstitute(regex, commandContext) }
+ }
+ }
+ }
+
+ private fun String.substitute(regex: Regex, commandContext: TemplatingContext<SubstitutionCommand>) = buildString {
+ var lastOffset = 0
+ regex.findAll(this@substitute).forEach { match ->
+ append(this@substitute, lastOffset, match.range.first)
+ append(findSubstitution(commandContext, match))
+ lastOffset = match.range.last + 1
+ }
+ append(this@substitute, lastOffset, this@substitute.length)
+ }
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/Substitutor.kt b/plugins/templating/src/main/kotlin/templates/Substitutor.kt
new file mode 100644
index 00000000..55463974
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/Substitutor.kt
@@ -0,0 +1,7 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.base.templating.SubstitutionCommand
+
+fun interface Substitutor {
+ fun trySubstitute(context: TemplatingContext<SubstitutionCommand>, match: MatchResult): String?
+} \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt
new file mode 100644
index 00000000..8fbd76b6
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt
@@ -0,0 +1,56 @@
+package org.jetbrains.dokka.templates
+
+import kotlinx.coroutines.*
+import org.jetbrains.dokka.base.templating.Command
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.query
+import org.jsoup.nodes.Element
+import java.io.File
+
+interface TemplateProcessor {
+ fun process()
+}
+
+interface TemplateProcessingStrategy {
+ fun process(input: File, output: File): Boolean
+ fun finish(output: File) {}
+}
+
+class DefaultTemplateProcessor(
+ private val context: DokkaContext,
+): TemplateProcessor {
+
+ private val strategies: List<TemplateProcessingStrategy> = context.plugin<TemplatingPlugin>().query { templateProcessingStrategy }
+
+ override fun process() = runBlocking(Dispatchers.Default) {
+ coroutineScope {
+ context.configuration.modules.forEach {
+ launch {
+ it.sourceOutputDirectory.visit(context.configuration.outputDir.resolve(it.relativePathToOutputDirectory))
+ }
+ }
+ }
+ strategies.map { it.finish(context.configuration.outputDir) }
+ Unit
+ }
+
+ 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 {
+ strategies.first { it.process(source, target) }
+ }
+ }
+}
+
+data class TemplatingContext<out T: Command>(
+ val input: File,
+ val output: File,
+ val element: Element,
+ val command: T,
+) \ No newline at end of file
diff --git a/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt
new file mode 100644
index 00000000..29ca4904
--- /dev/null
+++ b/plugins/templating/src/main/kotlin/templates/TemplatingPlugin.kt
@@ -0,0 +1,50 @@
+package org.jetbrains.dokka.templates
+
+import org.jetbrains.dokka.allModulesPage.templates.NavigationSearchTemplateStrategy
+import org.jetbrains.dokka.allModulesPage.templates.PagesSearchTemplateStrategy
+import org.jetbrains.dokka.plugability.DokkaPlugin
+
+class TemplatingPlugin : DokkaPlugin() {
+
+ val templateProcessor by extensionPoint<TemplateProcessor>()
+ val templateProcessingStrategy by extensionPoint<TemplateProcessingStrategy>()
+ val directiveBasedCommandHandlers by extensionPoint<CommandHandler>()
+
+ val substitutor by extensionPoint<Substitutor>()
+
+ val defaultTemplateProcessor by extending {
+ templateProcessor providing ::DefaultTemplateProcessor
+ }
+
+ val directiveBasedHtmlTemplateProcessingStrategy by extending {
+ templateProcessingStrategy providing ::DirectiveBasedHtmlTemplateProcessingStrategy order {
+ before(fallbackProcessingStrategy)
+ }
+ }
+ val navigationSearchTemplateStrategy by extending {
+ templateProcessingStrategy providing ::NavigationSearchTemplateStrategy order {
+ before(fallbackProcessingStrategy)
+ }
+ }
+
+ val pagesSearchTemplateStrategy by extending {
+ templateProcessingStrategy providing ::PagesSearchTemplateStrategy order {
+ before(fallbackProcessingStrategy)
+ }
+ }
+
+ val fallbackProcessingStrategy by extending {
+ templateProcessingStrategy providing ::FallbackTemplateProcessingStrategy
+ }
+
+ val pathToRootSubstitutor by extending {
+ substitutor providing ::PathToRootSubstitutor
+ }
+
+ val addToNavigationCommandHandler by extending {
+ directiveBasedCommandHandlers providing ::AddToNavigationCommandHandler
+ }
+ val substitutionCommandHandler by extending {
+ directiveBasedCommandHandlers providing ::SubstitutionCommandHandler
+ }
+} \ No newline at end of file