80 lines
1.5 KiB
Kotlin
80 lines
1.5 KiB
Kotlin
package nl.astraeus.vst.ui.css
|
|
|
|
import nl.astraeus.css.style.DescriptionProvider
|
|
import nl.astraeus.css.style.cls
|
|
|
|
private val CAPITAL_LETTER = Regex("[A-Z]")
|
|
|
|
fun String.hyphenize(): String {
|
|
var result = replace(CAPITAL_LETTER) {
|
|
"-${it.value.lowercase()}"
|
|
}
|
|
|
|
if (result.startsWith('-')) {
|
|
result = result.substring(1)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
private var nextCssId = 1
|
|
|
|
object CssSettings {
|
|
var preFix = "css"
|
|
var shortId = false
|
|
var minified = false
|
|
}
|
|
|
|
private fun nextShortId(): String {
|
|
var id = nextCssId++
|
|
val result = StringBuilder()
|
|
|
|
while (id > 0) {
|
|
val ch = ((id % 26) + 'a'.code).toChar()
|
|
result.append(ch)
|
|
|
|
id /= 26
|
|
}
|
|
|
|
return result.toString()
|
|
}
|
|
|
|
interface CssName : DescriptionProvider {
|
|
val name: String
|
|
get() = if (CssSettings.shortId) {
|
|
nextShortId()
|
|
} else {
|
|
"${CssSettings.preFix}-${this::class.simpleName?.hyphenize() ?: this::class}"
|
|
}
|
|
|
|
fun cls(): DescriptionProvider = cls(this)
|
|
|
|
fun cssName(): String = "${this::class.simpleName?.hyphenize() ?: this::class}"
|
|
|
|
override fun description() = name
|
|
}
|
|
|
|
open class CssId(name: String) : DescriptionProvider {
|
|
val name: String = if (CssSettings.shortId) {
|
|
nextShortId()
|
|
} else {
|
|
"daw-$name-css"
|
|
}
|
|
|
|
override fun description() = name
|
|
|
|
override fun equals(other: Any?): Boolean {
|
|
if (this === other) return true
|
|
if (other !is CssId) return false
|
|
|
|
if (name != other.name) return false
|
|
|
|
return true
|
|
}
|
|
|
|
override fun hashCode(): Int {
|
|
return name.hashCode()
|
|
}
|
|
|
|
}
|