diff options
Diffstat (limited to 'dokka-subprojects/plugin-gfm-template-processing/src')
3 files changed, 121 insertions, 0 deletions
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<AllModulesPagePlugin>() } + private val templateProcessingPlugin by lazy { plugin<TemplatingPlugin>() } + private val gfmPlugin by lazy { plugin<GfmPlugin>() } + private val dokkaBase by lazy { plugin<DokkaBase>()} + + public val gfmTemplateProcessingStrategy: Extension<TemplateProcessingStrategy, *, *> by extending { + (templateProcessingPlugin.templateProcessingStrategy + providing ::GfmTemplateProcessingStrategy + order { before(templateProcessingPlugin.fallbackProcessingStrategy) }) + } + + public val gfmLocationProvider: Extension<LocationProviderFactory, *, *> by extending { + dokkaBase.locationProviderFactory providing MultimoduleLocationProvider::Factory override listOf(gfmPlugin.locationProvider, allModulesPagePlugin.multimoduleLocationProvider) + } + + public val gfmPartialLocationProvider: Extension<LocationProviderFactory, *, *> 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<AllModulesPagePlugin>().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<GfmCommand>(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 |