Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/kotlin/app/shosetsu/lib/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.google.common.io.Resources
* along with shosetsu-kotlin-lib. If not, see <https://www.gnu.org/licenses/>.
*/

fun Array<Filter<*>>.mapify(): Map<Int, Any> =
fun List<Filter<*>>.mapify(): Map<Int, Any> =
HashMap<Int, Any>().apply hasMap@{
[email protected] {
when (val state = it.state) {
Expand All @@ -28,7 +28,7 @@ fun Array<Filter<*>>.mapify(): Map<Int, Any> =
}
}

fun <T> Array<Filter<T>>.mapifyS(): Map<Int, T> =
fun <T> List<Filter<T>>.mapifyS(): Map<Int, T> =
HashMap<Int, T>().apply hasMap@{
[email protected] {
when (val state = it.state) {
Expand Down
100 changes: 59 additions & 41 deletions src/main/kotlin/app/shosetsu/lib/Filter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ package app.shosetsu.lib
*
* @param state States of the filter stuff
*/
sealed class Filter<T>(val id: Int, val name: String, open var state: T) {
sealed class Filter<T> {
abstract val id: Int
abstract val name: String
abstract var state: T

@Suppress("IMPLICIT_CAST_TO_ANY")
override fun toString(): String =
"Filter(id='$id', name='$name', state=`${
Expand All @@ -41,48 +45,63 @@ sealed class Filter<T>(val id: Int, val name: String, open var state: T) {
* Represents a header, used to separate different parts of filters/settings
* Includes [Separator]
*/
class Header(name: String) : Filter<Unit>(-1, name, Unit)
class Header(override val name: String) : Filter<Unit>() {
override var state: Unit = Unit
override val id: Int = -1
}

/**
* Divides parts of the filters/settings
*/
class Separator : Filter<Unit>(-1, "", Unit)
class Separator : Filter<Unit>() {
override var state: Unit = Unit
override val id: Int = -1
override val name: String = ""
}

/**
* Input for text
* Includes [Separator]
*/
class Text(id: Int, name: String) : Filter<String>(id, name, "")
data class Text(
override val id: Int,
override val name: String,
override var state: String = ""
) : Filter<String>()


/**
* Input for boolean option
* Includes [Separator]
*/
class Switch(
id: Int,
name: String
) : Filter<Boolean>(id, name, false)
data class Switch(
override val id: Int,
override val name: String,
override var state: Boolean = false
) : Filter<Boolean>()


/**
* Input for boolean option
* Includes [Separator]
*/
class Checkbox(
id: Int,
name: String
) : Filter<Boolean>(id, name, false)
data class Checkbox(
override val id: Int,
override val name: String,
override var state: Boolean = false
) : Filter<Boolean>()


/**
* Input for ternary value
* Includes [Separator]
*/
class TriState(
id: Int,
name: String
) : Filter<Int>(id, name, STATE_IGNORED) {
data class TriState(
override val id: Int,
override val name: String,
override var state: Int = STATE_IGNORED
) : Filter<Int>() {

companion object {
const val STATE_IGNORED = 0
const val STATE_INCLUDE = 1
Expand All @@ -95,23 +114,24 @@ sealed class Filter<T>(val id: Int, val name: String, open var state: T) {
* Input for a choice from a list
* Includes [Separator]
*/
class Dropdown(
id: Int,
name: String,
val choices: Array<String>
) : Filter<Int>(id, name, 0)
data class Dropdown(
override val id: Int,
override val name: String,
val choices: List<String>,
override var state: Int = 0
) : Filter<Int>()


/**
* Input for a choice from a list
* Includes [Separator]
*/
class RadioGroup(
id: Int,
name: String,
val choices: Array<String>
) : Filter<Int>(id, name, 0)

data class RadioGroup(
override val id: Int,
override val name: String,
val choices: List<String>,
override var state: Int = 0
) : Filter<Int>()

// Grouping

Expand All @@ -120,26 +140,24 @@ sealed class Filter<T>(val id: Int, val name: String, open var state: T) {
* Includes [Separator]
* @param filters Filters present
*/
class List(
name: String,
val filters: Array<Filter<*>>
) : Filter<Map<Int, Any>>(-1, name, filters.mapify()) {
override var state: Map<Int, Any>
get() = filters.mapify()
set(_) {}
data class FList(
override val name: String,
val filters: List<Filter<*>>,
override var state: Map<Int, Any> = filters.mapify()
) : Filter<Map<Int, Any>>() {
override val id: Int = -1
}

/**
* Input for a specific list of filters
* Includes [Separator]
* @param filters Filters present
*/
class Group<T>(
name: String,
val filters: Array<Filter<T>>
) : Filter<Map<Int, T>>(-1, name, filters.mapifyS()) {
override var state: Map<Int, T>
get() = filters.mapifyS()
set(_) {}
data class Group<T>(
override val name: String,
val filters: List<Filter<T>>,
override var state: Map<Int, T> = filters.mapifyS()
) : Filter<Map<Int, T>>() {
override val id: Int = -1
}
}
20 changes: 12 additions & 8 deletions src/main/kotlin/app/shosetsu/lib/lua/ShosetsuLuaLib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,26 @@ class ShosetsuLuaLib : TwoArgFunction() {
name: String,
choices: Array<String>
): Filter.Dropdown =
Filter.Dropdown(id, name, choices)
Filter.Dropdown(id, name, choices.toList())

/** [app.shosetsu.lib.Filter.RadioGroup] Constructor */
fun RadioGroupFilter(
id: Int,
name: String,
choices: Array<String>
): Filter.RadioGroup =
Filter.RadioGroup(id, name, choices)
Filter.RadioGroup(id, name, choices.toList())

/** [app.shosetsu.lib.Filter.List] Constructor */
fun FilterList(name: String, filters: Array<Filter<*>>): Filter.List =
Filter.List(name, filters)
fun FilterList(name: String, filters: Array<Filter<*>>): Filter.FList =
Filter.FList(name, filters.toList())

/** [app.shosetsu.lib.Filter.Group] Constructor */
fun <T> FilterGroup(
name: String,
filters: Array<Filter<T>>
): Filter.Group<T> =
Filter.Group(name, filters)
Filter.Group(name, filters.toList())


fun _GET(
Expand Down Expand Up @@ -201,7 +201,10 @@ class ShosetsuLuaLib : TwoArgFunction() {
}

node.attributes().forEach {
if (remove_style_attr && it.key == "style" || !keep_scripts && it.key.startsWith("on"))
if (remove_style_attr && it.key == "style" || !keep_scripts && it.key.startsWith(
"on"
)
)
node.removeAttr(it.key)
}
}
Expand All @@ -210,11 +213,12 @@ class ShosetsuLuaLib : TwoArgFunction() {
})
toRemove.forEach { it.remove() }

val head = if (custom_style != "") "<style type=\"text/css\">$custom_style</style>" else ""
val head =
if (custom_style != "") "<style type=\"text/css\">$custom_style</style>" else ""
return "<!DOCTYPE html><html><head>$head</head><body>${elem.outerHtml()}</body></html>"
}

fun Document(str: String): Document = Jsoup.parse(str)!!
fun Document(str: String): Document = Jsoup.parse(str)
fun Request(req: Request): Response = ShosetsuSharedLib.httpClient.newCall(req).execute()

@Throws(HTTPException::class)
Expand Down