aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin
diff options
context:
space:
mode:
authorFilip Zybała <48246851+pikinier20@users.noreply.github.com>2020-08-17 12:38:55 +0200
committerGitHub <noreply@github.com>2020-08-17 12:38:55 +0200
commitc49ebd73257ecb912589a50fc896a1d782aa916f (patch)
tree3d9b816635ea6b4819854f3ac1ab982cfdb4f63b /plugins/base/src/main/kotlin
parent9c4dd8d13039a497bf4e93abf2f1a54b183fec93 (diff)
downloaddokka-c49ebd73257ecb912589a50fc896a1d782aa916f.tar.gz
dokka-c49ebd73257ecb912589a50fc896a1d782aa916f.tar.bz2
dokka-c49ebd73257ecb912589a50fc896a1d782aa916f.zip
Fix for creating searchbar pages data - escaping apostrophes (#1233)
Co-authored-by: Filip Zybała <fzybala@virtuslab.com>
Diffstat (limited to 'plugins/base/src/main/kotlin')
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt58
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt69
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt8
3 files changed, 82 insertions, 53 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
index 812776af..8c2e9c9e 100644
--- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt
@@ -23,7 +23,6 @@ import org.jetbrains.dokka.plugability.query
import org.jetbrains.dokka.plugability.querySingle
import java.io.File
import java.net.URI
-import java.util.concurrent.ConcurrentHashMap
open class HtmlRenderer(
context: DokkaContext
@@ -38,10 +37,10 @@ open class HtmlRenderer(
private var shouldRenderSourceSetBubbles: Boolean = false
- private val pageList = ConcurrentHashMap<String, Pair<String, String>>()
-
override val preprocessors = context.plugin<DokkaBase>().query { htmlPreprocessors }
+ val searchbarDataInstaller = SearchbarDataInstaller()
+
private val tabSortingStrategy = context.plugin<DokkaBase>().querySingle { tabSortingStrategy }
private fun <T : ContentNode> sortTabs(strategy: TabSortingStrategy, tabs: Collection<T>): List<T> {
@@ -585,46 +584,11 @@ open class HtmlRenderer(
}
}
- private fun getSymbolSignature(page: ContentPage) = page.content.dfs { it.dci.kind == ContentKind.Symbol }
-
- private fun flattenToText(node: ContentNode): String {
- fun getContentTextNodes(node: ContentNode, sourceSetRestriction: DisplaySourceSet): List<ContentText> =
- when (node) {
- is ContentText -> listOf(node)
- is ContentComposite -> node.children
- .filter { sourceSetRestriction in it.sourceSets }
- .flatMap { getContentTextNodes(it, sourceSetRestriction) }
- .takeIf { node.dci.kind != ContentKind.Annotations }
- .orEmpty()
- else -> emptyList()
- }
-
- val sourceSetRestriction =
- node.sourceSets.find { it.platform == Platform.common } ?: node.sourceSets.first()
- return getContentTextNodes(node, sourceSetRestriction).joinToString("") { it.text }
- }
override suspend fun renderPage(page: PageNode) {
super.renderPage(page)
- if (page is ContentPage && page !is ModulePageNode && page !is PackagePageNode) {
- val signature = getSymbolSignature(page)
- val textNodes = signature?.let { flattenToText(it) }
- val documentable = page.documentable
- if (documentable != null) {
- listOf(
- documentable.dri.packageName,
- documentable.dri.classNames,
- documentable.dri.callable?.name
- )
- .filter { !it.isNullOrEmpty() }
- .takeIf { it.isNotEmpty() }
- ?.joinToString(".")
- ?.let {
- pageList.put(it, Pair(textNodes ?: page.name, locationProvider.resolve(page)))
- }
-
- }
- }
+ if (page is ContentPage && page !is ModulePageNode && page !is PackagePageNode)
+ searchbarDataInstaller.processPage(page, locationProvider.resolve(page))
}
override fun FlowContent.buildText(textNode: ContentText) =
@@ -637,24 +601,12 @@ open class HtmlRenderer(
else -> text(textNode.text)
}
- private fun generatePagesList() =
- pageList.entries
- .filter { !it.key.isNullOrEmpty() }
- .groupBy { it.key.substringAfterLast(".") }
- .entries
- .flatMapIndexed { topLevelIndex, entry ->
- entry.value.mapIndexed { index, subentry ->
- "{\'name\': \'${subentry.value.first}\', \'description\':\'${subentry.key}\', \'location\':\'${subentry.value.second}\', 'searchKey':'${entry.key}'}"
- }
- }
- .joinToString(prefix = "[", separator = ",\n", postfix = "]")
-
override fun render(root: RootPageNode) {
shouldRenderSourceSetBubbles = shouldRenderSourceSetBubbles(root)
super.render(root)
runBlocking(Dispatchers.Default) {
launch {
- outputWriter.write("scripts/pages", "var pages = ${generatePagesList()}", ".js")
+ outputWriter.write("scripts/pages", "var pages = ${searchbarDataInstaller.generatePagesList()}", ".js")
}
}
}
diff --git a/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt
new file mode 100644
index 00000000..cd95013a
--- /dev/null
+++ b/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt
@@ -0,0 +1,69 @@
+package org.jetbrains.dokka.base.renderers.html
+
+import org.jetbrains.dokka.DokkaConfiguration
+import org.jetbrains.dokka.Platform
+import org.jetbrains.dokka.base.resolvers.local.LocationProvider
+import org.jetbrains.dokka.model.DisplaySourceSet
+import org.jetbrains.dokka.model.dfs
+import org.jetbrains.dokka.pages.*
+
+class SearchbarDataInstaller() {
+ private val pageList = mutableMapOf<String, Pair<String, String>>()
+
+ private fun String.escaped(): String = this.replace("'","\\'")
+
+ fun generatePagesList(): String {
+ return pageList.entries
+ .filter { it.key.isNotEmpty() }
+ .groupBy { it.key.substringAfterLast(".") }
+ .entries
+ .flatMapIndexed { topLevelIndex, entry ->
+ entry.value.mapIndexed { index, subentry ->
+ "{\'name\': \'${subentry.value.first.escaped()}\', \'description\':\'${subentry.key.escaped()}\', \'location\':\'${subentry.value.second.escaped()}\', 'searchKey':'${entry.key.escaped()}'}"
+ }
+ }
+ .joinToString(prefix = "[", separator = ",\n", postfix = "]")
+ }
+
+
+ private fun getSymbolSignature(page: ContentPage) = page.content.dfs { it.dci.kind == ContentKind.Symbol }
+
+ private fun flattenToText(node: ContentNode): String {
+ fun getContentTextNodes(node: ContentNode, sourceSetRestriction: DisplaySourceSet): List<ContentText> =
+ when (node) {
+ is ContentText -> listOf(node)
+ is ContentComposite -> node.children
+ .filter { sourceSetRestriction in it.sourceSets }
+ .flatMap { getContentTextNodes(it, sourceSetRestriction) }
+ .takeIf { node.dci.kind != ContentKind.Annotations }
+ .orEmpty()
+ else -> emptyList()
+ }
+
+ val sourceSetRestriction =
+ node.sourceSets.find { it.platform == Platform.common } ?: node.sourceSets.first()
+ return getContentTextNodes(node, sourceSetRestriction).joinToString("") { it.text }
+ }
+
+ fun processPage(page: ContentPage, link: String) {
+ val signature = getSymbolSignature(page)
+ val textNodes = signature?.let { flattenToText(it) }
+ val documentable = page.documentable
+ if (documentable != null) {
+ listOf(
+ documentable.dri.packageName,
+ documentable.dri.classNames,
+ documentable.dri.callable?.name
+ )
+ .filter { !it.isNullOrEmpty() }
+ .takeIf { it.isNotEmpty() }
+ ?.joinToString(".")
+ ?.let {
+ pageList.put(it, Pair(textNodes ?: page.name, link))
+ }
+
+ }
+ }
+
+
+} \ No newline at end of file
diff --git a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
index 70878bbe..407e5899 100644
--- a/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
+++ b/plugins/base/src/main/kotlin/renderers/html/htmlPreprocessors.kt
@@ -4,11 +4,17 @@ import kotlinx.html.h1
import kotlinx.html.id
import kotlinx.html.table
import kotlinx.html.tbody
+import org.jetbrains.dokka.DokkaConfiguration
+import org.jetbrains.dokka.Platform
+import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.renderers.sourceSets
+import org.jetbrains.dokka.base.resolvers.local.LocationProvider
import org.jetbrains.dokka.model.DEnum
import org.jetbrains.dokka.model.DEnumEntry
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
+import org.jetbrains.dokka.plugability.plugin
+import org.jetbrains.dokka.plugability.querySingle
import org.jetbrains.dokka.transformers.pages.PageTransformer
@@ -107,3 +113,5 @@ class SourcesetDependencyAppender(val context: DokkaContext) : PageTransformer {
)
}
}
+
+