<commit_before>package ko.html

import kotlin.properties.ReadWriteProperty
import com.google.common.base.CaseFormat
import com.google.common.html.HtmlEscapers

fun String.hyphens(): String {
    return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, this)
}

object attributeHandler : ReadWriteProperty<AbstractElement, String?> {
    override fun get(thisRef: AbstractElement, desc: PropertyMetadata): String? {
        val name = desc.name.hyphens()
        if (thisRef.attributes.containsKey(name)) {
            return thisRef.attributes[name]
        }
        return null
    }

    override fun set(thisRef: AbstractElement, desc: PropertyMetadata, value: String?) {
        val name = desc.name.hyphens()
        if (value != null) {
            thisRef.attributes.put(name, value)
        } else {
            if (thisRef.attributes.containsKey(name)) {
                thisRef.attributes.remove(name)
            }
        }
    }
}

abstract class AbstractElement(val elementName: String) {
    val attributes: MutableMap<String, String> = hashMapOf()

    abstract fun render(builder: StringBuilder)

    override fun toString(): String {
        val builder = StringBuilder()
        render(builder)
        return builder.toString()
    }

    protected fun renderOpen(builder: StringBuilder) {
        builder.append("<").append(elementName)
        attributes
                .map {
                    it.getKey().replaceAll("[_]+$", "") to it.getValue()
                }
                .sortBy { it.first }
                .forEach {
                    val (name, value) = it
                    builder.append(" ").append(name).append("=\"").append(value).append("\"")
                }
        builder.append(">")
    }
}

abstract class FindableElement(name: String, id: String?, class_: String?) : AbstractElement(elementName = name) {
    var id by attributeHandler
    var class_ by attributeHandler

    {
        this.id = id
        this.class_ = class_
    }
}

class TextElement(val text: String) : AbstractElement("TextElement") {
    override fun render(builder: StringBuilder) {
        builder.append(HtmlEscapers.htmlEscaper().escape(text))
    }
}

abstract class SingleElement(name: String, id: String?, class_: String?) : FindableElement(name = name, id = id, class_ = class_) {
    override fun render(builder: StringBuilder) {
        renderOpen(builder)
    }
}

abstract class Element(name: String, id: String?, class_: String?) : FindableElement(name = name, id = id, class_ = class_) {
    protected val children: MutableList<AbstractElement> = arrayListOf()

    override fun render(builder: StringBuilder) {
        renderOpen(builder)
        for (child in children) {
            child.render(builder)
        }
        builder.append("</").append(elementName).append(">")
    }
}

abstract class ContainerElement(name: String, id: String?, class_: String?) : Element(name = name, id = id, class_ = class_) {
    fun h1(id: String? = null, class_: String? = null, text: String? = null): H1 {
        val h1 = H1(id = id, class_ = class_, text = text)
        children.add(h1)
        return h1
    }
}

class Html(lang: String? = null, init: Html.() -> Unit = {}) : Element(name = "html", id = null, class_ = null) {
    var lang by attributeHandler

    override fun render(builder: StringBuilder) {<commit_msg>change string builder to Appendable<commit_after>package ko.html

import kotlin.properties.ReadWriteProperty
import com.google.common.base.CaseFormat
import com.google.common.html.HtmlEscapers

fun String.hyphens(): String {
    return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, this)
}

object attributeHandler : ReadWriteProperty<AbstractElement, String?> {
    override fun get(thisRef: AbstractElement, desc: PropertyMetadata): String? {
        val name = desc.name.hyphens()
        if (thisRef.attributes.containsKey(name)) {
            return thisRef.attributes[name]
        }
        return null
    }

    override fun set(thisRef: AbstractElement, desc: PropertyMetadata, value: String?) {
        val name = desc.name.hyphens()
        if (value != null) {
            thisRef.attributes.put(name, value)
        } else {
            if (thisRef.attributes.containsKey(name)) {
                thisRef.attributes.remove(name)
            }
        }
    }
}

abstract class AbstractElement(val elementName: String) {
    val attributes: MutableMap<String, String> = hashMapOf()

    abstract fun render(builder: Appendable)

    override fun toString(): String {
        val builder = StringBuilder()
        render(builder)
        return builder.toString()
    }

    protected fun renderOpen(builder: Appendable) {
        builder.append("<").append(elementName)
        attributes
                .map {
                    it.getKey().replaceAll("[_]+$", "") to it.getValue()
                }
                .sortBy { it.first }
                .forEach {
                    val (name, value) = it
                    builder.append(" ").append(name).append("=\"").append(value).append("\"")
                }
        builder.append(">")
    }
}

abstract class FindableElement(name: String, id: String?, class_: String?) : AbstractElement(elementName = name) {
    var id by attributeHandler
    var class_ by attributeHandler

    {
        this.id = id
        this.class_ = class_
    }
}

class TextElement(val text: String) : AbstractElement("TextElement") {
    override fun render(builder: Appendable) {
        builder.append(HtmlEscapers.htmlEscaper().escape(text))
    }
}

abstract class SingleElement(name: String, id: String?, class_: String?) : FindableElement(name = name, id = id, class_ = class_) {
    override fun render(builder: Appendable) {
        renderOpen(builder)
    }
}

abstract class Element(name: String, id: String?, class_: String?) : FindableElement(name = name, id = id, class_ = class_) {
    protected val children: MutableList<AbstractElement> = arrayListOf()

    override fun render(builder: Appendable) {
        renderOpen(builder)
        for (child in children) {
            child.render(builder)
        }
        builder.append("</").append(elementName).append(">")
    }
}

abstract class ContainerElement(name: String, id: String?, class_: String?) : Element(name = name, id = id, class_ = class_) {
    fun h1(id: String? = null, class_: String? = null, text: String? = null): H1 {
        val h1 = H1(id = id, class_ = class_, text = text)
        children.add(h1)
        return h1
    }
}

class Html(lang: String? = null, init: Html.() -> Unit = {}) : Element(name = "html", id = null, class_ = null) {
    var lang by attributeHandler

    override fun render(builder: Appendable) {
{'fraction_non_alphanumeric': 0.12058212058212059, 'fraction_numerical': 0.001782001782001782, 'mean_word_length': 2.634646519158122, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690470', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1878, 'n_words': 476}