Skip to content

Commit

Permalink
Custom dokka multi platform labels (#2480)
Browse files Browse the repository at this point in the history
* Adds overview box to core quickstart and changing side-bar menu accordingly

* Adds overview box to optics quickstart (needs restyling and css id reassignments cc @israelperezglez)

* customizes mpp labels (not yet working)

* add class per platform ex. class="platform-common"
  • Loading branch information
raulraja authored Sep 8, 2021
1 parent 48c2bfb commit c544d83
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ import org.jetbrains.dokka.base.transformers.pages.comments.DocTagToContentConve
import org.jetbrains.dokka.gfm.GfmPlugin
import org.jetbrains.dokka.gfm.renderer.BriefCommentPreprocessor
import org.jetbrains.dokka.gfm.renderer.CommonmarkRenderer
import org.jetbrains.dokka.model.DisplaySourceSet
import org.jetbrains.dokka.model.doc.CodeBlock
import org.jetbrains.dokka.model.doc.DocTag
import org.jetbrains.dokka.model.properties.PropertyContainer
import org.jetbrains.dokka.model.toDisplaySourceSets
import org.jetbrains.dokka.pages.ContentCodeBlock
import org.jetbrains.dokka.pages.ContentCodeInline
import org.jetbrains.dokka.pages.ContentDivergentGroup
import org.jetbrains.dokka.pages.ContentDivergentInstance
import org.jetbrains.dokka.pages.ContentGroup
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.ContentPage
import org.jetbrains.dokka.pages.ContentTable
import org.jetbrains.dokka.pages.DCI
import org.jetbrains.dokka.pages.PlatformHintedContent
import org.jetbrains.dokka.pages.SimpleAttr
import org.jetbrains.dokka.pages.Style
import org.jetbrains.dokka.plugability.DokkaContext
Expand Down Expand Up @@ -91,6 +97,98 @@ public class JekyllRenderer(context: DokkaContext) : CommonmarkRenderer(context)
return builder.toString()
}

override fun StringBuilder.buildDivergent(node: ContentDivergentGroup, pageContext: ContentPage) {

val distinct =
node.groupDivergentInstances(pageContext, { instance, contentPage, sourceSet ->
instance.before?.let { before ->
buildString { buildContentNode(before, pageContext, sourceSet) }
} ?: ""
}, { instance, contentPage, sourceSet ->
instance.after?.let { after ->
buildString { buildContentNode(after, pageContext, sourceSet) }
} ?: ""
})

distinct.values.forEach { entry ->
val (instance, sourceSets) = entry.getInstanceAndSourceSets()

buildParagraph()
buildSourceSetTags(sourceSets)
buildNewLine()

instance.before?.let {
buildContentNode(
it,
pageContext,
sourceSets.first()
) // It's workaround to render content only once
buildParagraph()
}

entry.groupBy { buildString { buildContentNode(it.first.divergent, pageContext, setOf(it.second)) } }
.values.forEach { innerEntry ->
val (innerInstance, innerSourceSets) = innerEntry.getInstanceAndSourceSets()
if (sourceSets.size > 1) {
buildSourceSetTags(innerSourceSets)
buildNewLine()
}
innerInstance.divergent.build(
this@buildDivergent,
pageContext,
setOf(innerSourceSets.first())
) // It's workaround to render content only once
buildParagraph()
}

instance.after?.let {
buildContentNode(
it,
pageContext,
sourceSets.first()
) // It's workaround to render content only once
}

buildParagraph()
}
}

private fun List<Pair<ContentDivergentInstance, DisplaySourceSet>>.getInstanceAndSourceSets() =
this.let { Pair(it.first().first, it.map { it.second }.toSet()) }

override fun StringBuilder.buildPlatformDependent(
content: PlatformHintedContent,
pageContext: ContentPage,
sourceSetRestriction: Set<DisplaySourceSet>?
) {
buildPlatformDependentItem(content.inner, content.sourceSets, pageContext)
}

private fun StringBuilder.buildPlatformDependentItem(
content: ContentNode,
sourceSets: Set<DisplaySourceSet>,
pageContext: ContentPage,
) {
if (content is ContentGroup && content.children.firstOrNull { it is ContentTable } != null) {
buildContentNode(content, pageContext, sourceSets)
} else {
val distinct = sourceSets.map {
it to buildString { buildContentNode(content, pageContext, setOf(it)) }
}.groupBy(Pair<DisplaySourceSet, String>::second, Pair<DisplaySourceSet, String>::first)

distinct.filter { it.key.isNotBlank() }.forEach { (text, platforms) ->
buildParagraph()
buildSourceSetTags(platforms.toSet())
buildNewLine()
append(text.trim())
buildParagraph()
}
}
}

private fun StringBuilder.buildSourceSetTags(sourceSets: Set<DisplaySourceSet>) =
sourceSets.forEach { append("""<span class="platform-${it.name}">${it.name}</span>""") }

override fun StringBuilder.buildCodeBlock(code: ContentCodeBlock, pageContext: ContentPage) {
append("```${code.language}\n")
code.children.forEach { it.build(this, pageContext) }
Expand Down

0 comments on commit c544d83

Please sign in to comment.