aboutsummaryrefslogtreecommitdiff
path: root/plugins/javadoc/src
diff options
context:
space:
mode:
authorsebastian.sellmair <sebastian.sellmair@jetbrains.com>2020-06-30 08:03:38 +0200
committerSebastian Sellmair <34319766+sellmair@users.noreply.github.com>2020-07-01 09:43:03 +0200
commita16e101fad10d024a84aa36069ec6032fcbaf3e6 (patch)
tree64e19695dccd67c3e1b1621eeac2ad469e01cb46 /plugins/javadoc/src
parent14e71e40dfab5f18ccd56b0c015383c0dfb5df51 (diff)
downloaddokka-a16e101fad10d024a84aa36069ec6032fcbaf3e6.tar.gz
dokka-a16e101fad10d024a84aa36069ec6032fcbaf3e6.tar.bz2
dokka-a16e101fad10d024a84aa36069ec6032fcbaf3e6.zip
Implement `AbstractJavadocTemplateMapTest` API
Diffstat (limited to 'plugins/javadoc/src')
-rw-r--r--plugins/javadoc/src/main/kotlin/javadoc/JavadocPageCreator.kt2
-rw-r--r--plugins/javadoc/src/main/kotlin/javadoc/renderer/JavadocContentToTemplateMapTranslator.kt23
-rw-r--r--plugins/javadoc/src/main/kotlin/javadoc/renderer/KorteJavadocRenderer.kt16
-rw-r--r--plugins/javadoc/src/main/kotlin/javadoc/utils.kt8
-rw-r--r--plugins/javadoc/src/test/kotlin/javadoc/AbstractJavadocTemplateMapTest.kt69
-rw-r--r--plugins/javadoc/src/test/kotlin/javadoc/JavadocModuleTemplateMapTest.kt22
6 files changed, 124 insertions, 16 deletions
diff --git a/plugins/javadoc/src/main/kotlin/javadoc/JavadocPageCreator.kt b/plugins/javadoc/src/main/kotlin/javadoc/JavadocPageCreator.kt
index 767e0c68..ebe223e5 100644
--- a/plugins/javadoc/src/main/kotlin/javadoc/JavadocPageCreator.kt
+++ b/plugins/javadoc/src/main/kotlin/javadoc/JavadocPageCreator.kt
@@ -163,7 +163,7 @@ open class JavadocPageCreator(
private val firstSentenceRegex = Regex("^((?:[^.?!]|[.!?](?!\\s))*[.!?])")
private inline fun <reified T : TagWrapper> Documentable.findNodeInDocumentation(sourceSetData: DokkaSourceSet?): T? =
- documentation[sourceSetData]?.firstChildOfType<T>()
+ documentation[sourceSetData]?.firstChildOfTypeOrNull<T>()
private fun Documentable.descriptionToContentNodes(sourceSet: DokkaSourceSet? = highestJvmSourceSet) = findNodeInDocumentation<Description>(sourceSet)?.let {
DocTagToContentConverter.buildContent(
diff --git a/plugins/javadoc/src/main/kotlin/javadoc/renderer/JavadocContentToTemplateMapTranslator.kt b/plugins/javadoc/src/main/kotlin/javadoc/renderer/JavadocContentToTemplateMapTranslator.kt
index 50a971ea..c9dfdbfc 100644
--- a/plugins/javadoc/src/main/kotlin/javadoc/renderer/JavadocContentToTemplateMapTranslator.kt
+++ b/plugins/javadoc/src/main/kotlin/javadoc/renderer/JavadocContentToTemplateMapTranslator.kt
@@ -1,6 +1,7 @@
package javadoc.renderer
import javadoc.pages.*
+import javadoc.toNormalized
import org.jetbrains.dokka.Platform
import org.jetbrains.dokka.base.resolvers.local.LocationProvider
import org.jetbrains.dokka.links.DRI
@@ -9,22 +10,23 @@ import org.jetbrains.dokka.model.ImplementedInterfaces
import org.jetbrains.dokka.model.InheritedFunction
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
+import java.nio.file.Paths
internal class JavadocContentToTemplateMapTranslator(
private val locationProvider: LocationProvider,
private val context: DokkaContext,
) {
- fun templateMapForPageNode(node: JavadocPageNode, pathToRoot: String): TemplateMap =
+ fun templateMapForPageNode(node: JavadocPageNode): TemplateMap =
mapOf<String, Any?>(
"docName" to "docName", // todo docname
- "pathToRoot" to pathToRoot,
+ "pathToRoot" to pathToRoot(node),
"contextRoot" to node,
"kind" to "main",
) + templateMapForNode(node)
- fun templateMapForNode(node: JavadocPageNode): TemplateMap =
+ private fun templateMapForNode(node: JavadocPageNode): TemplateMap =
when (node) {
is JavadocModulePageNode -> InnerTranslator(node).templateMapForJavadocContentNode(node.content)
is JavadocClasslikePageNode -> InnerTranslator(node).templateMapForClasslikeNode(node)
@@ -34,6 +36,21 @@ internal class JavadocContentToTemplateMapTranslator(
else -> emptyMap()
}
+ private fun pathToRoot(node: JavadocPageNode): String {
+ return when(node){
+ is JavadocModulePageNode -> ""
+ else -> run {
+ val link = locationProvider.resolve(node, skipExtension = true)
+ val dir = Paths.get(link).parent?.toNormalized().orEmpty()
+ return dir.split("/").filter { it.isNotEmpty() }.joinToString("/") { ".." }.let {
+ if (it.isNotEmpty()) "$it/" else it
+ }
+ }
+ }
+ }
+
+ private fun String.toNormalized() = Paths.get(this).toNormalized()
+
private inner class InnerTranslator(val contextNode: PageNode) {
private val htmlTranslator = JavadocContentToHtmlTranslator(locationProvider, context)
diff --git a/plugins/javadoc/src/main/kotlin/javadoc/renderer/KorteJavadocRenderer.kt b/plugins/javadoc/src/main/kotlin/javadoc/renderer/KorteJavadocRenderer.kt
index a7d18100..f7d75924 100644
--- a/plugins/javadoc/src/main/kotlin/javadoc/renderer/KorteJavadocRenderer.kt
+++ b/plugins/javadoc/src/main/kotlin/javadoc/renderer/KorteJavadocRenderer.kt
@@ -4,6 +4,7 @@ import com.soywiz.korte.*
import javadoc.location.JavadocLocationProvider
import javadoc.pages.*
import javadoc.renderer.JavadocContentToHtmlTranslator.Companion.buildLink
+import javadoc.toNormalized
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -62,9 +63,8 @@ class KorteJavadocRenderer(private val outputWriter: OutputWriter, val context:
private fun CoroutineScope.renderModulePageNode(node: JavadocModulePageNode) {
val link = "."
val name = "index"
- val pathToRoot = ""
- val contentMap = contentToTemplateMapTranslator.templateMapForPageNode(node, pathToRoot)
+ val contentMap = contentToTemplateMapTranslator.templateMapForPageNode(node)
writeFromTemplate(outputWriter, "$link/$name".toNormalized(), "tabPage.korte", contentMap.toList())
node.children.forEach { renderNode(it, link) }
@@ -72,12 +72,7 @@ class KorteJavadocRenderer(private val outputWriter: OutputWriter, val context:
private fun CoroutineScope.renderJavadocPageNode(node: JavadocPageNode) {
val link = locationProvider.resolve(node, skipExtension = true)
- val dir = Paths.get(link).parent?.let { it.toNormalized() }.orEmpty()
- val pathToRoot = dir.split("/").filter { it.isNotEmpty() }.joinToString("/") { ".." }.let {
- if (it.isNotEmpty()) "$it/" else it
- }
-
- val contentMap = contentToTemplateMapTranslator.templateMapForPageNode(node, pathToRoot)
+ val contentMap = contentToTemplateMapTranslator.templateMapForPageNode(node)
writeFromTemplate(outputWriter, link, templateForNode(node), contentMap.toList())
node.children.forEach { renderNode(it, link.toNormalized()) }
}
@@ -99,9 +94,6 @@ class KorteJavadocRenderer(private val outputWriter: OutputWriter, val context:
private fun DRI.toLink(context: PageNode? = null) = locationProvider.resolve(this, emptySet(), context)
- private fun Path.toNormalized() = this.normalize().toFile().toString()
- private fun String.toNormalized() = Paths.get(this).toNormalized()
-
private suspend fun OutputWriter.writeHtml(path: String, text: String) = write(path, text, ".html")
private fun CoroutineScope.writeFromTemplate(
writer: OutputWriter,
@@ -194,4 +186,4 @@ class KorteJavadocRenderer(private val outputWriter: OutputWriter, val context:
?.joinToString("\n") ?: throw IllegalStateException("Template not found: $basePath/$template")
}
-} \ No newline at end of file
+}
diff --git a/plugins/javadoc/src/main/kotlin/javadoc/utils.kt b/plugins/javadoc/src/main/kotlin/javadoc/utils.kt
new file mode 100644
index 00000000..94f7c8b4
--- /dev/null
+++ b/plugins/javadoc/src/main/kotlin/javadoc/utils.kt
@@ -0,0 +1,8 @@
+package javadoc
+
+import java.nio.file.Path
+import java.nio.file.Paths
+
+internal fun Path.toNormalized() = this.normalize().toFile().toString()
+
+internal fun String.toNormalized() = Paths.get(this).toNormalized()
diff --git a/plugins/javadoc/src/test/kotlin/javadoc/AbstractJavadocTemplateMapTest.kt b/plugins/javadoc/src/test/kotlin/javadoc/AbstractJavadocTemplateMapTest.kt
new file mode 100644
index 00000000..7b31d064
--- /dev/null
+++ b/plugins/javadoc/src/test/kotlin/javadoc/AbstractJavadocTemplateMapTest.kt
@@ -0,0 +1,69 @@
+package javadoc
+
+import javadoc.pages.JavadocPageNode
+import javadoc.renderer.JavadocContentToTemplateMapTranslator
+import org.jetbrains.dokka.DokkaConfigurationImpl
+import org.jetbrains.dokka.javadoc.JavadocPlugin
+import org.jetbrains.dokka.model.withDescendants
+import org.jetbrains.dokka.pages.RootPageNode
+import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.querySingle
+import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest
+
+internal abstract class AbstractJavadocTemplateMapTest : AbstractCoreTest() {
+ protected var config: DokkaConfigurationImpl = dokkaConfiguration {
+ format = "javadoc"
+ passes {
+ pass {
+ sourceRoots = listOf("src")
+ analysisPlatform = "jvm"
+ }
+ }
+ }
+
+ data class Result(
+ val rootPageNode: RootPageNode,
+ val context: DokkaContext
+ ) {
+
+ val translator: JavadocContentToTemplateMapTranslator by lazy {
+ val locationProvider = context.plugin<JavadocPlugin>()
+ .querySingle { locationProviderFactory }
+ .getLocationProvider(rootPageNode)
+
+ JavadocContentToTemplateMapTranslator(locationProvider, context)
+ }
+
+ val JavadocPageNode.templateMap: Map<String, Any?> get() = translator.templateMapForPageNode(this)
+
+ inline fun <reified T : JavadocPageNode> allPagesOfType(): List<T> {
+ return rootPageNode.withDescendants().filterIsInstance<T>().toList()
+ }
+
+ inline fun <reified T : JavadocPageNode> firstPageOfType(): T {
+ return rootPageNode.withDescendants().filterIsInstance<T>().first()
+ }
+
+ inline fun <reified T : JavadocPageNode> firstPageOfTypeOrNull(): T? {
+ return rootPageNode.withDescendants().filterIsInstance<T>().firstOrNull()
+ }
+
+ inline fun <reified T : JavadocPageNode> singlePageOfType(): T {
+ return rootPageNode.withDescendants().filterIsInstance<T>().single()
+ }
+ }
+
+ fun testTemplateMapInline(
+ query: String,
+ configuration: DokkaConfigurationImpl = config,
+ assertions: Result.() -> Unit
+ ) {
+ testInline(query, configuration) {
+ renderingStage = { rootPageNode, dokkaContext ->
+ Result(rootPageNode, dokkaContext).assertions()
+ }
+ }
+ }
+}
+
diff --git a/plugins/javadoc/src/test/kotlin/javadoc/JavadocModuleTemplateMapTest.kt b/plugins/javadoc/src/test/kotlin/javadoc/JavadocModuleTemplateMapTest.kt
new file mode 100644
index 00000000..732454f7
--- /dev/null
+++ b/plugins/javadoc/src/test/kotlin/javadoc/JavadocModuleTemplateMapTest.kt
@@ -0,0 +1,22 @@
+package javadoc
+
+import javadoc.pages.JavadocModulePageNode
+import org.junit.jupiter.api.Test
+
+internal class JavadocModuleTemplateMapTest : AbstractJavadocTemplateMapTest() {
+
+ @Test
+ fun simpleKotlinExample() {
+ testTemplateMapInline(
+ """
+ /src/source.kt
+ package test
+ class Test
+ """.trimIndent(),
+ config
+ ) {
+ val moduleContentMap = singlePageOfType<JavadocModulePageNode>().templateMap
+ println("module")
+ }
+ }
+}