aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt
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/renderers/html/SearchbarDataInstaller.kt
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/renderers/html/SearchbarDataInstaller.kt')
-rw-r--r--plugins/base/src/main/kotlin/renderers/html/SearchbarDataInstaller.kt69
1 files changed, 69 insertions, 0 deletions
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