diff options
6 files changed, 152 insertions, 16 deletions
diff --git a/core/test-api/api/test-api.api b/core/test-api/api/test-api.api index ef0f69a5..b1f6a381 100644 --- a/core/test-api/api/test-api.api +++ b/core/test-api/api/test-api.api @@ -152,6 +152,7 @@ public final class testApi/testRunner/TestDokkaConfigurationBuilder { public fun <init> ()V public final fun build ()Lorg/jetbrains/dokka/DokkaConfigurationImpl; public final fun getCacheRoot ()Ljava/lang/String; + public final fun getDelayTemplateSubstitution ()Z public final fun getFailOnWarning ()Z public final fun getFormat ()Ljava/lang/String; public final fun getIncludes ()Ljava/util/List; @@ -165,6 +166,7 @@ public final class testApi/testRunner/TestDokkaConfigurationBuilder { public final fun getSuppressInheritedMembers ()Z public final fun getSuppressObviousFunctions ()Z public final fun setCacheRoot (Ljava/lang/String;)V + public final fun setDelayTemplateSubstitution (Z)V public final fun setFailOnWarning (Z)V public final fun setFormat (Ljava/lang/String;)V public final fun setIncludes (Ljava/util/List;)V diff --git a/core/test-api/src/main/kotlin/testApi/testRunner/TestDokkaConfigurationBuilder.kt b/core/test-api/src/main/kotlin/testApi/testRunner/TestDokkaConfigurationBuilder.kt index fc26ff98..33f45b02 100644 --- a/core/test-api/src/main/kotlin/testApi/testRunner/TestDokkaConfigurationBuilder.kt +++ b/core/test-api/src/main/kotlin/testApi/testRunner/TestDokkaConfigurationBuilder.kt @@ -37,6 +37,7 @@ class TestDokkaConfigurationBuilder { var suppressObviousFunctions: Boolean = DokkaDefaults.suppressObviousFunctions var includes: List<File> = emptyList() var suppressInheritedMembers: Boolean = DokkaDefaults.suppressInheritedMembers + var delayTemplateSubstitution: Boolean = DokkaDefaults.delayTemplateSubstitution private val lazySourceSets = mutableListOf<Lazy<DokkaSourceSetImpl>>() fun build() = DokkaConfigurationImpl( @@ -53,6 +54,7 @@ class TestDokkaConfigurationBuilder { suppressObviousFunctions = suppressObviousFunctions, includes = includes.toSet(), suppressInheritedMembers = suppressInheritedMembers, + delayTemplateSubstitution = delayTemplateSubstitution ) fun sourceSets(block: SourceSetsBuilder.() -> Unit) { diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index fc138461..03fbf64e 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -227,7 +227,7 @@ class DokkaBase : DokkaPlugin() { after(stylesInstaller) after(scriptsInstaller) after(assetsInstaller) - } applyIf { !delayTemplateSubstitution } + } } val packageListCreator by extending { diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index 665ae730..43526dc3 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -76,6 +76,8 @@ class CustomResourceInstaller(val dokkaContext: DokkaContext) : PageTransformer val customResourcesPaths = (customAssets + customStylesheets).map { it.name }.toSet() val withEmbeddedResources = input.transformContentPagesTree { it.modified(embeddedResources = it.embeddedResources + customResourcesPaths) } + if(dokkaContext.configuration.delayTemplateSubstitution) + return withEmbeddedResources val (currentResources, otherPages) = withEmbeddedResources.children.partition { it is RendererSpecificResourcePage } return input.modified(children = otherPages + currentResources.filterNot { it.name in customResourcesPaths } + customAssets + customStylesheets) } diff --git a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt index 69a851ed..c4c46331 100644 --- a/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt +++ b/plugins/base/src/main/kotlin/resolvers/local/DokkaLocationProvider.kt @@ -140,7 +140,7 @@ open class DokkaLocationProvider( private fun PageNode.parent() = pageGraphRoot.parentMap[this] private val PageNode.pathName: String - get() = if (this is PackagePageNode) name else identifierToFilename(name) + get() = if (this is PackagePageNode || this is RendererSpecificResourcePage) name else identifierToFilename(name) protected data class DRIWithSourceSets(val dri: DRI, val sourceSet: Set<DisplaySourceSet>) diff --git a/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt index 57345f5c..9b010b56 100644 --- a/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt +++ b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt @@ -1,28 +1,38 @@ package resourceLinks +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.PluginConfigurationImpl import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.base.DokkaBaseConfiguration +import org.jetbrains.dokka.base.templating.toJsonString import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest import org.jetbrains.dokka.transformers.pages.PageTransformer import org.jsoup.Jsoup import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource import utils.TestOutputWriterPlugin +import java.io.File +import kotlin.test.assertNotNull +import kotlin.test.assertNull class ResourceLinksTest : BaseAbstractTest() { class TestResourcesAppenderPlugin(val resources: List<String>) : DokkaPlugin() { class TestResourcesAppender(val resources: List<String>) : PageTransformer { override fun invoke(input: RootPageNode) = input.transformContentPagesTree { - it.modified( - embeddedResources = it.embeddedResources + resources - ) - } + it.modified( + embeddedResources = it.embeddedResources + resources + ) + } } val appender by extending { plugin<DokkaBase>().htmlPreprocessors with TestResourcesAppender(resources) } } + @Test fun resourceLinksTest() { val configuration = dokkaConfiguration { @@ -52,20 +62,140 @@ class ResourceLinksTest : BaseAbstractTest() { configuration, pluginOverrides = listOf(TestResourcesAppenderPlugin(absoluteResources + relativeResources), writerPlugin) ) { - renderingStage = { - root, context -> Jsoup - .parse(writerPlugin.writer.contents["root/example.html"]) - .head() - .select("link, script") - .let { - absoluteResources.forEach { - r -> assert(it.`is`("[href=$r], [src=$r]")) + renderingStage = { root, context -> + Jsoup + .parse(writerPlugin.writer.contents["root/example.html"]) + .head() + .select("link, script") + .let { + absoluteResources.forEach { r -> + assert(it.`is`("[href=$r], [src=$r]")) + } + relativeResources.forEach { r -> + assert(it.`is`("[href=../$r] , [src=../$r]")) + } + } + } + } + } + + @ParameterizedTest + @ValueSource(booleans = [true, false]) + fun resourceCustomPreprocessorTest(isMultiModule: Boolean) { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/main/kotlin/test/Test.kt") + } + } + delayTemplateSubstitution = isMultiModule + pluginsConfigurations = mutableListOf( + PluginConfigurationImpl( + DokkaBase::class.java.canonicalName, + DokkaConfiguration.SerializationFormat.JSON, + toJsonString( + DokkaBaseConfiguration( + customStyleSheets = listOf(File("test/customStyle.css")), + customAssets = listOf(File("test/customImage.svg")) + ) + ) + ) + ) + } + val source = + """ + |/src/main/kotlin/test/Test.kt + |package example + """.trimIndent() + val writerPlugin = TestOutputWriterPlugin() + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { root, context -> + run { + if (isMultiModule) { + assertNull(writerPlugin.writer.contents["images/customImage.svg"]) + assertNull(writerPlugin.writer.contents["styles/customStyle.css"]) + } else { + assertNotNull(writerPlugin.writer.contents["images/customImage.svg"]) + assertNotNull(writerPlugin.writer.contents["styles/customStyle.css"]) } - relativeResources.forEach { - r -> assert(it.`is`("[href=../$r] , [src=../$r]")) + if (isMultiModule) { + Jsoup + .parse(writerPlugin.writer.contents["example.html"]) + .body() + .select("link, script") + .let { + listOf("styles/customStyle.css").forEach { r -> + assert(it.`is`("[href=###$r]")) + } + } + } else { + Jsoup + .parse(writerPlugin.writer.contents["root/example.html"]) + .head() + .select("link, script") + .let { + listOf("styles/customStyle.css").forEach { r -> + assert(it.`is`("[href=../$r], [src=../$r]")) + } + } } } } } } + + @Test + fun resourceMultiModuleLinksTest() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/main/kotlin/test/Test.kt") + } + } + delayTemplateSubstitution = false + } + val absoluteResources = listOf( + "https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css", + "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" + ) + val relativeResources = listOf( + "test/relativePath.js", + "test/relativePath.css" + ) + + val source = + """ + |/src/main/kotlin/test/Test.kt + |package example + """.trimIndent() + val writerPlugin = TestOutputWriterPlugin() + testInline( + source, + configuration, + pluginOverrides = listOf(TestResourcesAppenderPlugin(absoluteResources + relativeResources), writerPlugin) + ) { + renderingStage = { root, context -> + run { + assertNull(writerPlugin.writer.contents["scripts/relativePath.js"]) + assertNull(writerPlugin.writer.contents["styles/relativePath.js"]) + Jsoup + .parse(writerPlugin.writer.contents["root/example.html"]) + .head() + .select("link, script") + .let { + absoluteResources.forEach { r -> + assert(it.`is`("[href=$r], [src=$r]")) + } + relativeResources.forEach { r -> + assert(it.`is`("[href=../$r] , [src=../$r]")) + } + } + } + } + } + } } |