aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
-rw-r--r--plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt11
-rw-r--r--plugins/base/src/main/kotlin/renderers/FileWriter.kt79
-rw-r--r--plugins/base/src/main/kotlin/renderers/OutputWriter.kt7
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt3
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt2
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt2
6 files changed, 95 insertions, 9 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
index c6183cf3..951545d2 100644
--- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
@@ -1,18 +1,19 @@
package org.jetbrains.dokka.base.renderers
-import org.jetbrains.dokka.CoreExtensions
+import org.jetbrains.dokka.base.DokkaBase
+import org.jetbrains.dokka.base.resolvers.LocationProvider
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
-import org.jetbrains.dokka.renderers.OutputWriter
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.renderers.Renderer
-import org.jetbrains.dokka.resolvers.LocationProvider
import org.jetbrains.dokka.transformers.pages.PageNodeTransformer
abstract class DefaultRenderer<T>(
protected val context: DokkaContext
) : Renderer {
- protected val outputWriter = context.single(CoreExtensions.outputWriter)
+ protected val outputWriter = context.plugin<DokkaBase>().querySingle { outputWriter }
protected lateinit var locationProvider: LocationProvider
private set
@@ -116,7 +117,7 @@ abstract class DefaultRenderer<T>(
val newRoot = preprocessors.fold(root) { acc, t -> t(acc) }
locationProvider =
- context.single(CoreExtensions.locationProviderFactory).getLocationProvider(newRoot)
+ context.plugin<DokkaBase>().querySingle { locationproviderFactory }.getLocationProvider(newRoot)
root.children<ModulePageNode>().forEach { renderPackageList(it) }
diff --git a/plugins/base/src/main/kotlin/renderers/FileWriter.kt b/plugins/base/src/main/kotlin/renderers/FileWriter.kt
new file mode 100644
index 00000000..5d3067fc
--- /dev/null
+++ b/plugins/base/src/main/kotlin/renderers/FileWriter.kt
@@ -0,0 +1,79 @@
+package org.jetbrains.dokka.base.renderers
+
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.renderers.OutputWriter
+import java.io.File
+import java.io.IOException
+import java.net.URI
+import java.nio.file.*
+
+class FileWriter(val context: DokkaContext): OutputWriter {
+ private val createdFiles: MutableSet<String> = mutableSetOf()
+ private val jarUriPrefix = "jar:file:"
+ private val root = context.configuration.outputDir
+
+ override fun write(path: String, text: String, ext: String) {
+ if (createdFiles.contains(path)) {
+ context.logger.error("An attempt to write ${root}/$path several times!")
+ return
+ }
+ createdFiles.add(path)
+
+ try {
+ val dir = Paths.get(root, path.dropLastWhile { it != '/' }).toFile()
+ dir.mkdirsOrFail()
+ Files.write(Paths.get(root, "$path$ext"), text.lines())
+ } catch (e: Throwable) {
+ context.logger.error("Failed to write $this. ${e.message}")
+ e.printStackTrace()
+ }
+ }
+
+ override fun writeResources(pathFrom: String, pathTo: String) =
+ if (javaClass.getResource(pathFrom).toURI().toString().startsWith(jarUriPrefix)) {
+ copyFromJar(pathFrom, pathTo)
+ } else {
+ copyFromDirectory(pathFrom, pathTo)
+ }
+
+
+ private fun copyFromDirectory(pathFrom: String, pathTo: String) {
+ val dest = Paths.get(root, pathTo).toFile()
+ val uri = javaClass.getResource(pathFrom).toURI()
+ File(uri).copyRecursively(dest, true)
+ }
+
+ private fun copyFromJar(pathFrom: String, pathTo: String) {
+ val rebase = fun(path: String) =
+ "$pathTo/${path.removePrefix(pathFrom)}"
+ val dest = Paths.get(root, pathTo).toFile()
+ dest.mkdirsOrFail()
+ val uri = javaClass.getResource(pathFrom).toURI()
+ val fs = getFileSystemForURI(uri)
+ val path = fs.getPath(pathFrom)
+ for (file in Files.walk(path).iterator()) {
+ if (Files.isDirectory(file)) {
+ val dirPath = file.toAbsolutePath().toString()
+ Paths.get(root, rebase(dirPath)).toFile().mkdirsOrFail()
+ } else {
+ val filePath = file.toAbsolutePath().toString()
+ Paths.get(root, rebase(filePath)).toFile().writeBytes(
+ javaClass.getResourceAsStream(filePath).readBytes()
+ )
+ }
+ }
+ }
+
+ private fun File.mkdirsOrFail() {
+ if (!mkdirs() && !exists()) {
+ throw IOException("Failed to create directory $this")
+ }
+ }
+
+ private fun getFileSystemForURI(uri: URI): FileSystem =
+ try {
+ FileSystems.newFileSystem(uri, emptyMap<String, Any>())
+ } catch (e: FileSystemAlreadyExistsException) {
+ FileSystems.getFileSystem(uri)
+ }
+} \ No newline at end of file
diff --git a/plugins/base/src/main/kotlin/renderers/OutputWriter.kt b/plugins/base/src/main/kotlin/renderers/OutputWriter.kt
new file mode 100644
index 00000000..a6fda51a
--- /dev/null
+++ b/plugins/base/src/main/kotlin/renderers/OutputWriter.kt
@@ -0,0 +1,7 @@
+package org.jetbrains.dokka.base.renderers
+
+interface OutputWriter {
+
+ fun write(path: String, text: String, ext: String)
+ fun writeResources(pathFrom: String, pathTo: String)
+} \ 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 c9270681..8bf00043 100644
--- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
@@ -1,4 +1,4 @@
-package org.jetbrains.dokka.renderers.html
+package org.jetbrains.dokka.base.renderers.html
import kotlinx.html.*
import kotlinx.html.stream.createHTML
@@ -7,7 +7,6 @@ import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.Function
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
-import org.jetbrains.dokka.renderers.OutputWriter
import java.io.File
open class HtmlRenderer(
diff --git a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt
index 4a2fb40d..ad574769 100644
--- a/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/NavigationPage.kt
@@ -1,4 +1,4 @@
-package org.jetbrains.dokka.renderers.html
+package org.jetbrains.dokka.base.renderers.html
import kotlinx.html.*
import kotlinx.html.stream.createHTML
diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
index 09164d97..ecd2e89a 100644
--- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
@@ -1,4 +1,4 @@
-package org.jetbrains.dokka.renderers.html
+package org.jetbrains.dokka.base.renderers.html
import kotlinx.html.h1
import kotlinx.html.id