aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-04-06 15:58:55 +0200
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-04-22 13:10:48 +0200
commit720c50fb7f4bf4ae4b4d4c406cfb958a7fba8ffb (patch)
tree10869d6dcbd5af8599b2091fc6581ce6b16a47d5 /plugins/base/src/main/kotlin/renderers
parent58126c324c30fff4f65fef7cc72aa715364ef358 (diff)
downloaddokka-720c50fb7f4bf4ae4b4d4c406cfb958a7fba8ffb.tar.gz
dokka-720c50fb7f4bf4ae4b4d4c406cfb958a7fba8ffb.tar.bz2
dokka-720c50fb7f4bf4ae4b4d4c406cfb958a7fba8ffb.zip
Adds asynchronous rendering
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
-rw-r--r--plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt71
-rw-r--r--plugins/base/src/main/kotlin/renderers/FileWriter.kt4
-rw-r--r--plugins/base/src/main/kotlin/renderers/OutputWriter.kt4
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt14
4 files changed, 73 insertions, 20 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
index 26c75dbf..4178e427 100644
--- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
@@ -1,5 +1,7 @@
package org.jetbrains.dokka.base.renderers
+import com.sun.jna.platform.win32.COM.Dispatch
+import kotlinx.coroutines.*
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
import org.jetbrains.dokka.pages.*
@@ -22,10 +24,20 @@ abstract class DefaultRenderer<T>(
abstract fun T.buildHeader(level: Int, content: T.() -> Unit)
abstract fun T.buildLink(address: String, content: T.() -> Unit)
- abstract fun T.buildList(node: ContentList, pageContext: ContentPage, platformRestriction: PlatformData? = null)
+ abstract fun T.buildList(
+ node: ContentList,
+ pageContext: ContentPage,
+ platformRestriction: PlatformData? = null
+ )
+
abstract fun T.buildNewLine()
abstract fun T.buildResource(node: ContentEmbeddedResource, pageContext: ContentPage)
- abstract fun T.buildTable(node: ContentTable, pageContext: ContentPage, platformRestriction: PlatformData? = null)
+ abstract fun T.buildTable(
+ node: ContentTable,
+ pageContext: ContentPage,
+ platformRestriction: PlatformData? = null
+ )
+
abstract fun T.buildText(textNode: ContentText)
abstract fun T.buildNavigation(page: PageNode)
@@ -35,7 +47,11 @@ abstract class DefaultRenderer<T>(
open fun T.buildPlatformDependent(content: PlatformHintedContent, pageContext: ContentPage) =
buildContentNode(content.inner, pageContext)
- open fun T.buildGroup(node: ContentGroup, pageContext: ContentPage, platformRestriction: PlatformData? = null) =
+ open fun T.buildGroup(
+ node: ContentGroup,
+ pageContext: ContentPage,
+ platformRestriction: PlatformData? = null
+ ) =
wrapGroup(node, pageContext) { node.children.forEach { it.build(this, pageContext, platformRestriction) } }
open fun T.wrapGroup(node: ContentGroup, pageContext: ContentPage, childrenCallback: T.() -> Unit) =
@@ -53,11 +69,19 @@ abstract class DefaultRenderer<T>(
code.forEach { it.build(this, pageContext) }
}
- open fun T.buildHeader(node: ContentHeader, pageContext: ContentPage, platformRestriction: PlatformData? = null) {
+ open fun T.buildHeader(
+ node: ContentHeader,
+ pageContext: ContentPage,
+ platformRestriction: PlatformData? = null
+ ) {
buildHeader(node.level) { node.children.forEach { it.build(this, pageContext, platformRestriction) } }
}
- open fun ContentNode.build(builder: T, pageContext: ContentPage, platformRestriction: PlatformData? = null) =
+ open fun ContentNode.build(
+ builder: T,
+ pageContext: ContentPage,
+ platformRestriction: PlatformData? = null
+ ) =
builder.buildContentNode(this, pageContext, platformRestriction)
open fun T.buildContentNode(
@@ -93,14 +117,14 @@ abstract class DefaultRenderer<T>(
page.content.build(context, page)
}
- open fun renderPage(page: PageNode) {
+ open suspend fun renderPage(page: PageNode) {
val path by lazy { locationProvider.resolve(page, skipExtension = true) }
when (page) {
- is ContentPage -> outputWriter.write(path, buildPage(page) { c, p -> buildPageContent(c, p) }, ".html")
+ is ContentPage -> outputWriter.write(path, runBlocking { buildPage(page) { c, p -> buildPageContent(c, p) } }, ".html")
is RendererSpecificPage -> when (val strategy = page.strategy) {
is RenderingStrategy.Copy -> outputWriter.writeResources(strategy.from, path)
is RenderingStrategy.Write -> outputWriter.write(path, strategy.text, "")
- is RenderingStrategy.Callback -> outputWriter.write(path, strategy.instructions(this, page), ".html")
+ is RenderingStrategy.Callback -> outputWriter.write(path, strategy.instructions(this@DefaultRenderer, page), ".html")
RenderingStrategy.DoNothing -> Unit
}
else -> throw AssertionError(
@@ -109,12 +133,33 @@ abstract class DefaultRenderer<T>(
}
}
- open fun renderPages(root: PageNode) {
- renderPage(root)
- root.children.forEach { renderPages(it) }
+ private suspend fun CoroutineScope.renderPages(root: PageNode) {
+ coroutineScope {
+ launch(Dispatchers.IO) { renderPage(root) }.join()
+ root.children.forEach {
+ renderPages(it)
+ }
+ }
}
- override fun render(root: RootPageNode) {
+ // reimplement this as preprocessor
+ open suspend fun renderPackageList(root: ContentPage) =
+ getPackageNamesAndPlatforms(root)
+ .keys
+ .joinToString("\n")
+ .also { outputWriter.write("${root.name}/package-list", it, "") }
+
+ open suspend fun getPackageNamesAndPlatforms(root: PageNode): Map<String, List<PlatformData>> =
+ root.children
+ .map { getPackageNamesAndPlatforms(it) }
+ .fold(emptyMap<String, List<PlatformData>>()) { e, acc -> acc + e } +
+ if (root is PackagePageNode) {
+ mapOf(root.name to root.platforms())
+ } else {
+ emptyMap()
+ }
+
+ protected fun renderImpl(coroutineScope: CoroutineScope, root: RootPageNode): Job = coroutineScope.launch(Dispatchers.Default) {
val newRoot = preprocessors.fold(root) { acc, t -> t(acc) }
locationProvider =
@@ -122,6 +167,8 @@ abstract class DefaultRenderer<T>(
renderPages(newRoot)
}
+
+ override fun CoroutineScope.render(root: RootPageNode) = renderImpl(this, root)
}
fun ContentPage.platforms() = this.content.platforms.toList() \ 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 bf2426fe..07d25eb6 100644
--- a/plugins/base/src/main/kotlin/renderers/FileWriter.kt
+++ b/plugins/base/src/main/kotlin/renderers/FileWriter.kt
@@ -11,7 +11,7 @@ class FileWriter(val context: DokkaContext): OutputWriter {
private val jarUriPrefix = "jar:file:"
private val root = File(context.configuration.outputDir).absolutePath
- override fun write(path: String, text: String, ext: String) {
+ override suspend fun write(path: String, text: String, ext: String) {
if (createdFiles.contains(path)) {
context.logger.error("An attempt to write ${root}/$path several times!")
return
@@ -28,7 +28,7 @@ class FileWriter(val context: DokkaContext): OutputWriter {
}
}
- override fun writeResources(pathFrom: String, pathTo: String) =
+ override suspend fun writeResources(pathFrom: String, pathTo: String) =
if (javaClass.getResource(pathFrom).toURI().toString().startsWith(jarUriPrefix)) {
copyFromJar(pathFrom, pathTo)
} else {
diff --git a/plugins/base/src/main/kotlin/renderers/OutputWriter.kt b/plugins/base/src/main/kotlin/renderers/OutputWriter.kt
index a6fda51a..1827c7f0 100644
--- a/plugins/base/src/main/kotlin/renderers/OutputWriter.kt
+++ b/plugins/base/src/main/kotlin/renderers/OutputWriter.kt
@@ -2,6 +2,6 @@ package org.jetbrains.dokka.base.renderers
interface OutputWriter {
- fun write(path: String, text: String, ext: String)
- fun writeResources(pathFrom: String, pathTo: String)
+ suspend fun write(path: String, text: String, ext: String)
+ suspend 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 bbb5b685..a32cb715 100644
--- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
@@ -1,5 +1,9 @@
package org.jetbrains.dokka.base.renderers.html
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.launch
import kotlinx.html.*
import kotlinx.html.stream.createHTML
import org.jetbrains.dokka.base.DokkaBase
@@ -264,7 +268,7 @@ open class HtmlRenderer(
}
}
- override fun renderPage(page: PageNode) {
+ override suspend fun renderPage(page: PageNode) {
super.renderPage(page)
if (page is ContentPage) {
pageList.add(
@@ -282,9 +286,11 @@ open class HtmlRenderer(
text(textNode.text)
}
- override fun render(root: RootPageNode) {
- super.render(root)
- outputWriter.write("scripts/pages", "var pages = [\n${pageList.joinToString(",\n")}\n]", ".js")
+ override fun CoroutineScope.render(root: RootPageNode): Job {
+ super.renderImpl(this, root)
+ return launch(Dispatchers.IO) {
+ outputWriter.write("scripts/pages", "var pages = [\n${pageList.joinToString(",\n")}\n]", ".js")
+ }
}
private fun PageNode.root(path: String) = locationProvider.resolveRoot(this) + path