diff options
Diffstat (limited to 'plugins/gfm')
7 files changed, 93 insertions, 28 deletions
diff --git a/plugins/gfm/build.gradle.kts b/plugins/gfm/build.gradle.kts index 33917cdf..6400dba1 100644 --- a/plugins/gfm/build.gradle.kts +++ b/plugins/gfm/build.gradle.kts @@ -4,6 +4,7 @@ dependencies { implementation(project(":plugins:base")) testImplementation(project(":plugins:base")) testImplementation(project(":plugins:base:base-test-utils")) + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.1") } registerDokkaArtifactPublication("gfmPlugin") { diff --git a/plugins/gfm/gfm-all-module-page/build.gradle.kts b/plugins/gfm/gfm-all-module-page/build.gradle.kts index 66deb291..85f83587 100644 --- a/plugins/gfm/gfm-all-module-page/build.gradle.kts +++ b/plugins/gfm/gfm-all-module-page/build.gradle.kts @@ -4,6 +4,9 @@ dependencies { implementation(project(":plugins:base")) implementation(project(":plugins:gfm")) implementation(project(":plugins:all-module-page")) + + val coroutines_version: String by project + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version") } registerDokkaArtifactPublication("dokkaGfmAllModulePage") { diff --git a/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmAllModulesPagaPlugin.kt b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmAllModulesPagaPlugin.kt index 8052a762..eb5e7e45 100644 --- a/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmAllModulesPagaPlugin.kt +++ b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmAllModulesPagaPlugin.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.gfm.allModulesPage import org.jetbrains.dokka.allModulesPage.AllModulesPagePlugin +import org.jetbrains.dokka.allModulesPage.templates.DefaultTemplateProcessor import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.gfm.GfmPlugin import org.jetbrains.dokka.plugability.DokkaPlugin @@ -17,4 +18,11 @@ class GfmAllModulesPagePlugin : DokkaPlugin() { (dokkaBase.locationProviderFactory providing GfmMultimoduleLocationProvider::Factory override listOf(allModulesPagePlugin.multimoduleLocationProvider, gfmPlugin.locationProvider)) } + + val defaultTemplateProcessor by extending { + (allModulesPagePlugin.templateProcessingStrategy + providing ::GfmTemplateProcessingStrategy + override allModulesPagePlugin.directiveBasedHtmlTemplateProcessingStrategy + order { before(allModulesPagePlugin.fallbackProcessingStrategy) }) + } }
\ No newline at end of file diff --git a/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmMultimoduleLocationProvider.kt b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmMultimoduleLocationProvider.kt index d49be426..9f2ee140 100644 --- a/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmMultimoduleLocationProvider.kt +++ b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmMultimoduleLocationProvider.kt @@ -1,38 +1,14 @@ package org.jetbrains.dokka.gfm.allModulesPage -import org.jetbrains.dokka.base.resolvers.local.DokkaBaseLocationProvider -import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider +import org.jetbrains.dokka.allModulesPage.MultimoduleLocationProvider import org.jetbrains.dokka.base.resolvers.local.LocationProviderFactory import org.jetbrains.dokka.gfm.location.MarkdownLocationProvider -import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.model.DisplaySourceSet -import org.jetbrains.dokka.pages.PageNode import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaContext -class GfmMultimoduleLocationProvider(private val root: RootPageNode, context: DokkaContext) : DokkaBaseLocationProvider(root, context, ".md") { +class GfmMultimoduleLocationProvider(root: RootPageNode, context: DokkaContext) : MultimoduleLocationProvider(root, context, ".md") { - private val defaultLocationProvider = MarkdownLocationProvider(root, context) - - val paths = context.configuration.modules.map { - it.name to it.relativePathToOutputDirectory - }.toMap() - - override fun resolve(dri: DRI, sourceSets: Set<DisplaySourceSet>, context: PageNode?) = - dri.takeIf { it.packageName == MULTIMODULE_PACKAGE_PLACEHOLDER }?.classNames?.let { paths[it] }?.let { - "$it/${DokkaLocationProvider.identifierToFilename(dri.classNames.orEmpty())}/index.md" - } ?: defaultLocationProvider.resolve(dri, sourceSets, context) - - override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean) = - defaultLocationProvider.resolve(node, context, skipExtension) - - override fun pathToRoot(from: PageNode): String = defaultLocationProvider.pathToRoot(from) - - override fun ancestors(node: PageNode): List<PageNode> = listOf(root) - - companion object { - const val MULTIMODULE_PACKAGE_PLACEHOLDER = ".ext" - } + override val defaultLocationProvider = MarkdownLocationProvider(root, context) class Factory(private val context: DokkaContext): LocationProviderFactory { override fun getLocationProvider(pageNode: RootPageNode) = diff --git a/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmTemplateProcessingStrategy.kt b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmTemplateProcessingStrategy.kt new file mode 100644 index 00000000..37226d1e --- /dev/null +++ b/plugins/gfm/gfm-all-module-page/src/main/kotlin/org/jetbrains/dokka/gfm/allModulesPage/GfmTemplateProcessingStrategy.kt @@ -0,0 +1,45 @@ +package org.jetbrains.dokka.gfm.allModulesPage + +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch +import org.jetbrains.dokka.allModulesPage.templates.ExternalModuleLinkResolver +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.templateCommandRegex +import org.jetbrains.dokka.gfm.ResolveLinkGfmCommand +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.plugability.DokkaContext +import java.io.File + +class GfmTemplateProcessingStrategy(val context: DokkaContext) : TemplateProcessingStrategy { + + private val externalModuleLinkResolver = ExternalModuleLinkResolver(context) + + override suspend fun process(input: File, output: File): Boolean = coroutineScope { + if (input.extension == "md") { + launch(IO) { + val reader = input.bufferedReader() + val writer = output.bufferedWriter() + do { + val line = reader.readLine() + if (line != null) { + writer.write(line.replace(templateCommandRegex) { + when (val command = parseJson<GfmCommand>(it.groupValues.last())) { + is ResolveLinkGfmCommand -> resolveLink(output, command.dri) + } + }) + writer.newLine() + } + } while (line != null) + reader.close() + writer.close() + } + true + } else false + } + + private fun resolveLink(fileContext: File, dri: DRI): String = + externalModuleLinkResolver.resolve(dri, fileContext) ?: "" +}
\ No newline at end of file diff --git a/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/gfmTemplating.kt b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/gfmTemplating.kt new file mode 100644 index 00000000..1473ceee --- /dev/null +++ b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/gfmTemplating.kt @@ -0,0 +1,19 @@ +package org.jetbrains.dokka.gfm + +import org.jetbrains.dokka.base.templating.toJsonString +import org.jetbrains.dokka.links.DRI +import com.fasterxml.jackson.annotation.JsonTypeInfo +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.CLASS + +@JsonTypeInfo(use = CLASS) +sealed class GfmCommand { + companion object { + private const val delimiter = "\u1680" + fun templateCommand(command: GfmCommand): String = "$delimiter GfmCommand ${toJsonString(command)}$delimiter" + val templateCommandRegex: Regex = Regex("$delimiter GfmCommand ([^$delimiter ]*)$delimiter") + } +} + +class ResolveLinkGfmCommand(val dri: DRI): GfmCommand() + + diff --git a/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt index 0655ce20..25b24f0c 100644 --- a/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt +++ b/plugins/gfm/src/main/kotlin/org/jetbrains/dokka/gfm/renderer/CommonmarkRenderer.kt @@ -3,7 +3,9 @@ package org.jetbrains.dokka.gfm.renderer import org.jetbrains.dokka.DokkaException import org.jetbrains.dokka.base.renderers.DefaultRenderer import org.jetbrains.dokka.base.renderers.isImage +import org.jetbrains.dokka.gfm.GfmCommand.Companion.templateCommand import org.jetbrains.dokka.gfm.GfmPlugin +import org.jetbrains.dokka.gfm.ResolveLinkGfmCommand import org.jetbrains.dokka.model.DisplaySourceSet import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext @@ -80,6 +82,17 @@ open class CommonmarkRenderer( } } + override fun StringBuilder.buildDRILink( + node: ContentDRILink, + pageContext: ContentPage, + sourceSetRestriction: Set<DisplaySourceSet>? + ) { + val address = locationProvider.resolve(node.address, node.sourceSets, pageContext) + buildLink(address ?: templateCommand(ResolveLinkGfmCommand(node.address))) { + buildText(node.children, pageContext, sourceSetRestriction) + } + } + override fun StringBuilder.buildNewLine() { append(" \n") } @@ -118,7 +131,7 @@ open class CommonmarkRenderer( } override fun StringBuilder.buildResource(node: ContentEmbeddedResource, pageContext: ContentPage) { - if(node.isImage()){ + if (node.isImage()) { append("!") } append("[${node.altText}](${node.address})") |