Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Feb 21, 2022
1 parent 12256cf commit dc93c71
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 51 deletions.
1 change: 1 addition & 0 deletions plugins/all-modules-page/api/all-modules-page.api
Expand Up @@ -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
}

1 change: 1 addition & 0 deletions plugins/base/api/base.api
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions plugins/base/src/main/kotlin/renderers/html/Tags.kt
Expand Up @@ -26,13 +26,14 @@ inline fun FlowOrPhrasingContent.strike(classes : String? = null, crossinline bl
open class STRIKE(initialAttributes: Map<String, String>, 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)
}
Expand Down
Expand Up @@ -19,7 +19,7 @@ class ReplaceVersionsConsumer(private val context: DokkaContext) : ImmediateHtml
}

override fun <R> processCommandAndFinalize(command: Command, block: TemplateBlock, tagConsumer: ImmediateResolutionTagConsumer<R>): R {
PathToRootConsumer.processCommand(command, block, tagConsumer)
processCommand(command, block, tagConsumer)
return tagConsumer.finalize()
}
}
24 changes: 9 additions & 15 deletions plugins/templating/api/templating.api
Expand Up @@ -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 {
Expand All @@ -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 <init> (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
}

Expand All @@ -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 <init> (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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions plugins/templating/src/main/kotlin/templates/CommandHandler.kt
Expand Up @@ -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<Node>, command: Command, input: File, output: File)
interface CommandHandler {
fun handleCommand(element: Element, command: Command, input: File, output: File)
fun handleCommandAsComment(command: Command, body: List<Node>, input: File, output: File) { }
fun canHandle(command: Command): Boolean
fun finish(output: File) {}
}
Expand Up @@ -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
Expand All @@ -27,59 +28,63 @@ 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<Node>, command: Command, input: File, output: File) {
val handlers = directiveBasedCommandHandlers.filterIsInstance<CommentCommandHandler>().filter { it.canHandle(command) }
fun handleCommandAsComment(command: Command, body: List<Node>, 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<Node>, Command) -> Unit) {
private fun extractCommandsFromComments(
node: Node,
startFrom: Int = 0,
handler: (command: Command, body: List<Node>) -> Unit
) {
val nodes: MutableList<Node> = 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
nodes.clear()
} 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)
}
}
}
Expand Down
Expand Up @@ -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
Expand All @@ -25,9 +25,9 @@ class SubstitutionCommandHandler(context: DokkaContext) : CommandHandler, Commen
parent?.insertChildren(position, childrenCopy)
}

override fun handleCommand(nodes: List<Node>, command: Command, input: File, output: File) {
override fun handleCommandAsComment(command: Command, body: List<Node>, 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
Expand Down
Expand Up @@ -88,7 +88,7 @@ class DefaultMultiModuleTemplateProcessor(
data class TemplatingContext<out T : Command>(
val input: File,
val output: File,
val nodes: List<Node>,
val body: List<Node>,
val command: T,
)

Expand Down
1 change: 1 addition & 0 deletions plugins/versioning/api/versioning.api
Expand Up @@ -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 {
Expand Down

0 comments on commit dc93c71

Please sign in to comment.