Skip to content

Commit

Permalink
Add information about content kind and sourceset to anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinAman authored and kamildoleglo committed Oct 8, 2020
1 parent b7dfbf3 commit 2c9e112
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class BasicGradleIntegrationTest(override val versions: BuildVersions) : Abstrac
"Expected `SampleJavaClass` source link to GitHub"
)

val anchorsShouldNotHaveHashes = "data-name=\".*#.*\"".toRegex()
val anchorsShouldNotHaveHashes = "<a data-name=\".*#.*\"\\sanchor-label=\"*.*\">".toRegex()
assertTrue(
allHtmlFiles().all { file ->
!anchorsShouldNotHaveHashes.containsMatchIn(file.readText())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MultimodulePageCreator(
val displayedModuleDocumentation = getDisplayedModuleDocumentation(module)
val dri = DRI(packageName = MULTIMODULE_PACKAGE_PLACEHOLDER, classNames = module.name)
val dci = DCI(setOf(dri), ContentKind.Comment)
val extraWithAnchor = PropertyContainer.withAll(SymbolAnchorHint(module.name))
val extraWithAnchor = PropertyContainer.withAll(SymbolAnchorHint(module.name, ContentKind.Main))
val header = linkNode(module.name, dri, DCI(setOf(dri), ContentKind.Main), extra = extraWithAnchor)
val content = ContentGroup(
children =
Expand Down
4 changes: 2 additions & 2 deletions plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ val ContentNode.isAnchorable: Boolean
get() = anchorLabel != null

val ContentNode.anchorLabel: String?
get() = extra[SymbolAnchorHint.SymbolAnchorHintKey]?.anchorName
get() = extra[SymbolAnchorHint]?.anchorName

val ContentNode.anchor: String
get() = dci.dri.first().toString().urlEncoded()
get() = (dci.dri.first().toString() + "/" + extra[SymbolAnchorHint]?.contentKind + "/" + sourceSets.joinToString { it.sourceSetIDs.all.joinToString() }).urlEncoded()
14 changes: 8 additions & 6 deletions plugins/base/src/main/kotlin/resolvers/anchors/AnchorsHint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package org.jetbrains.dokka.base.resolvers.anchors
import org.jetbrains.dokka.model.Documentable
import org.jetbrains.dokka.model.properties.ExtraProperty
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.Kind

data class SymbolAnchorHint(val anchorName: String): ExtraProperty<ContentNode> {
object SymbolAnchorHintKey : ExtraProperty.Key<ContentNode, SymbolAnchorHint>
override val key: ExtraProperty.Key<ContentNode, SymbolAnchorHint> = SymbolAnchorHintKey
companion object: ExtraProperty.Key<ContentNode, SymbolAnchorHint> {
fun from(d: Documentable): SymbolAnchorHint? = d.name?.let { SymbolAnchorHint(it) }
data class SymbolAnchorHint(val anchorName: String, val contentKind: Kind) : ExtraProperty<ContentNode> {
override val key: ExtraProperty.Key<ContentNode, SymbolAnchorHint> = SymbolAnchorHint

companion object : ExtraProperty.Key<ContentNode, SymbolAnchorHint> {
fun from(d: Documentable, contentKind: Kind): SymbolAnchorHint? =
d.name?.let { SymbolAnchorHint(it, contentKind) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class DokkaLocationProvider(
pageGraphRoot.withDescendants().filterIsInstance<ContentPage>()
.flatMap { page ->
page.content.withDescendants()
.filter { it.extra[SymbolAnchorHint.SymbolAnchorHintKey] != null }
.filter { it.extra[SymbolAnchorHint] != null }
.mapNotNull { it.dci.dri.singleOrNull() }
.distinct()
.flatMap { dri ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SourceLinksTransformer(val context: DokkaContext, val builder: PageContent
+ContentTable(
emptyList(),
sources.map {
buildGroup(node.dri, setOf(it.first), kind = ContentKind.Source, extra = mainExtra + SymbolAnchorHint(it.second)) {
buildGroup(node.dri, setOf(it.first), kind = ContentKind.Source, extra = mainExtra + SymbolAnchorHint(it.second, ContentKind.Source)) {
link("(source)", it.second)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ open class DefaultPageCreator(
.groupBy({ it.second }, { it.first }).map { (classlike, platforms) ->
val label = classlike.classNames?.substringBeforeLast(".") ?: classlike.toString()
.also { logger.warn("No class name found for DRI $classlike") }
buildGroup(setOf(dri), platforms.toSet(), ContentKind.Inheritors, extra = mainExtra + SymbolAnchorHint(label)) {
buildGroup(setOf(dri), platforms.toSet(), ContentKind.Inheritors, extra = mainExtra + SymbolAnchorHint(label, ContentKind.Inheritors)) {
link(label, classlike)
}
},
Expand Down Expand Up @@ -529,7 +529,7 @@ open class DefaultPageCreator(
sourceSets = elements.flatMap { it.sourceSets }.toSet(),
kind = kind,
styles = emptySet(),
extra = elementName?.let { name -> extra + SymbolAnchorHint(name) } ?: extra
extra = elementName?.let { name -> extra + SymbolAnchorHint(name, kind) } ?: extra
) {
link(elementName.orEmpty(), elements.first().dri, kind = kind)
divergentGroup(
Expand All @@ -539,7 +539,7 @@ open class DefaultPageCreator(
extra = extra
) {
elements.map {
instance(setOf(it.dri), it.sourceSets.toSet(), extra = PropertyContainer.withAll(SymbolAnchorHint(it.name ?: ""))) {
instance(setOf(it.dri), it.sourceSets.toSet(), extra = PropertyContainer.withAll(SymbolAnchorHint(it.name ?: "", kind))) {
before {
contentForBrief(it)
contentForSinceKotlin(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ open class PageContentBuilder(
sourceSets,
kind,
styles,
extra + SymbolAnchorHint(text.replace("\\s".toRegex(), "").toLowerCase())
extra + SymbolAnchorHint(text.replace("\\s".toRegex(), "").toLowerCase(), kind)
) {
text(text, kind = kind)
block()
Expand Down Expand Up @@ -185,7 +185,7 @@ open class PageContentBuilder(
else it
}
.map {
val newExtra = if (needsAnchors) extra + SymbolAnchorHint.from(it) else extra
val newExtra = if (needsAnchors) extra + SymbolAnchorHint.from(it, kind) else extra
buildGroup(setOf(it.dri), it.sourceSets.toSet(), kind, styles, newExtra) {
operation(it)
}
Expand Down

0 comments on commit 2c9e112

Please sign in to comment.