diff options
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
5 files changed, 81 insertions, 61 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt index 5c0f54d8..f393c0fa 100644 --- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt @@ -15,7 +15,6 @@ import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.renderers.Renderer import org.jetbrains.dokka.transformers.pages.PageTransformer -import java.io.File abstract class DefaultRenderer<T>( protected val context: DokkaContext @@ -223,9 +222,4 @@ abstract class DefaultRenderer<T>( internal typealias SerializedBeforeAndAfter = Pair<String, String> internal typealias InstanceWithSource = Pair<ContentDivergentInstance, DisplaySourceSet> -fun ContentPage.sourceSets() = this.content.sourceSets - -fun ContentEmbeddedResource.isImage(): Boolean { - val imageExtensions = setOf("png", "jpg", "jpeg", "gif", "bmp", "tif", "webp", "svg") - return File(address).extension.toLowerCase() in imageExtensions -} +fun ContentPage.sourceSets() = this.content.sourceSets
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/FileWriter.kt b/plugins/base/src/main/kotlin/renderers/FileWriter.kt index cd38f1b9..f8c0e882 100644 --- a/plugins/base/src/main/kotlin/renderers/FileWriter.kt +++ b/plugins/base/src/main/kotlin/renderers/FileWriter.kt @@ -33,7 +33,7 @@ class FileWriter(val context: DokkaContext): OutputWriter { } override suspend fun writeResources(pathFrom: String, pathTo: String) = - if (javaClass.getResource(pathFrom).toURI().toString().startsWith(jarUriPrefix)) { + if (javaClass.getResource(pathFrom)?.toURI()?.toString()?.startsWith(jarUriPrefix) == true) { copyFromJar(pathFrom, pathTo) } else { copyFromDirectory(pathFrom, pathTo) @@ -42,9 +42,10 @@ class FileWriter(val context: DokkaContext): OutputWriter { private suspend fun copyFromDirectory(pathFrom: String, pathTo: String) { val dest = Paths.get(root.path, pathTo).toFile() - val uri = javaClass.getResource(pathFrom).toURI() + val uri = javaClass.getResource(pathFrom)?.toURI() + val file = uri?.let { File(it) } ?: File(pathFrom) withContext(Dispatchers.IO) { - File(uri).copyRecursively(dest, true) + file.copyRecursively(dest, true) } } @@ -52,7 +53,11 @@ class FileWriter(val context: DokkaContext): OutputWriter { val rebase = fun(path: String) = "$pathTo/${path.removePrefix(pathFrom)}" val dest = Paths.get(root.path, pathTo).toFile() - dest.mkdirsOrFail() + if(dest.isDirectory){ + dest.mkdirsOrFail() + } else { + dest.parentFile.mkdirsOrFail() + } val uri = javaClass.getResource(pathFrom).toURI() val fs = getFileSystemForURI(uri) val path = fs.getPath(pathFrom) diff --git a/plugins/base/src/main/kotlin/renderers/contentTypeChecking.kt b/plugins/base/src/main/kotlin/renderers/contentTypeChecking.kt new file mode 100644 index 00000000..4619bc53 --- /dev/null +++ b/plugins/base/src/main/kotlin/renderers/contentTypeChecking.kt @@ -0,0 +1,16 @@ +package org.jetbrains.dokka.base.renderers + +import org.jetbrains.dokka.base.renderers.HtmlFileExtensions.imageExtensions +import org.jetbrains.dokka.pages.ContentEmbeddedResource +import java.io.File + +fun ContentEmbeddedResource.isImage(): Boolean { + return File(address).extension.toLowerCase() in imageExtensions +} + +fun String.isImage(): Boolean = + substringBefore('?').substringAfterLast('.') in imageExtensions + +object HtmlFileExtensions { + val imageExtensions = setOf("png", "jpg", "jpeg", "gif", "bmp", "tif", "webp", "svg") +}
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 6bdeae62..7a047d3c 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -679,10 +679,10 @@ open class HtmlRenderer( ) { async = true } + it.isImage() -> link(href = page.root(it)) else -> unsafe { +it } } } - link(rel = LinkRel.stylesheet, href = page.root("styles/main.css")) {} script { unsafe { +"""var pathToRoot = "${locationProvider.pathToRoot(page)}";""" } } } body { diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt index a5fca92f..a87254ce 100644 --- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt +++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt @@ -7,6 +7,9 @@ import kotlinx.html.h1 import kotlinx.html.id import kotlinx.html.table import kotlinx.html.tbody +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.base.DokkaBaseConfiguration import org.jetbrains.dokka.base.renderers.sourceSets import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.DEnum @@ -14,30 +17,9 @@ import org.jetbrains.dokka.model.DEnumEntry import org.jetbrains.dokka.model.withDescendants import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.configuration import org.jetbrains.dokka.transformers.pages.PageTransformer - -object SearchPageInstaller : PageTransformer { - override fun invoke(input: RootPageNode) = input.modified(children = input.children + searchPage) - - private val searchPage = RendererSpecificResourcePage( - name = "Search", - children = emptyList(), - strategy = RenderingStrategy<HtmlRenderer> { - buildHtml(it, listOf("styles/style.css", "scripts/pages.js", "scripts/search.js")) { - h1 { - id = "searchTitle" - text("Search results for ") - } - table { - tbody { - id = "searchTable" - } - } - } - }) -} - object NavigationPageInstaller : PageTransformer { private val mapper = jacksonObjectMapper() @@ -89,56 +71,79 @@ object NavigationPageInstaller : PageTransformer { }.sortedBy { it.name.toLowerCase() } } +class CustomResourceInstaller(val dokkaContext: DokkaContext) : PageTransformer { + private val configuration = configuration<DokkaBase, DokkaBaseConfiguration>(dokkaContext) + + private val customAssets = configuration?.customAssets?.map { + RendererSpecificResourcePage("images/${it.name}", emptyList(), RenderingStrategy.Copy(it.absolutePath)) + }.orEmpty() + + private val customStylesheets = configuration?.customStyleSheets?.map { + RendererSpecificResourcePage("styles/${it.name}", emptyList(), RenderingStrategy.Copy(it.absolutePath)) + }.orEmpty() + + override fun invoke(input: RootPageNode): RootPageNode { + val customResourcesPaths = (customAssets + customStylesheets).map { it.name }.toSet() + val withEmbeddedResources = input.transformContentPagesTree { it.modified(embeddedResources = it.embeddedResources + customResourcesPaths) } + val (currentResources, otherPages) = withEmbeddedResources.children.partition { it is RendererSpecificResourcePage } + return input.modified(children = otherPages + currentResources.filterNot { it.name in customResourcesPaths } + customAssets + customStylesheets) + } +} + object ScriptsInstaller : PageTransformer { + private val scriptsPages = listOf( + "scripts/clipboard.js", + "scripts/navigation-loader.js", + "scripts/platform-content-handler.js", + "scripts/main.js" + ) + override fun invoke(input: RootPageNode): RootPageNode { return input.modified( - children = input.children + RendererSpecificResourcePage( - "scripts", - emptyList(), - RenderingStrategy.Copy("/dokka/scripts") - ) + children = input.children + scriptsPages.toRenderSpecificResourcePage() ).transformContentPagesTree { it.modified( - embeddedResources = it.embeddedResources + listOf( - "scripts/navigationLoader.js", - "scripts/platformContentHandler.js", - "scripts/sourceset_dependencies.js", - "scripts/clipboard.js", - ) + embeddedResources = it.embeddedResources + scriptsPages ) } } } object StylesInstaller : PageTransformer { + private val stylesPages = listOf( + "styles/style.css", + "styles/logo-styles.css", + "styles/jetbrains-mono.css", + "styles/main.css" + ) + override fun invoke(input: RootPageNode): RootPageNode = input.modified( - children = input.children + RendererSpecificResourcePage( - "styles", - emptyList(), - RenderingStrategy.Copy("/dokka/styles") - ) + children = input.children + stylesPages.toRenderSpecificResourcePage() ).transformContentPagesTree { it.modified( - embeddedResources = it.embeddedResources + listOf( - "styles/style.css", - "styles/jetbrains-mono.css" - ) + embeddedResources = it.embeddedResources + stylesPages ) } } object AssetsInstaller : PageTransformer { + private val imagesPages = listOf( + "images/arrow_down.svg", + "images/docs_logo.svg", + "images/logo-icon.svg" + ) + override fun invoke(input: RootPageNode) = input.modified( - children = input.children + RendererSpecificResourcePage( - "images", - emptyList(), - RenderingStrategy.Copy("/dokka/images") - ) + children = input.children + imagesPages.toRenderSpecificResourcePage() ) } +private fun List<String>.toRenderSpecificResourcePage(): List<RendererSpecificResourcePage> = + map { RendererSpecificResourcePage(it, emptyList(), RenderingStrategy.Copy("/dokka/$it")) } + class SourcesetDependencyAppender(val context: DokkaContext) : PageTransformer { + private val name = "scripts/sourceset_dependencies.js" override fun invoke(input: RootPageNode): RootPageNode { val dependenciesMap = context.configuration.sourceSets.map { it.sourceSetID to it.dependentSourceSets @@ -155,14 +160,14 @@ class SourcesetDependencyAppender(val context: DokkaContext) : PageTransformer { }}'" val deps = RendererSpecificResourcePage( - name = "scripts/sourceset_dependencies.js", + name = name, children = emptyList(), strategy = RenderingStrategy.Write(createDependenciesJson()) ) return input.modified( children = input.children + deps - ) + ).transformContentPagesTree { it.modified(embeddedResources = it.embeddedResources + name) } } } |