From 8e5c63d035ef44a269b8c43430f43f5c8eebfb63 Mon Sep 17 00:00:00 2001 From: Ignat Beresnev Date: Fri, 10 Nov 2023 11:46:54 +0100 Subject: Restructure the project to utilize included builds (#3174) * Refactor and simplify artifact publishing * Update Gradle to 8.4 * Refactor and simplify convention plugins and build scripts Fixes #3132 --------- Co-authored-by: Adam <897017+aSemy@users.noreply.github.com> Co-authored-by: Oleg Yukhnevich --- .../GfmTemplateProcessingPlugin.kt | 44 +++++++++++++ .../GfmTemplateProcessingStrategy.kt | 72 ++++++++++++++++++++++ .../org.jetbrains.dokka.plugability.DokkaPlugin | 5 ++ 3 files changed, 121 insertions(+) create mode 100644 dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt create mode 100644 dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt create mode 100644 dokka-subprojects/plugin-gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin (limited to 'dokka-subprojects/plugin-gfm-template-processing/src') diff --git a/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt b/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt new file mode 100644 index 00000000..fd2af274 --- /dev/null +++ b/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package org.jetbrains.dokka.gfm.templateProcessing + +import org.jetbrains.dokka.allModulesPage.AllModulesPagePlugin +import org.jetbrains.dokka.allModulesPage.MultimoduleLocationProvider +import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.base.resolvers.local.LocationProviderFactory +import org.jetbrains.dokka.gfm.GfmPlugin +import org.jetbrains.dokka.gfm.location.MarkdownLocationProvider +import org.jetbrains.dokka.plugability.DokkaPlugin +import org.jetbrains.dokka.plugability.DokkaPluginApiPreview +import org.jetbrains.dokka.plugability.Extension +import org.jetbrains.dokka.plugability.PluginApiPreviewAcknowledgement +import org.jetbrains.dokka.templates.TemplateProcessingStrategy +import org.jetbrains.dokka.templates.TemplatingPlugin + +public class GfmTemplateProcessingPlugin : DokkaPlugin() { + + private val allModulesPagePlugin by lazy { plugin() } + private val templateProcessingPlugin by lazy { plugin() } + private val gfmPlugin by lazy { plugin() } + private val dokkaBase by lazy { plugin()} + + public val gfmTemplateProcessingStrategy: Extension by extending { + (templateProcessingPlugin.templateProcessingStrategy + providing ::GfmTemplateProcessingStrategy + order { before(templateProcessingPlugin.fallbackProcessingStrategy) }) + } + + public val gfmLocationProvider: Extension by extending { + dokkaBase.locationProviderFactory providing MultimoduleLocationProvider::Factory override listOf(gfmPlugin.locationProvider, allModulesPagePlugin.multimoduleLocationProvider) + } + + public val gfmPartialLocationProvider: Extension by extending { + allModulesPagePlugin.partialLocationProviderFactory providing MarkdownLocationProvider::Factory override allModulesPagePlugin.baseLocationProviderFactory + } + + @OptIn(DokkaPluginApiPreview::class) + override fun pluginApiPreviewAcknowledgement(): PluginApiPreviewAcknowledgement = + PluginApiPreviewAcknowledgement +} diff --git a/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt b/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt new file mode 100644 index 00000000..8f23e8e9 --- /dev/null +++ b/dokka-subprojects/plugin-gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt @@ -0,0 +1,72 @@ +/* + * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package org.jetbrains.dokka.gfm.templateProcessing + +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.allModulesPage.AllModulesPagePlugin +import org.jetbrains.dokka.base.templating.parseJson +import org.jetbrains.dokka.gfm.GfmCommand +import org.jetbrains.dokka.gfm.GfmCommand.Companion.command +import org.jetbrains.dokka.gfm.GfmCommand.Companion.label +import org.jetbrains.dokka.gfm.GfmCommand.Companion.templateCommandRegex +import org.jetbrains.dokka.gfm.ResolveLinkGfmCommand +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.plugin +import org.jetbrains.dokka.plugability.querySingle +import org.jetbrains.dokka.templates.TemplateProcessingStrategy +import java.io.BufferedWriter +import java.io.File + +public class GfmTemplateProcessingStrategy( + public val context: DokkaContext +) : TemplateProcessingStrategy { + + private val externalModuleLinkResolver = + context.plugin().querySingle { externalModuleLinkResolver } + + override fun process(input: File, output: File, moduleContext: DokkaConfiguration.DokkaModuleDescription?): Boolean = + if (input.extension == "md") { + input.bufferedReader().use { reader -> + //This should also work whenever we have a misconfigured dokka and output is pointing to the input + //the same way that html processing does + if (input.absolutePath == output.absolutePath) { + context.logger.info("Attempting to process GFM templates in place for directory $input, this suggests miss configuration.") + val lines = reader.readLines() + output.bufferedWriter().use { writer -> + lines.forEach { line -> + writer.processAndWrite(line, output) + } + + } + } else { + output.bufferedWriter().use { writer -> + reader.lineSequence().forEach { line -> + writer.processAndWrite(line, output) + } + } + } + } + true + } else false + + private fun BufferedWriter.processAndWrite(line: String, output: File) = + processLine(line, output).run { + write(this) + newLine() + } + + private fun processLine(line: String, output: File): String = + line.replace(templateCommandRegex) { + when (val command = parseJson(it.command)) { + is ResolveLinkGfmCommand -> resolveLink(output, command.dri, it.label) + } + } + + private fun resolveLink(fileContext: File, dri: DRI, label: String): String = + externalModuleLinkResolver.resolve(dri, fileContext)?.let { address -> + "[$label]($address)" + } ?: label +} diff --git a/dokka-subprojects/plugin-gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin b/dokka-subprojects/plugin-gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin new file mode 100644 index 00000000..375990bb --- /dev/null +++ b/dokka-subprojects/plugin-gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin @@ -0,0 +1,5 @@ +# +# Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. +# + +org.jetbrains.dokka.gfm.templateProcessing.GfmTemplateProcessingPlugin -- cgit