1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package org.jetbrains.dokka.base.translators.documentables
import org.jetbrains.dokka.model.doc.*
import org.jetbrains.dokka.model.withDescendants
import org.jetbrains.dokka.pages.*
import org.jetbrains.kotlin.util.firstNotNullResult
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
fun firstParagraphBrief(docTag: DocTag): DocTag? =
when(docTag){
is P -> docTag
is CustomDocTag -> docTag.children.firstNotNullResult { firstParagraphBrief(it) }
is Text -> docTag
else -> null
}
fun firstSentenceBriefFromContentNodes(description: List<ContentNode>): List<ContentNode> {
val firstSentenceRegex = """^((?:[^.?!]|[.!?](?!\s))*[.!?])""".toRegex()
//Description that is entirely based on html content. In html it is hard to define a brief so we render all of it
if(description.all { it.withDescendants().all { it is ContentGroup || it.safeAs<ContentText>()?.isHtml == true } }){
return description
}
var sentenceFound = false
fun lookthrough(node: ContentNode, neighbours: List<ContentNode>, currentIndex: Int): ContentNode =
if (node.finishesWithSentenceNotFollowedByHtml(firstSentenceRegex, neighbours, currentIndex) || node.containsSentenceFinish(firstSentenceRegex)) {
node as ContentText
sentenceFound = true
node.copy(text = firstSentenceRegex.find(node.text)?.value.orEmpty())
} else if (node is ContentGroup) {
node.copy(children = node.children.mapIndexedNotNull { i, element ->
if (!sentenceFound) lookthrough(element, node.children, i) else null
}, style = node.style - TextStyle.Paragraph)
} else {
node
}
return description.mapIndexedNotNull { i, element ->
if (!sentenceFound) lookthrough(element, description, i) else null
}
}
private fun ContentNode.finishesWithSentenceNotFollowedByHtml(firstSentenceRegex: Regex, neighbours: List<ContentNode>, currentIndex: Int): Boolean =
this is ContentText && !isHtml && matchContainsEnd(this, firstSentenceRegex) && !neighbours.nextElementIsHtml(currentIndex)
private fun ContentNode.containsSentenceFinish(firstSentenceRegex: Regex): Boolean =
this is ContentText && !isHtml && firstSentenceRegex.containsMatchIn(text) && !matchContainsEnd(this, firstSentenceRegex)
private fun matchContainsEnd(node: ContentText, regex: Regex): Boolean =
regex.find(node.text)?.let { node.text.endsWith(it.value) } ?: false
private fun List<ContentNode>.nextElementIsHtml(currentElementIndex: Int): Boolean =
currentElementIndex != lastIndex && get(currentElementIndex + 1).isHtml
private val ContentNode.isHtml
get() = extra[HtmlContent] != null
|