aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers
diff options
context:
space:
mode:
authorKamil Doległo <kamilok1965@interia.pl>2020-05-05 11:53:16 +0200
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-05-19 09:56:32 +0200
commit430d7d6453a0b63dcabecd54aea915410cd35103 (patch)
treeb9e40e3eaa9f1948590141d7d46491e2f34f2ef9 /plugins/base/src/main/kotlin/renderers
parentc1b4669ee227a479516f37ce1b9dff5f2d2bef38 (diff)
downloaddokka-430d7d6453a0b63dcabecd54aea915410cd35103.tar.gz
dokka-430d7d6453a0b63dcabecd54aea915410cd35103.tar.bz2
dokka-430d7d6453a0b63dcabecd54aea915410cd35103.zip
Add a draft version of divergent rendering
Diffstat (limited to 'plugins/base/src/main/kotlin/renderers')
-rw-r--r--plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt21
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt95
2 files changed, 96 insertions, 20 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
index 1340529c..e229f3a6 100644
--- a/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/DefaultRenderer.kt
@@ -1,6 +1,7 @@
package org.jetbrains.dokka.base.renderers
import kotlinx.coroutines.*
+import kotlinx.html.FlowContent
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
import org.jetbrains.dokka.model.SourceSetData
@@ -27,7 +28,7 @@ abstract class DefaultRenderer<T>(
abstract fun T.buildList(
node: ContentList,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
)
abstract fun T.buildNewLine()
@@ -35,7 +36,7 @@ abstract class DefaultRenderer<T>(
abstract fun T.buildTable(
node: ContentTable,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
)
abstract fun T.buildText(textNode: ContentText)
@@ -50,17 +51,20 @@ abstract class DefaultRenderer<T>(
open fun T.buildGroup(
node: ContentGroup,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
) =
wrapGroup(node, pageContext) { node.children.forEach { it.build(this, pageContext, platformRestriction) } }
+ open fun T.buildDivergent(node: ContentDivergentGroup, pageContext: ContentPage) =
+ node.children.forEach { it.build(this, pageContext) }
+
open fun T.wrapGroup(node: ContentGroup, pageContext: ContentPage, childrenCallback: T.() -> Unit) =
childrenCallback()
open fun T.buildLinkText(
nodes: List<ContentNode>,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
) {
nodes.forEach { it.build(this, pageContext, platformRestriction) }
}
@@ -72,7 +76,7 @@ abstract class DefaultRenderer<T>(
open fun T.buildHeader(
node: ContentHeader,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
) {
buildHeader(node.level) { node.children.forEach { it.build(this, pageContext, platformRestriction) } }
}
@@ -80,16 +84,16 @@ abstract class DefaultRenderer<T>(
open fun ContentNode.build(
builder: T,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
) =
builder.buildContentNode(this, pageContext, platformRestriction)
open fun T.buildContentNode(
node: ContentNode,
pageContext: ContentPage,
- platformRestriction: SourceSetData? = null
+ platformRestriction: Set<SourceSetData>? = null
) {
- if (platformRestriction == null || platformRestriction in node.sourceSets) {
+ if (platformRestriction == null || node.sourceSets.any { it in platformRestriction } ) {
when (node) {
is ContentText -> buildText(node)
is ContentHeader -> buildHeader(node, pageContext, platformRestriction)
@@ -107,6 +111,7 @@ abstract class DefaultRenderer<T>(
is ContentGroup -> buildGroup(node, pageContext, platformRestriction)
is ContentBreakLine -> buildNewLine()
is PlatformHintedContent -> buildPlatformDependent(node, pageContext)
+ is ContentDivergentGroup -> buildDivergent(node, pageContext)
else -> buildError(node)
}
}
diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
index caabcda7..97f0bdbd 100644
--- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
@@ -40,14 +40,40 @@ open class HtmlRenderer(
}
}
- override fun FlowContent.buildPlatformDependent(content: PlatformHintedContent, pageContext: ContentPage) {
+ private fun FlowContent.wrapPlatformTagged(
+ node: ContentGroup,
+ pageContext: ContentPage,
+ childrenCallback: FlowContent.() -> Unit
+ ) {
+ div("platform-tagged") {
+ node.sourceSets.forEach {
+ div("platform-tag") {
+ if (it.sourceSetName.equals("common", ignoreCase = true)) classes = classes + "common"
+ text(it.sourceSetName)
+ }
+ }
+ div("content") {
+ childrenCallback()
+ }
+ }
+ }
+
+ override fun FlowContent.buildPlatformDependent(content: PlatformHintedContent, pageContext: ContentPage) =
+ buildPlatformDependent(content.sourceSets.map { it to setOf(content.inner) }.toMap(), pageContext)
+
+ private fun FlowContent.buildPlatformDependent(
+ nodes: Map<SourceSetData, Collection<ContentNode>>,
+ pageContext: ContentPage
+ ) {
div("platform-hinted") {
attributes["data-platform-hinted"] = "data-platform-hinted"
- val contents = content.sourceSets.mapIndexed { index, platform ->
- platform to createHTML(prettyPrint = false).div(classes = "content") {
+ val contents = nodes.toList().mapIndexed { index, (sourceSet, elements) ->
+ sourceSet to createHTML(prettyPrint = false).div(classes = "content") {
if (index == 0) attributes["data-active"] = ""
- attributes["data-togglable"] = platform.sourceSetName
- buildContentNode(content.inner, pageContext, platform)
+ attributes["data-togglable"] = sourceSet.sourceSetName
+ elements.forEach {
+ buildContentNode(it, pageContext, setOf(sourceSet))
+ }
}
}
@@ -69,17 +95,61 @@ open class HtmlRenderer(
}
}
+ override fun FlowContent.buildDivergent(node: ContentDivergentGroup, pageContext: ContentPage) {
+ val distinct =
+ node.children.map { instance ->
+ instance to Pair(
+ createHTML(prettyPrint = false).div {
+ instance.before?.let { before ->
+ buildContentNode(before, pageContext, instance.sourceSets)
+ }
+ }.drop(5).dropLast(6),
+ createHTML(prettyPrint = false).div {
+ instance.after?.let { after ->
+ buildContentNode(after, pageContext, instance.sourceSets)
+ }
+ }.drop(5).dropLast(6) // TODO: Find a way to do it without arbitrary trims
+ )
+
+ }.groupBy(
+ Pair<ContentDivergentInstance, Pair<String, String>>::second,
+ Pair<ContentDivergentInstance, Pair<String, String>>::first
+ )
+
+ distinct.forEach {
+ consumer.onTagContentUnsafe { +it.key.first }
+ consumer.onTagContentUnsafe {
+ +createHTML(prettyPrint = false).div {
+ if (node.implicitlySourceSetHinted) {
+ buildPlatformDependent(
+ it.value.groupBy { it.sourceSets }
+ .flatMap { (sourceSets, elements) ->
+ sourceSets.map { sourceSet -> sourceSet to elements.map { e -> e.divergent } }
+ }.toMap(),
+ pageContext
+ )
+ } else {
+ it.value.forEach {
+ buildContentNode(it.divergent, pageContext, null)
+ }
+ }
+ }.drop(5).dropLast(6)
+ }
+ consumer.onTagContentUnsafe { +it.key.second }
+ }
+ }
+
override fun FlowContent.buildList(
node: ContentList,
pageContext: ContentPage,
- sourceSetRestriction: SourceSetData?
+ sourceSetRestriction: Set<SourceSetData>?
) = if (node.ordered) ol { buildListItems(node.children, pageContext, sourceSetRestriction) }
else ul { buildListItems(node.children, pageContext, sourceSetRestriction) }
open fun OL.buildListItems(
items: List<ContentNode>,
pageContext: ContentPage,
- sourceSetRestriction: SourceSetData? = null
+ sourceSetRestriction: Set<SourceSetData>? = null
) {
items.forEach {
if (it is ContentList)
@@ -92,7 +162,7 @@ open class HtmlRenderer(
open fun UL.buildListItems(
items: List<ContentNode>,
pageContext: ContentPage,
- sourceSetRestriction: SourceSetData? = null
+ sourceSetRestriction: Set<SourceSetData>? = null
) {
items.forEach {
if (it is ContentList)
@@ -119,18 +189,18 @@ open class HtmlRenderer(
private fun FlowContent.buildRow(
node: ContentGroup,
pageContext: ContentPage,
- sourceSetRestriction: SourceSetData?
+ sourceSetRestriction: Set<SourceSetData>?
) {
node.children
.filter {
- sourceSetRestriction == null || sourceSetRestriction in it.sourceSets
+ sourceSetRestriction == null || it.sourceSets.any { s -> s in sourceSetRestriction }
}
.takeIf { it.isNotEmpty() }
?.let {
div(classes = "table-row") {
it.filter { it.dci.kind != ContentKind.Symbol }.takeIf { it.isNotEmpty() }?.let {
div("main-subrow ${node.style.joinToString { it.toString().decapitalize() }}") {
- it.filter { sourceSetRestriction == null || sourceSetRestriction in it.sourceSets }
+ it.filter { sourceSetRestriction == null || it.sourceSets.any { s -> s in sourceSetRestriction } }
.forEach {
when(it.dci.kind){
ContentKind.SourceSetDependantHint -> {
@@ -170,6 +240,7 @@ open class HtmlRenderer(
}
}
+
private fun FlowContent.createPlatformTags(node: ContentNode) {
div("platform-tags") {
node.sourceSets.forEach {
@@ -184,7 +255,7 @@ open class HtmlRenderer(
override fun FlowContent.buildTable(
node: ContentTable,
pageContext: ContentPage,
- sourceSetRestriction: SourceSetData?
+ sourceSetRestriction: Set<SourceSetData>?
) {
div(classes = "table") {
node.children.forEach {