diff options
author | Marcin Aman <marcin.aman@gmail.com> | 2020-12-17 10:17:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 10:17:45 +0100 |
commit | 2f7ee2b82cda39f6bd94c5200b83563418b68dd7 (patch) | |
tree | 0942f00012ee7a90208c5d80ed3dd5ec6a3d9f92 /plugins/base/src/main/kotlin | |
parent | 9e344b2047f72051bed509fb4e7ac1ae53f8098e (diff) | |
download | dokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.tar.gz dokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.tar.bz2 dokka-2f7ee2b82cda39f6bd94c5200b83563418b68dd7.zip |
Navigate to root after logo click, add data to searchbars on multimodule (#1631)
Diffstat (limited to 'plugins/base/src/main/kotlin')
6 files changed, 79 insertions, 30 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 7287c08a..2a24a959 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -719,13 +719,14 @@ open class HtmlRenderer( private fun resolveLink(link: String, page: PageNode): String = if (URI(link).isAbsolute) link else page.root(link) - open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit) = - createHTML().prepareForTemplates().html { + open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit): String { + val pathToRoot = locationProvider.pathToRoot(page) + return createHTML().prepareForTemplates().html { 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 = locationProvider.pathToRoot(page))) { + templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { script { unsafe { +"""var pathToRoot = "###";""" } } } resources.forEach { @@ -750,13 +751,9 @@ open class HtmlRenderer( id = "container" div { id = "leftColumn" + clickableLogo(page, pathToRoot) div { - id = "logo" - } - if (page !is MultimoduleRootPage) { - div { - id = "paneSearch" - } + id = "paneSearch" } div { id = "sideMenu" @@ -768,7 +765,6 @@ open class HtmlRenderer( id = "leftToggler" span("icon-toggler") } - script(type = ScriptType.textJavaScript, src = page.root("scripts/pages.js")) {} script(type = ScriptType.textJavaScript, src = page.root("scripts/main.js")) {} content() div(classes = "footer") { @@ -788,6 +784,33 @@ open class HtmlRenderer( } } } + } + + /** + * This is deliberately left open for plugins that have some other pages above ours and would like to link to them + * instead of ours when clicking the logo + */ + open fun FlowContent.clickableLogo(page: PageNode, pathToRoot: String) { + if (context.configuration.delayTemplateSubstitution && page is ContentPage) { + templateCommand(PathToRootSubstitutionCommand(pattern = "###", default = pathToRoot)) { + a { + href = "###index.html" + div { + id = "logo" + } + } + } + } else a { + href = pathToRoot.split("/") + .filter { it.isNotBlank() } + .drop(1).takeIf { it.isNotEmpty() } + ?.joinToString(separator = "/", postfix = "/index.html") + ?: "index.html" + div { + id = "logo" + } + } + } private val ContentNode.isAnchorable: Boolean get() = anchorLabel != null diff --git a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt index 15d2473f..e2953d46 100644 --- a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt +++ b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt @@ -10,8 +10,10 @@ import org.jetbrains.dokka.model.WithChildren import org.jetbrains.dokka.pages.PageNode import org.jetbrains.dokka.pages.RendererSpecificPage import org.jetbrains.dokka.pages.RenderingStrategy +import org.jetbrains.dokka.plugability.DokkaContext -class NavigationPage(val root: NavigationNode, val moduleName: String) : RendererSpecificPage { +class NavigationPage(val root: NavigationNode, val moduleName: String, val context: DokkaContext) : + RendererSpecificPage { override val name = "navigation" override val children = emptyList<PageNode>() @@ -23,9 +25,13 @@ class NavigationPage(val root: NavigationNode, val moduleName: String) : Rendere } private fun <R> TagConsumer<R>.visit(node: NavigationNode, renderer: HtmlRenderer): R = with(renderer) { - templateCommand(AddToNavigationCommand(moduleName)) { - visit(node,"${moduleName}-nav-submenu", renderer) - } + if (context.configuration.delayTemplateSubstitution) { + templateCommand(AddToNavigationCommand(moduleName)) { + visit(node, "${moduleName}-nav-submenu", renderer) + } + } else { + visit(node, "${moduleName}-nav-submenu", renderer) + } } private fun <R> TagConsumer<R>.visit(node: NavigationNode, navId: String, renderer: HtmlRenderer): R = @@ -52,9 +58,10 @@ data class NavigationNode( val dri: DRI, val sourceSets: Set<DisplaySourceSet>, override val children: List<NavigationNode> -): WithChildren<NavigationNode> +) : WithChildren<NavigationNode> -fun NavigationPage.transform(block: (NavigationNode) -> NavigationNode) = NavigationPage(root.transform(block), moduleName) +fun NavigationPage.transform(block: (NavigationNode) -> NavigationNode) = + NavigationPage(root.transform(block), moduleName, context) fun NavigationNode.transform(block: (NavigationNode) -> NavigationNode) = run(block).let { NavigationNode(it.name, it.dri, it.sourceSets, it.children.map(block)) } diff --git a/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt index 6ef6a6ec..e2def59b 100644 --- a/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt +++ b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt @@ -2,16 +2,15 @@ package org.jetbrains.dokka.base.renderers.html import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.jetbrains.dokka.Platform +import org.jetbrains.dokka.base.templating.AddToSearch import org.jetbrains.dokka.model.DisplaySourceSet import org.jetbrains.dokka.model.dfs import org.jetbrains.dokka.model.withDescendants import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.pages.PageTransformer -import java.util.concurrent.ConcurrentHashMap typealias PageId = String -typealias Json = String data class SearchRecord( val name: String, @@ -29,7 +28,7 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { private val mapper = jacksonObjectMapper() - open fun generatePagesList(pages: Map<PageId, PageWithId>, locationResolver: PageResolver): Json = + open fun generatePagesList(pages: Map<PageId, PageWithId>, locationResolver: PageResolver): List<SearchRecord> = pages.entries .filter { it.key.isNotEmpty() } .sortedWith(compareBy({ it.key }, { it.value.displayableSignature })) @@ -44,7 +43,7 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { searchKeys = listOf(entry.key, subentry.value.displayableSignature) ) } - }.run { mapper.writeValueAsString(this) } + } open fun createSearchRecord( name: String, @@ -76,13 +75,19 @@ open class SearchbarDataInstaller(val context: DokkaContext) : PageTransformer { override fun invoke(input: RootPageNode): RootPageNode { val page = RendererSpecificResourcePage( - name = "scripts/pages.js", + name = "scripts/pages.json", children = emptyList(), strategy = RenderingStrategy.PageLocationResolvableWrite { resolver -> - input.withDescendants().fold(emptyMap<PageId, PageWithId>()) { pageList, page -> + val content = input.withDescendants().fold(emptyMap<PageId, PageWithId>()) { pageList, page -> processPage(page)?.let { pageList + Pair(it.id, it) } ?: pageList }.run { - """var pages = ${generatePagesList(this, resolver)}""" + generatePagesList(this, resolver) + } + + if (context.configuration.delayTemplateSubstitution) { + mapper.writeValueAsString(AddToSearch(context.configuration.moduleName, content)) + } else { + mapper.writeValueAsString(content) } }) diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index cc97bb72..25d95a8f 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -4,6 +4,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.DokkaBaseConfiguration import org.jetbrains.dokka.base.renderers.sourceSets +import org.jetbrains.dokka.base.templating.AddToSearch import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.* import org.jetbrains.dokka.pages.* @@ -51,10 +52,14 @@ open class NavigationSearchInstaller(val context: DokkaContext) : NavigationData name = "scripts/navigation-pane.json", children = emptyList(), strategy = RenderingStrategy.DriLocationResolvableWrite { resolver -> - mapper.writeValueAsString( - navigableChildren(input).withDescendants().map { - createSearchRecordFromNode(it, resolveLocation(resolver, it.dri, it.sourceSets).orEmpty()) - }) + val content = navigableChildren(input).withDescendants().map { + createSearchRecordFromNode(it, resolveLocation(resolver, it.dri, it.sourceSets).orEmpty()) + } + if (context.configuration.delayTemplateSubstitution) { + mapper.writeValueAsString(AddToSearch(context.configuration.moduleName, content.toList())) + } else { + mapper.writeValueAsString(content) + } }) return input.modified(children = input.children + page) @@ -70,8 +75,13 @@ open class NavigationSearchInstaller(val context: DokkaContext) : NavigationData open class NavigationPageInstaller(val context: DokkaContext) : NavigationDataProvider(), PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = - input.modified(children = input.children + NavigationPage(navigableChildren(input), - (listOf(input) + input.children).firstOrNull { it is ContentPage && it.name.isNotBlank() }?.name.orEmpty())) + input.modified( + children = input.children + NavigationPage( + root = navigableChildren(input), + moduleName = context.configuration.moduleName, + context = context + ) + ) } class CustomResourceInstaller(val dokkaContext: DokkaContext) : PageTransformer { diff --git a/plugins/base/src/main/kotlin/templating/AddToSearch.kt b/plugins/base/src/main/kotlin/templating/AddToSearch.kt new file mode 100644 index 00000000..fdec45c2 --- /dev/null +++ b/plugins/base/src/main/kotlin/templating/AddToSearch.kt @@ -0,0 +1,5 @@ +package org.jetbrains.dokka.base.templating + +import org.jetbrains.dokka.base.renderers.html.SearchRecord + +data class AddToSearch(val moduleName: String, val elements: List<SearchRecord>): Command
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/templating/jsonMapperForPlugins.kt b/plugins/base/src/main/kotlin/templating/jsonMapperForPlugins.kt index 9b656309..71245778 100644 --- a/plugins/base/src/main/kotlin/templating/jsonMapperForPlugins.kt +++ b/plugins/base/src/main/kotlin/templating/jsonMapperForPlugins.kt @@ -38,7 +38,6 @@ fun toJsonString(value: Any): String = objectMapper.writeValueAsString(value) inline fun <reified T : Any> parseJson(json: String): T = parseJson(json, TypeReference()) - @PublishedApi internal fun <T : Any> parseJson(json: String, typeReference: TypeReference<T>): T = objectMapper.readValue(json, typeReference.jackson) |