aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/translators/documentables/briefFromContentNodes.kt
blob: 7ac6763db4c45adf76ac996cb0a769b73a856175 (plain)
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
package org.jetbrains.dokka.base.translators.documentables

import org.jetbrains.dokka.model.withDescendants
import org.jetbrains.dokka.pages.*
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

fun briefFromContentNodes(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): ContentNode =
        if (node is ContentText && !node.isHtml && firstSentenceRegex.containsMatchIn(node.text)) {
            sentenceFound = true
            node.copy(text = firstSentenceRegex.find(node.text)?.value.orEmpty())
        } else if (node is ContentGroup) {
            node.copy(children = node.children.mapNotNull {
                if (!sentenceFound) lookthrough(it) else null
            }, style = node.style - TextStyle.Paragraph)
        } else {
            node
        }
    return description.mapNotNull {
        if (!sentenceFound) lookthrough(it) else null
    }
}

private val ContentText.isHtml
    get() = extra[HtmlContent] != null