diff --git a/plugins/all-modules-page/api/all-modules-page.api b/plugins/all-modules-page/api/all-modules-page.api index 9d8bca9b48..d9724a9a2d 100644 --- a/plugins/all-modules-page/api/all-modules-page.api +++ b/plugins/all-modules-page/api/all-modules-page.api @@ -83,5 +83,6 @@ public final class org/jetbrains/dokka/allModulesPage/ResolveLinkCommandHandler public fun canHandle (Lorg/jetbrains/dokka/base/templating/Command;)Z public fun finish (Ljava/io/File;)V public fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } diff --git a/plugins/base/api/base.api b/plugins/base/api/base.api index 8ee15183f7..6906627c5e 100644 --- a/plugins/base/api/base.api +++ b/plugins/base/api/base.api @@ -514,6 +514,7 @@ public final class org/jetbrains/dokka/base/renderers/html/StylesInstaller : org public final class org/jetbrains/dokka/base/renderers/html/TagsKt { public static final field TEMPLATE_COMMAND_BEGIN_BORDER Ljava/lang/String; public static final field TEMPLATE_COMMAND_END_BORDER Ljava/lang/String; + public static final field TEMPLATE_COMMAND_SEPARATOR Ljava/lang/String; public static final fun buildAsInnerHtml (Lkotlin/jvm/functions/Function1;)Ljava/lang/String; public static final fun strike (Lkotlinx/html/FlowOrPhrasingContent;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V public static synthetic fun strike$default (Lkotlinx/html/FlowOrPhrasingContent;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V diff --git a/plugins/base/src/main/kotlin/renderers/html/Tags.kt b/plugins/base/src/main/kotlin/renderers/html/Tags.kt index ec4603ac5f..94a53c27fb 100644 --- a/plugins/base/src/main/kotlin/renderers/html/Tags.kt +++ b/plugins/base/src/main/kotlin/renderers/html/Tags.kt @@ -26,13 +26,14 @@ inline fun FlowOrPhrasingContent.strike(classes : String? = null, crossinline bl open class STRIKE(initialAttributes: Map, override val consumer: TagConsumer<*>) : HTMLTag("strike", consumer, initialAttributes, null, false, false), HtmlBlockInlineTag -const val TEMPLATE_COMMAND_BEGIN_BORDER = "[+]cmd:" +const val TEMPLATE_COMMAND_SEPARATOR = ":" +const val TEMPLATE_COMMAND_BEGIN_BORDER = "[+]cmd" const val TEMPLATE_COMMAND_END_BORDER = "[-]cmd" fun FlowOrMetaDataContent.templateCommandAsHtmlComment(data: Command, block: FlowOrMetaDataContent.() -> Unit = {}): Unit = (consumer as? ImmediateResolutionTagConsumer)?.processCommand(data, block) ?: let{ - comment( "$TEMPLATE_COMMAND_BEGIN_BORDER${toJsonString(data)}") + comment( "$TEMPLATE_COMMAND_BEGIN_BORDER$TEMPLATE_COMMAND_SEPARATOR${toJsonString(data)}") block() comment(TEMPLATE_COMMAND_END_BORDER) } diff --git a/plugins/base/src/main/kotlin/renderers/html/command/consumers/ReplaceVersionsConsumer.kt b/plugins/base/src/main/kotlin/renderers/html/command/consumers/ReplaceVersionsConsumer.kt index ea8cde0f4c..bbc33164a7 100644 --- a/plugins/base/src/main/kotlin/renderers/html/command/consumers/ReplaceVersionsConsumer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/command/consumers/ReplaceVersionsConsumer.kt @@ -19,7 +19,7 @@ class ReplaceVersionsConsumer(private val context: DokkaContext) : ImmediateHtml } override fun processCommandAndFinalize(command: Command, block: TemplateBlock, tagConsumer: ImmediateResolutionTagConsumer): R { - PathToRootConsumer.processCommand(command, block, tagConsumer) + processCommand(command, block, tagConsumer) return tagConsumer.finalize() } } \ No newline at end of file diff --git a/plugins/templating/api/templating.api b/plugins/templating/api/templating.api index 6ad3983123..17c9751553 100644 --- a/plugins/templating/api/templating.api +++ b/plugins/templating/api/templating.api @@ -28,26 +28,19 @@ public final class org/jetbrains/dokka/templates/AddToNavigationCommandHandler : public fun finish (Ljava/io/File;)V public final fun getContext ()Lorg/jetbrains/dokka/plugability/DokkaContext; public fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public abstract interface class org/jetbrains/dokka/templates/CommandHandler { public abstract fun canHandle (Lorg/jetbrains/dokka/base/templating/Command;)Z public abstract fun finish (Ljava/io/File;)V public abstract fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public abstract fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public final class org/jetbrains/dokka/templates/CommandHandler$DefaultImpls { public static fun finish (Lorg/jetbrains/dokka/templates/CommandHandler;Ljava/io/File;)V -} - -public abstract interface class org/jetbrains/dokka/templates/CommentCommandHandler { - public abstract fun canHandle (Lorg/jetbrains/dokka/base/templating/Command;)Z - public abstract fun finish (Ljava/io/File;)V - public abstract fun handleCommand (Ljava/util/List;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V -} - -public final class org/jetbrains/dokka/templates/CommentCommandHandler$DefaultImpls { - public static fun finish (Lorg/jetbrains/dokka/templates/CommentCommandHandler;Ljava/io/File;)V + public static fun handleCommandAsComment (Lorg/jetbrains/dokka/templates/CommandHandler;Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public final class org/jetbrains/dokka/templates/DefaultMultiModuleTemplateProcessor : org/jetbrains/dokka/templates/MultiModuleTemplateProcessor { @@ -64,8 +57,8 @@ public final class org/jetbrains/dokka/templates/DefaultSubmoduleTemplateProcess public final class org/jetbrains/dokka/templates/DirectiveBasedHtmlTemplateProcessingStrategy : org/jetbrains/dokka/templates/TemplateProcessingStrategy { public fun (Lorg/jetbrains/dokka/plugability/DokkaContext;)V public fun finish (Ljava/io/File;)V - public final fun handleCommand (Ljava/util/List;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V - public final fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public final fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V + public final fun handleCommandAsTag (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V public fun process (Ljava/io/File;Ljava/io/File;Lorg/jetbrains/dokka/DokkaConfiguration$DokkaModuleDescription;)Z } @@ -88,12 +81,12 @@ public abstract interface class org/jetbrains/dokka/templates/SubmoduleTemplateP public abstract fun process (Ljava/util/List;)Lorg/jetbrains/dokka/templates/TemplatingResult; } -public final class org/jetbrains/dokka/templates/SubstitutionCommandHandler : org/jetbrains/dokka/templates/CommandHandler, org/jetbrains/dokka/templates/CommentCommandHandler { +public final class org/jetbrains/dokka/templates/SubstitutionCommandHandler : org/jetbrains/dokka/templates/CommandHandler { public fun (Lorg/jetbrains/dokka/plugability/DokkaContext;)V public fun canHandle (Lorg/jetbrains/dokka/base/templating/Command;)Z public fun finish (Ljava/io/File;)V - public fun handleCommand (Ljava/util/List;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V public fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public abstract interface class org/jetbrains/dokka/templates/Substitutor { @@ -121,9 +114,9 @@ public final class org/jetbrains/dokka/templates/TemplatingContext { public final fun copy (Ljava/io/File;Ljava/io/File;Ljava/util/List;Lorg/jetbrains/dokka/base/templating/Command;)Lorg/jetbrains/dokka/templates/TemplatingContext; public static synthetic fun copy$default (Lorg/jetbrains/dokka/templates/TemplatingContext;Ljava/io/File;Ljava/io/File;Ljava/util/List;Lorg/jetbrains/dokka/base/templating/Command;ILjava/lang/Object;)Lorg/jetbrains/dokka/templates/TemplatingContext; public fun equals (Ljava/lang/Object;)Z + public final fun getBody ()Ljava/util/List; public final fun getCommand ()Lorg/jetbrains/dokka/base/templating/Command; public final fun getInput ()Ljava/io/File; - public final fun getNodes ()Ljava/util/List; public final fun getOutput ()Ljava/io/File; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -174,6 +167,7 @@ public final class templates/ReplaceVersionCommandHandler : org/jetbrains/dokka/ public fun canHandle (Lorg/jetbrains/dokka/base/templating/Command;)Z public fun finish (Ljava/io/File;)V public fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public final class templates/SourcesetDependencyProcessingStrategy : org/jetbrains/dokka/templates/TemplateProcessingStrategy { diff --git a/plugins/templating/src/main/kotlin/templates/CommandHandler.kt b/plugins/templating/src/main/kotlin/templates/CommandHandler.kt index 30277648b5..772754564c 100644 --- a/plugins/templating/src/main/kotlin/templates/CommandHandler.kt +++ b/plugins/templating/src/main/kotlin/templates/CommandHandler.kt @@ -5,14 +5,10 @@ import org.jsoup.nodes.Element import org.jsoup.nodes.Node 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) {} -} -interface CommentCommandHandler { - fun handleCommand(nodes: List, command: Command, input: File, output: File) +interface CommandHandler { + fun handleCommand(element: Element, command: Command, input: File, output: File) + fun handleCommandAsComment(command: Command, body: List, 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 index e242cd53e1..d5579f8a16 100644 --- a/plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt +++ b/plugins/templating/src/main/kotlin/templates/DirectiveBasedTemplateProcessing.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.templates import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.base.renderers.html.TEMPLATE_COMMAND_BEGIN_BORDER import org.jetbrains.dokka.base.renderers.html.TEMPLATE_COMMAND_END_BORDER +import org.jetbrains.dokka.base.renderers.html.TEMPLATE_COMMAND_SEPARATOR import org.jetbrains.dokka.base.templating.Command import org.jetbrains.dokka.base.templating.parseJson import org.jetbrains.dokka.plugability.DokkaContext @@ -27,41 +28,45 @@ class DirectiveBasedHtmlTemplateProcessingStrategy(private val context: DokkaCon document.outputSettings().indentAmount(0).prettyPrint(false) document.select("dokka-template-command").forEach { - handleCommand(it, parseJson(it.attr("data")), input, output) + handleCommandAsTag(it, parseJson(it.attr("data")), input, output) } - extractCommandsFromComments(document) { nodes, command -> - val nodesTrimed = - nodes.dropWhile { (it is TextNode && it.isBlank).also { res -> if (res) it.remove() } } - .dropLastWhile { (it is TextNode && it.isBlank).also { res -> if (res) it.remove() } } - handleCommand(nodesTrimed, command, input, output) + extractCommandsFromComments(document) { command, body -> + val bodyTrimed = + body.dropWhile { node -> (node is TextNode && node.isBlank).also { if (it) node.remove() } } + .dropLastWhile { node -> (node is TextNode && node.isBlank).also { if (it) node.remove() } } + handleCommandAsComment(command, bodyTrimed, 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) } + fun handleCommandAsTag(element: Element, command: Command, input: File, output: File) { + traverseHandlers(command) { handleCommand(element, command, input, output) } } - fun handleCommand(nodes: List, command: Command, input: File, output: File) { - val handlers = directiveBasedCommandHandlers.filterIsInstance().filter { it.canHandle(command) } + fun handleCommandAsComment(command: Command, body: List, input: File, output: File) { + traverseHandlers(command) { handleCommandAsComment(command, body, input, output) } + } + + private fun traverseHandlers(command: Command, action: CommandHandler.() -> Unit) { + val handlers = directiveBasedCommandHandlers.filter { it.canHandle(command) } if (handlers.isEmpty()) context.logger.warn("Unknown templating command $command") else - handlers.forEach { it.handleCommand(nodes, command, input, output) } + handlers.forEach(action) } - private fun extractCommandsFromComments(node: Node, startFrom: Int = 0, handler: (List, Command) -> Unit) { + private fun extractCommandsFromComments( + node: Node, + startFrom: Int = 0, + handler: (command: Command, body: List) -> Unit + ) { val nodes: MutableList = mutableListOf() var lastStartBorder: Comment? = null var firstStartBorder: Comment? = null - for (ind in startFrom until node.childNodeSize()) { - when (val currentChild = node.childNode(ind)) { + for (index in startFrom until node.childNodeSize()) { + when (val currentChild = node.childNode(index)) { is Comment -> if (currentChild.data?.startsWith(TEMPLATE_COMMAND_BEGIN_BORDER) == true) { lastStartBorder = currentChild firstStartBorder = firstStartBorder ?: currentChild @@ -69,17 +74,17 @@ class DirectiveBasedHtmlTemplateProcessingStrategy(private val context: DokkaCon } else if (lastStartBorder != null && currentChild.data?.startsWith(TEMPLATE_COMMAND_END_BORDER) == true) { lastStartBorder.remove() val cmd: Command? = - lastStartBorder.data?.removePrefix(TEMPLATE_COMMAND_BEGIN_BORDER)?.let { parseJson(it) } - cmd?.let { handler(nodes, it) } + lastStartBorder.data?.removePrefix("$TEMPLATE_COMMAND_BEGIN_BORDER$TEMPLATE_COMMAND_SEPARATOR")?.let { parseJson(it) } + cmd?.let { handler(it, nodes) } currentChild.remove() extractCommandsFromComments(node, firstStartBorder?.siblingIndex() ?: 0, handler) return } else { - lastStartBorder?.let { nodes.add(currentChild) } + if (lastStartBorder != null) nodes.add(currentChild) } else -> { extractCommandsFromComments(currentChild, handler = handler) - lastStartBorder?.let { nodes.add(currentChild) } + if (lastStartBorder != null) nodes.add(currentChild) } } } diff --git a/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt b/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt index faceef4f51..58cd76948f 100644 --- a/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt +++ b/plugins/templating/src/main/kotlin/templates/SubstitutionCommandHandler.kt @@ -11,7 +11,7 @@ import org.jsoup.nodes.Node import org.jsoup.nodes.TextNode import java.io.File -class SubstitutionCommandHandler(context: DokkaContext) : CommandHandler, CommentCommandHandler { +class SubstitutionCommandHandler(context: DokkaContext) : CommandHandler { override fun handleCommand(element: Element, command: Command, input: File, output: File) { command as SubstitutionCommand @@ -25,9 +25,9 @@ class SubstitutionCommandHandler(context: DokkaContext) : CommandHandler, Commen parent?.insertChildren(position, childrenCopy) } - override fun handleCommand(nodes: List, command: Command, input: File, output: File) { + override fun handleCommandAsComment(command: Command, body: List, input: File, output: File) { command as SubstitutionCommand - substitute(nodes, TemplatingContext(input, output, nodes, command)) + substitute(body, TemplatingContext(input, output, body, command)) } override fun canHandle(command: Command): Boolean = command is SubstitutionCommand diff --git a/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt index 9ea9139955..01c1006722 100644 --- a/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt +++ b/plugins/templating/src/main/kotlin/templates/TemplateProcessor.kt @@ -88,7 +88,7 @@ class DefaultMultiModuleTemplateProcessor( data class TemplatingContext( val input: File, val output: File, - val nodes: List, + val body: List, val command: T, ) diff --git a/plugins/versioning/api/versioning.api b/plugins/versioning/api/versioning.api index 15ebe7bea7..f62670582b 100644 --- a/plugins/versioning/api/versioning.api +++ b/plugins/versioning/api/versioning.api @@ -59,6 +59,7 @@ public final class org/jetbrains/dokka/versioning/ReplaceVersionCommandHandler : public fun finish (Ljava/io/File;)V public final fun getVersionsNavigationCreator ()Lorg/jetbrains/dokka/versioning/VersionsNavigationCreator; public fun handleCommand (Lorg/jsoup/nodes/Element;Lorg/jetbrains/dokka/base/templating/Command;Ljava/io/File;Ljava/io/File;)V + public fun handleCommandAsComment (Lorg/jetbrains/dokka/base/templating/Command;Ljava/util/List;Ljava/io/File;Ljava/io/File;)V } public final class org/jetbrains/dokka/versioning/SemVerVersionOrdering : org/jetbrains/dokka/versioning/VersionsOrdering {