aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/renderers/html/htmlFormatingUtils.kt
blob: b9eab293222d672afd3b3c983c67ea6d3489f75b (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.jetbrains.dokka.base.renderers.html

import kotlinx.html.FlowContent
import kotlinx.html.span

fun FlowContent.buildTextBreakableAfterCapitalLetters(name: String) {
    if (name.contains(" ")) {
        val withOutSpaces = name.split(" ")
        withOutSpaces.dropLast(1).forEach {
            buildBreakableText(it)
            +" "
        }
        buildBreakableText(withOutSpaces.last())
    } else {
        val content = name.replace(Regex("(?!^)([A-Z])"), " $1").split(" ")
        joinToHtml(content) {
            it
        }
    }
}

fun FlowContent.buildBreakableDotSeparatedHtml(name: String) {
    val phrases = name.split(".")
    phrases.forEachIndexed { i, e ->
        if (e.length > 10) {
            buildBreakableText(e)
        } else {
            val elementWithOptionalDot =
                if (i != phrases.lastIndex) {
                    "$e."
                } else {
                    e
                }
            buildBreakableHtmlElement(elementWithOptionalDot)
        }
    }
}

private fun FlowContent.joinToHtml(elements: List<String>, onEach: (String) -> String) {
    elements.dropLast(1).forEach {
        buildBreakableHtmlElement(onEach(it))
    }
    span {
        buildBreakableHtmlElement(elements.last(), last = true)
    }
}

private fun FlowContent.buildBreakableHtmlElement(element: String, last: Boolean = false) {
    span {
        +element
    }
    if (!last) {
        wbr { }
    }
}

fun FlowContent.buildBreakableText(name: String) =
    if (name.contains(".")) buildBreakableDotSeparatedHtml(name)
    else buildTextBreakableAfterCapitalLetters(name)