diff options
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
4 files changed, 89 insertions, 93 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index d63e8da6..31753332 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -15,6 +15,7 @@ import org.jetbrains.dokka.base.resolvers.anchors.SymbolAnchorHint import org.jetbrains.dokka.base.resolvers.local.DokkaBaseLocationProvider import org.jetbrains.dokka.base.templating.InsertTemplateExtra import org.jetbrains.dokka.base.templating.PathToRootSubstitutionCommand +import org.jetbrains.dokka.base.templating.ProjectNameSubstitutionCommand import org.jetbrains.dokka.base.templating.ResolveLinkCommand import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.DisplaySourceSet @@ -603,22 +604,13 @@ open class HtmlRenderer( override fun FlowContent.buildNavigation(page: PageNode) = - div("navigation-wrapper") { - id = "navigation-wrapper" - div(classes = "breadcrumbs") { - val path = locationProvider.ancestors(page).filterNot { it is RendererSpecificPage }.asReversed() - if (path.isNotEmpty()) { - buildNavigationElement(path.first(), page) - path.drop(1).forEach { node -> - text("/") - buildNavigationElement(node, page) - } - } - } - div("pull-right d-flex") { - filterButtons(page) - div { - id = "searchBar" + div(classes = "breadcrumbs") { + val path = locationProvider.ancestors(page).filterNot { it is RendererSpecificPage }.asReversed() + if (path.size > 1) { + buildNavigationElement(path.first(), page) + path.drop(1).forEach { node -> + text("/") + buildNavigationElement(node, page) } } } @@ -685,12 +677,15 @@ open class HtmlRenderer( pageContext: ContentPage ) { div("sample-container") { - code(code.style.joinToString(" ") { it.toString().toLowerCase() }) { - attributes["theme"] = "idea" - pre { + val codeLang = "lang-" + code.language.ifEmpty { "kotlin" } + val stylesWithBlock = code.style + TextStyle.Block + codeLang + pre { + code(stylesWithBlock.joinToString(" ") { it.toString().toLowerCase() }) { + attributes["theme"] = "idea" code.children.forEach { buildContentNode(it, pageContext) } } } + copyButton() } } @@ -698,7 +693,9 @@ open class HtmlRenderer( code: ContentCodeInline, pageContext: ContentPage ) { - code { + val codeLang = "lang-" + code.language.ifEmpty { "kotlin" } + val stylesWithBlock = code.style + codeLang + code(stylesWithBlock.joinToString(" ") { it.toString().toLowerCase() }) { code.children.forEach { buildContentNode(it, pageContext) } } } @@ -716,7 +713,7 @@ open class HtmlRenderer( } unappliedStyles.isNotEmpty() -> { val styleToApply = unappliedStyles.first() - applyStyle(styleToApply){ + applyStyle(styleToApply) { buildText(textNode, unappliedStyles - styleToApply) } } @@ -726,12 +723,13 @@ open class HtmlRenderer( } } - private inline fun FlowContent.applyStyle(styleToApply: Style, crossinline body: FlowContent.() -> Unit){ - when(styleToApply){ + private inline fun FlowContent.applyStyle(styleToApply: Style, crossinline body: FlowContent.() -> Unit) { + when (styleToApply) { TextStyle.Bold -> b { body() } TextStyle.Italic -> i { body() } TextStyle.Strikethrough -> strike { body() } TextStyle.Strong -> strong { body() } + is TokenStyle -> span("token " + styleToApply.toString().toLowerCase()) { body() } else -> body() } } @@ -741,8 +739,6 @@ open class HtmlRenderer( super.render(root) } - private fun PageNode.root(path: String) = locationProvider.pathToRoot(this) + path - override fun buildPage(page: ContentPage, content: (FlowContent, ContentPage) -> Unit): String = buildHtml(page, page.embeddedResources) { div("main-content") { @@ -762,11 +758,19 @@ open class HtmlRenderer( meta(name = "viewport", content = "width=device-width, initial-scale=1", charset = "UTF-8") title(page.name) templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { - link(href = page.root("###images/logo-icon.svg"), rel = "icon", type = "image/svg") + link(href = "###images/logo-icon.svg", rel = "icon", type = "image/svg") } templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { script { unsafe { +"""var pathToRoot = "###";""" } } } + // This script doesn't need to be there but it is nice to have since app in dark mode doesn't 'blink' (class is added before it is rendered) + script { unsafe { +""" + const storage = localStorage.getItem("dokka-dark-mode") + const savedDarkMode = storage ? JSON.parse(storage) : false + if(savedDarkMode === true){ + document.getElementsByTagName("html")[0].classList.add("theme-dark") + } + """.trimIndent() } } resources.forEach { when { it.substringBefore('?').substringAfterLast('.') == "css" -> @@ -803,24 +807,41 @@ open class HtmlRenderer( } } body { - div { - id = "container" + div("navigation-wrapper") { + id = "navigation-wrapper" div { - id = "leftColumn" + id = "leftToggler" + span("icon-toggler") + } + div("library-name") { clickableLogo(page, pathToRoot) + } + context.configuration.moduleVersion?.let { moduleVersion -> + div { text(moduleVersion) } + } + div("pull-right d-flex") { + filterButtons(page) + button { + id = "theme-toggle-button" + span { + id = "theme-toggle" + } + } div { - id = "paneSearch" + id = "searchBar" } + } + } + div { + id = "container" + div { + id = "leftColumn" div { id = "sideMenu" } } div { id = "main" - div { - id = "leftToggler" - span("icon-toggler") - } templateCommand(PathToRootSubstitutionCommand("###", default = pathToRoot)) { script(type = ScriptType.textJavaScript, src = "###scripts/main.js") {} } @@ -857,15 +878,17 @@ open class HtmlRenderer( templateCommand(PathToRootSubstitutionCommand(pattern = "###", default = pathToRoot)) { a { href = "###index.html" - div { - id = "logo" + templateCommand(ProjectNameSubstitutionCommand(pattern = "@@@", default = context.configuration.moduleName)) { + span { + text("@@@") + } } } } - } else a { - href = pathToRoot + "index.html" - div { - id = "logo" + } else { + a { + href = pathToRoot + "index.html" + text(context.configuration.moduleName) } } } diff --git a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt index e2953d46..e0b20f01 100644 --- a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt +++ b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt @@ -40,13 +40,13 @@ class NavigationPage(val root: NavigationNode, val moduleName: String, val conte id = navId attributes["pageId"] = "${moduleName}::${node.pageId}" div("overview") { - buildLink(node.dri, node.sourceSets.toList()) { buildBreakableText(node.name) } if (node.children.isNotEmpty()) { - span("navButton pull-right") { + span("navButton") { onClick = """document.getElementById("$navId").classList.toggle("hidden");""" span("navButtonContent") } } + buildLink(node.dri, node.sourceSets.toList()) { buildBreakableText(node.name) } } node.children.withIndex().forEach { (n, p) -> visit(p, "$navId-$n", renderer) } } diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlFormatingUtils.kt b/plugins/base/src/main/kotlin/renderers/html/htmlFormatingUtils.kt index b9eab293..c77a6e94 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlFormatingUtils.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlFormatingUtils.kt @@ -3,7 +3,7 @@ package org.jetbrains.dokka.base.renderers.html import kotlinx.html.FlowContent import kotlinx.html.span -fun FlowContent.buildTextBreakableAfterCapitalLetters(name: String) { +fun FlowContent.buildTextBreakableAfterCapitalLetters(name: String, hasLastElement: Boolean = false) { if (name.contains(" ")) { val withOutSpaces = name.split(" ") withOutSpaces.dropLast(1).forEach { @@ -12,8 +12,8 @@ fun FlowContent.buildTextBreakableAfterCapitalLetters(name: String) { } buildBreakableText(withOutSpaces.last()) } else { - val content = name.replace(Regex("(?!^)([A-Z])"), " $1").split(" ") - joinToHtml(content) { + val content = name.replace(Regex("(?<=[a-z])([A-Z])"), " $1").split(" ") + joinToHtml(content, hasLastElement) { it } } @@ -22,32 +22,35 @@ fun FlowContent.buildTextBreakableAfterCapitalLetters(name: String) { fun FlowContent.buildBreakableDotSeparatedHtml(name: String) { val phrases = name.split(".") phrases.forEachIndexed { i, e -> + val elementWithOptionalDot = e.takeIf { i == phrases.lastIndex } ?: "$e." if (e.length > 10) { - buildBreakableText(e) + buildTextBreakableAfterCapitalLetters(elementWithOptionalDot, hasLastElement = i == phrases.lastIndex) } else { - val elementWithOptionalDot = - if (i != phrases.lastIndex) { - "$e." - } else { - e - } - buildBreakableHtmlElement(elementWithOptionalDot) + buildBreakableHtmlElement(elementWithOptionalDot, i == phrases.lastIndex) } } } -private fun FlowContent.joinToHtml(elements: List<String>, onEach: (String) -> String) { +private fun FlowContent.joinToHtml(elements: List<String>, hasLastElement: Boolean = true, onEach: (String) -> String) { elements.dropLast(1).forEach { buildBreakableHtmlElement(onEach(it)) } - span { - buildBreakableHtmlElement(elements.last(), last = true) + elements.takeIf { it.isNotEmpty() && it.last().isNotEmpty() }?.let { + if (hasLastElement) { + span { + buildBreakableHtmlElement(it.last(), last = true) + } + } else { + buildBreakableHtmlElement(it.last(), last = false) + } } } private fun FlowContent.buildBreakableHtmlElement(element: String, last: Boolean = false) { - span { - +element + element.takeIf { it.isNotBlank() }?.let { + span { + +it + } } if (!last) { wbr { } @@ -56,4 +59,4 @@ private fun FlowContent.buildBreakableHtmlElement(element: String, last: Boolean fun FlowContent.buildBreakableText(name: String) = if (name.contains(".")) buildBreakableDotSeparatedHtml(name) - else buildTextBreakableAfterCapitalLetters(name)
\ No newline at end of file + else buildTextBreakableAfterCapitalLetters(name, hasLastElement = true)
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index b6099e21..347e16bf 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -49,37 +49,6 @@ abstract class NavigationDataProvider { } } -open class NavigationSearchInstaller(val context: DokkaContext) : NavigationDataProvider(), PageTransformer { - private val mapper = jacksonObjectMapper() - - open fun createSearchRecordFromNode(node: NavigationNode, location: String): SearchRecord = - SearchRecord(name = node.name, location = location) - - override fun invoke(input: RootPageNode): RootPageNode { - val page = RendererSpecificResourcePage( - name = "scripts/navigation-pane.json", - children = emptyList(), - strategy = RenderingStrategy.DriLocationResolvableWrite { resolver -> - 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) - } - - private fun resolveLocation(locationResolver: DriResolver, dri: DRI, sourceSets: Set<DisplaySourceSet>): String? = - locationResolver(dri, sourceSets).also { location -> - if (location.isNullOrBlank()) context.logger.warn("Cannot resolve path for $dri and sourceSets: ${sourceSets.joinToString { it.name }}") - } - -} - open class NavigationPageInstaller(val context: DokkaContext) : NavigationDataProvider(), PageTransformer { override fun invoke(input: RootPageNode): RootPageNode = @@ -118,6 +87,7 @@ class ScriptsInstaller(private val dokkaContext: DokkaContext) : PageTransformer "scripts/navigation-loader.js", "scripts/platform-content-handler.js", "scripts/main.js", + "scripts/prism.js" ) override fun invoke(input: RootPageNode): RootPageNode = @@ -134,9 +104,9 @@ class ScriptsInstaller(private val dokkaContext: DokkaContext) : PageTransformer class StylesInstaller(private val dokkaContext: DokkaContext) : PageTransformer { private val stylesPages = listOf( "styles/style.css", - "styles/logo-styles.css", "styles/jetbrains-mono.css", - "styles/main.css" + "styles/main.css", + "styles/prism.css" ) override fun invoke(input: RootPageNode): RootPageNode = @@ -153,13 +123,13 @@ class StylesInstaller(private val dokkaContext: DokkaContext) : PageTransformer object AssetsInstaller : PageTransformer { private val imagesPages = listOf( "images/arrow_down.svg", - "images/docs_logo.svg", "images/logo-icon.svg", "images/go-to-top-icon.svg", "images/footer-go-to-link.svg", "images/anchor-copy-button.svg", "images/copy-icon.svg", "images/copy-successful-icon.svg", + "images/theme-toggle.svg", ) override fun invoke(input: RootPageNode) = input.modified( |