diff options
author | Marcin Aman <marcin.aman@gmail.com> | 2021-02-05 15:52:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-05 15:52:05 +0100 |
commit | a78e47a8d3d0dae7b68b0e414967e20ffb6e9a18 (patch) | |
tree | 12bb2bd7659e7ad3ab6fc8cc62acb0a21524d00c | |
parent | 70000c87a37caa2a6b518a555f53c98514434403 (diff) | |
download | dokka-a78e47a8d3d0dae7b68b0e414967e20ffb6e9a18.tar.gz dokka-a78e47a8d3d0dae7b68b0e414967e20ffb6e9a18.tar.bz2 dokka-a78e47a8d3d0dae7b68b0e414967e20ffb6e9a18.zip |
Deduplicate resources in multimodule (#1711)
5 files changed, 52 insertions, 38 deletions
diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index 938c7701..4da2cb6e 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -193,15 +193,15 @@ class DokkaBase : DokkaPlugin() { } val scriptsInstaller by extending { - htmlPreprocessors with ScriptsInstaller order { after(rootCreator) } + htmlPreprocessors providing ::ScriptsInstaller order { after(rootCreator) } } val stylesInstaller by extending { - htmlPreprocessors with StylesInstaller order { after(rootCreator) } + htmlPreprocessors providing ::StylesInstaller order { after(rootCreator) } } val assetsInstaller by extending { - htmlPreprocessors with AssetsInstaller order { after(rootCreator) } + htmlPreprocessors with AssetsInstaller order { after(rootCreator) } applyIf { !delayTemplateSubstitution } } val customResourceInstaller by extending { @@ -209,7 +209,7 @@ class DokkaBase : DokkaPlugin() { after(stylesInstaller) after(scriptsInstaller) after(assetsInstaller) - } + } applyIf { !delayTemplateSubstitution } } val packageListCreator by extending { @@ -219,7 +219,7 @@ class DokkaBase : DokkaPlugin() { } val sourcesetDependencyAppender by extending { - htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) } + htmlPreprocessors providing ::SourcesetDependencyAppender order { after(rootCreator) } applyIf { !delayTemplateSubstitution } } val resolveLinkConsumer by extending { diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 6eb68280..2a0af90c 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -731,7 +731,8 @@ open class HtmlRenderer( } } - private fun resolveLink(link: String, page: PageNode): String = if (URI(link).isAbsolute) link else page.root(link) + private val String.isAbsolute: Boolean + get() = URI(this).isAbsolute open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit): String { val pathToRoot = locationProvider.pathToRoot(page) @@ -739,23 +740,43 @@ open class HtmlRenderer( head { meta(name = "viewport", content = "width=device-width, initial-scale=1", charset = "UTF-8") title(page.name) - link(href = page.root("images/logo-icon.svg"), rel = "icon", type = "image/svg") + templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { + link(href = page.root("###images/logo-icon.svg"), rel = "icon", type = "image/svg") + } templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { script { unsafe { +"""var pathToRoot = "###";""" } } } resources.forEach { when { - it.substringBefore('?').substringAfterLast('.') == "css" -> link( - rel = LinkRel.stylesheet, - href = resolveLink(it, page) - ) - it.substringBefore('?').substringAfterLast('.') == "js" -> script( - type = ScriptType.textJavaScript, - src = resolveLink(it, page) - ) { - async = true + it.substringBefore('?').substringAfterLast('.') == "css" -> + if (it.isAbsolute) link( + rel = LinkRel.stylesheet, + href = it + ) + else templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { + link( + rel = LinkRel.stylesheet, + href = "###$it" + ) + } + it.substringBefore('?').substringAfterLast('.') == "js" -> + if (it.isAbsolute) script( + type = ScriptType.textJavaScript, + src = it + ) { + async = true + } else templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { + script( + type = ScriptType.textJavaScript, + src = "###$it" + ) { + async = true + } + } + it.isImage() -> if (it.isAbsolute) link(href = it) + else templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { + link(href = "###$it") } - it.isImage() -> link(href = page.root(it)) else -> unsafe { +it } } } @@ -779,7 +800,9 @@ open class HtmlRenderer( id = "leftToggler" span("icon-toggler") } - script(type = ScriptType.textJavaScript, src = page.root("scripts/main.js")) {} + templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { + script(type = ScriptType.textJavaScript, src = "###scripts/main.js") {} + } content() div(classes = "footer") { span("go-to-top-icon") { diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index 25d95a8f..c5a92009 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -104,7 +104,7 @@ class CustomResourceInstaller(val dokkaContext: DokkaContext) : PageTransformer } } -object ScriptsInstaller : PageTransformer { +class ScriptsInstaller(private val dokkaContext: DokkaContext) : PageTransformer { private val scriptsPages = listOf( "scripts/clipboard.js", "scripts/navigation-loader.js", @@ -112,18 +112,18 @@ object ScriptsInstaller : PageTransformer { "scripts/main.js", ) - override fun invoke(input: RootPageNode): RootPageNode { - return input.modified( - children = input.children + scriptsPages.toRenderSpecificResourcePage() - ).transformContentPagesTree { + override fun invoke(input: RootPageNode): RootPageNode = + input.let { root -> + if (dokkaContext.configuration.delayTemplateSubstitution) root + else root.modified(children = input.children + scriptsPages.toRenderSpecificResourcePage()) + }.transformContentPagesTree { it.modified( embeddedResources = it.embeddedResources + scriptsPages ) } - } } -object StylesInstaller : PageTransformer { +class StylesInstaller(private val dokkaContext: DokkaContext) : PageTransformer { private val stylesPages = listOf( "styles/style.css", "styles/logo-styles.css", @@ -132,9 +132,10 @@ object StylesInstaller : PageTransformer { ) override fun invoke(input: RootPageNode): RootPageNode = - input.modified( - children = input.children + stylesPages.toRenderSpecificResourcePage() - ).transformContentPagesTree { + input.let { root -> + if (dokkaContext.configuration.delayTemplateSubstitution) root + else root.modified(children = input.children + stylesPages.toRenderSpecificResourcePage()) + }.transformContentPagesTree { it.modified( embeddedResources = it.embeddedResources + stylesPages ) diff --git a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt index 5a97d216..dec37799 100644 --- a/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt +++ b/plugins/templating/src/main/kotlin/templates/JsonElementBasedTemplateProcessingStrategy.kt @@ -39,10 +39,6 @@ abstract class BaseJsonNavigationTemplateProcessingStrategy(val context: DokkaCo }) output.resolve(path).mkdirs() output.resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content) - - fragments.keys.forEach { - output.resolve(it).resolve("$path/$navigationFileNameWithoutExtension.json").writeText(content) - } } } diff --git a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt index 049cef96..bb4fb1c4 100644 --- a/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt +++ b/plugins/templating/src/test/kotlin/templates/AddToSearchCommandResolutionTest.kt @@ -63,12 +63,6 @@ class AddToSearchCommandResolutionTest : TemplatingAbstractTest() { val output = parseJson<List<SearchRecord>>(outputDir.resolve("scripts/${fileName}").readText()) assertEquals(expected, output.sortedBy { it.location }) - - val outputFromModule1 = parseJson<List<SearchRecord>>(module1Navigation.readText()) - assertEquals(expected, outputFromModule1.sortedBy { it.location }) - - val outputFromModule2 = parseJson<List<SearchRecord>>(module2Navigation.readText()) - assertEquals(expected, outputFromModule2.sortedBy { it.location }) } } } |