blob: 17b5062760a5935a4b97e866043043edc7330307 [file] [log] [blame]
package org.jetbrains.dokka.Model
import org.jsoup.helper.StringUtil.isWhitespace
import org.jsoup.nodes.TextNode
class CodeNode(text: String, baseUri: String): TextNode(text, baseUri) {
override fun text(): String {
return normaliseInitialWhitespace(wholeText.removePrefix("<code>")
.removeSuffix("</code>"))
}
private fun normaliseInitialWhitespace(text: String): String {
val sb = StringBuilder(text.length)
removeInitialWhitespace(sb, text)
return sb.toString()
}
/**
* Remove initial whitespace.
* @param accum builder to append to
* @param string string to remove the initial whitespace
*/
private fun removeInitialWhitespace(accum: StringBuilder, string: String) {
var reachedNonWhite = false
val len = string.length
var c: Int
var i = 0
while (i < len) {
c = string.codePointAt(i)
if (isWhitespace(c) && !reachedNonWhite) {
i += Character.charCount(c)
continue
} else {
accum.appendCodePoint(c)
reachedNonWhite = true
}
i += Character.charCount(c)
}
}
}