diff options
author | vmishenev <vad-mishenev@yandex.ru> | 2021-08-25 17:14:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 16:14:31 +0200 |
commit | 4fa9524e52d8ff422bb355336e3810ab28ae135c (patch) | |
tree | 66d2bc381ffc7d5c598cee4ea9e0ba13f81b1d7b /plugins/base/base-test-utils/src/main | |
parent | 742f96bdf5c0b842e68dfaf43f4ab3446e87e3df (diff) | |
download | dokka-4fa9524e52d8ff422bb355336e3810ab28ae135c.tar.gz dokka-4fa9524e52d8ff422bb355336e3810ab28ae135c.tar.bz2 dokka-4fa9524e52d8ff422bb355336e3810ab28ae135c.zip |
Manual highlighting webhelp (#2079)
* Add manual code highlighting
* Fix test
* Add kotlinAsJava highlighting
* Add runtime highlighting via Prism
* Add copy-button for code block
* Add tests and refactor
* Replace `<br>` for prism.js
* Parse trivial default values
Co-authored-by: Marcin Aman <marcin.aman@gmail.com>
Diffstat (limited to 'plugins/base/base-test-utils/src/main')
-rw-r--r-- | plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt b/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt index ea2d13a7..9e38df10 100644 --- a/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt +++ b/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt @@ -4,12 +4,22 @@ import org.jsoup.nodes.Element import org.jsoup.nodes.Node import org.jsoup.nodes.TextNode -fun Element.match(vararg matchers: Any): Unit = +fun Element.match(vararg matchers: Any, ignoreSpanWithTokenStyle:Boolean = false): Unit = childNodes() - .filter { it !is TextNode || it.text().isNotBlank() } + .let { list -> + if(ignoreSpanWithTokenStyle) { + list + .filterNot { it is Element && it.tagName() == "span" && it.attr("class").startsWith("token ") && it.childNodeSize() == 0} + .map { if(it is Element && it.tagName() == "span" + && it.attr("class").startsWith("token ") + && it.childNodeSize() == 1) it.childNode(0) else it } + .uniteConsecutiveTextNodes() + } else list + } + .filter { (it !is TextNode || it.text().isNotBlank())} .let { it.drop(it.size - matchers.size) } .zip(matchers) - .forEach { (n, m) -> m.accepts(n) } + .forEach { (n, m) -> m.accepts(n, ignoreSpan = ignoreSpanWithTokenStyle) } open class Tag(val name: String, vararg val matchers: Any) class Div(vararg matchers: Any) : Tag("div", *matchers) @@ -20,13 +30,27 @@ class B(vararg matchers: Any) : Tag("b", *matchers) class I(vararg matchers: Any) : Tag("i", *matchers) class STRIKE(vararg matchers: Any) : Tag("strike", *matchers) object Wbr : Tag("wbr") -private fun Any.accepts(n: Node) { +private fun Any.accepts(n: Node, ignoreSpan:Boolean = true) { when (this) { is String -> assert(n is TextNode && n.text().trim() == this.trim()) { "\"$this\" expected but found: $n" } is Tag -> { assert(n is Element && n.tagName() == name) { "Tag $name expected but found: $n" } - if (n is Element && matchers.isNotEmpty()) n.match(*matchers) + if (n is Element && matchers.isNotEmpty()) n.match(*matchers, ignoreSpanWithTokenStyle = ignoreSpan) } else -> throw IllegalArgumentException("$this is not proper matcher") } -}
\ No newline at end of file +} +private fun List<Node>.uniteConsecutiveTextNodes(): MutableList<Node> { + val resList = mutableListOf<Node>() + var acc = StringBuilder() + forEachIndexed { index, item -> + if (item is TextNode) { + acc.append(item.text()) + if (!(index + 1 < size && this[index + 1] is TextNode)) { + resList.add(TextNode(acc.toString())) + acc = StringBuilder() + } + } else resList.add(item) + } + return resList + }
\ No newline at end of file |