From 3cb4702a68139788de6e1f7b087ced345f2b71ba Mon Sep 17 00:00:00 2001 From: Błażej Kardyś Date: Fri, 20 Nov 2020 17:23:10 +0100 Subject: Changing how multimodule location provider works and improving gfm link substitution --- .../GfmTemplateProcessingPlugin.kt | 31 +++++++++++++ .../GfmTemplateProcessingStrategy.kt | 51 ++++++++++++++++++++++ .../org.jetbrains.dokka.plugability.DokkaPlugin | 1 + 3 files changed, 83 insertions(+) create mode 100644 plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt create mode 100644 plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt create mode 100644 plugins/gfm/gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin (limited to 'plugins/gfm/gfm-template-processing/src/main') diff --git a/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt new file mode 100644 index 00000000..7df740a5 --- /dev/null +++ b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingPlugin.kt @@ -0,0 +1,31 @@ +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.gfm.GfmPlugin +import org.jetbrains.dokka.gfm.location.MarkdownLocationProvider +import org.jetbrains.dokka.plugability.DokkaPlugin + +class GfmTemplateProcessingPlugin : DokkaPlugin() { + + private val allModulesPagePlugin by lazy { plugin() } + + private val gfmPlugin by lazy { plugin() } + + private val dokkaBase by lazy { plugin()} + + val gfmTemplateProcessingStrategy by extending { + (allModulesPagePlugin.templateProcessingStrategy + providing ::GfmTemplateProcessingStrategy + order { before(allModulesPagePlugin.fallbackProcessingStrategy) }) + } + + val gfmLocationProvider by extending { + dokkaBase.locationProviderFactory providing MultimoduleLocationProvider::Factory override listOf(gfmPlugin.locationProvider, allModulesPagePlugin.multimoduleLocationProvider) + } + + val gfmPartialLocationProvider by extending { + allModulesPagePlugin.partialLocationProviderFactory providing MarkdownLocationProvider::Factory override allModulesPagePlugin.baseLocationProviderFactory + } +} \ No newline at end of file diff --git a/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt new file mode 100644 index 00000000..b2ef4d06 --- /dev/null +++ b/plugins/gfm/gfm-template-processing/src/main/kotlin/org/jetbrains/dokka/gfm/templateProcessing/GfmTemplateProcessingStrategy.kt @@ -0,0 +1,51 @@ +package org.jetbrains.dokka.gfm.templateProcessing + +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import org.jetbrains.dokka.allModulesPage.AllModulesPagePlugin +import org.jetbrains.dokka.allModulesPage.templates.TemplateProcessingStrategy +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 java.io.File + +class GfmTemplateProcessingStrategy(context: DokkaContext) : TemplateProcessingStrategy { + + private val externalModuleLinkResolver = context.plugin().querySingle { externalModuleLinkResolver } + + override suspend fun process(input: File, output: File): Boolean = coroutineScope { + if (input.extension == "md") { + launch(IO) { + input.bufferedReader().use { reader -> + output.bufferedWriter().use { writer -> + do { + val line = reader.readLine() + if (line != null) { + writer.write(line.replace(templateCommandRegex) { + when (val command = parseJson(it.command)) { + is ResolveLinkGfmCommand -> resolveLink(output, command.dri, it.label) + } + }) + writer.newLine() + } + } while (line != null) + } + } + } + true + } else false + } + + private fun resolveLink(fileContext: File, dri: DRI, label: String): String = + externalModuleLinkResolver.resolve(dri, fileContext)?.let { address -> + "[$label]($address)" + } ?: label +} \ No newline at end of file diff --git a/plugins/gfm/gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin b/plugins/gfm/gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin new file mode 100644 index 00000000..197823e7 --- /dev/null +++ b/plugins/gfm/gfm-template-processing/src/main/resources/META-INF/services/org.jetbrains.dokka.plugability.DokkaPlugin @@ -0,0 +1 @@ +org.jetbrains.dokka.gfm.templateProcessing.GfmTemplateProcessingPlugin \ No newline at end of file -- cgit