Merge remote-tracking branch 'origin/master'

pull/32/head
kunfei 5 years ago
commit f1d07a2caf
  1. 2
      app/src/main/java/io/legado/app/data/entities/Book.kt
  2. 2
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeByJSoup.kt
  3. 21
      app/src/main/java/io/legado/app/utils/GsonExtensions.kt
  4. 35
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -69,7 +69,7 @@ data class Book(
variableMap = if (isEmpty(variable)) { variableMap = if (isEmpty(variable)) {
HashMap() HashMap()
} else { } else {
GSON.fromJsonObject<HashMap<String, String>>(variable!!) GSON.fromJsonObject<HashMap<String, String>>(variable)
} }
} }
} }

@ -414,7 +414,7 @@ class AnalyzeByJSoup {
var replacement = "" var replacement = ""
init { init {
if (startWithIgnoreCase(ruleStr, "@CSS:")) { if (ruleStr.startWithIgnoreCase("@CSS:")) {
isCss = true isCss = true
elementsRule = ruleStr.substring(5).trim { it <= ' ' } elementsRule = ruleStr.substring(5).trim { it <= ' ' }
} else { } else {

@ -3,20 +3,23 @@ package io.legado.app.utils
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import com.google.gson.JsonParser import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import org.jetbrains.anko.attempt import org.jetbrains.anko.attempt
val GSON: Gson by lazy { GsonBuilder().create() } val GSON: Gson by lazy { GsonBuilder().create() }
inline fun <reified T> Gson.fromJsonObject(json: String?): T? = fromJson(json, T::class.java) inline fun <reified T> genericType() = object : TypeToken<T>() {}.type
inline fun <reified T> Gson.fromJsonArray(json: String): ArrayList<T>? { inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意类型
return attempt { return attempt {
with(JsonParser().parse(json).asJsonArray) { val result: T? = fromJson(json, genericType<T>())
val result = ArrayList<T>() result
for (obj in this) { }.value
attempt { fromJson(obj, T::class.java) }.value?.run { result.add(this) } }
}
result inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
} return attempt {
val result: List<T>? = fromJson(json, genericType<List<T>>())
result
}.value }.value
} }

@ -1,38 +1,33 @@
package io.legado.app.utils package io.legado.app.utils
import android.text.TextUtils
// import org.apache.commons.text.StringEscapeUtils // import org.apache.commons.text.StringEscapeUtils
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
fun String.isAbsUrl() = this.startsWith("http://", true) fun String?.isAbsUrl() = if (this.isNullOrBlank()) false else this.startsWith("http://", true)
|| this.startsWith("https://", true) || this.startsWith("https://", true)
fun String.isJson(): Boolean = kotlin.run { fun String?.isJson(): Boolean = this?.run {
var result = false val str = this.trim()
if (!TextUtils.isEmpty(this)) { when {
val str = this.trim() str.startsWith("{") && str.endsWith("}") -> true
if (str.startsWith("{") && str.endsWith("}")) { str.startsWith("[") && str.endsWith("]") -> true
result = true else -> false
} else if (str.startsWith("[") && str.endsWith("]")) {
result = true
}
} }
return result } ?: false
}
fun String.htmlFormat(): String = if (TextUtils.isEmpty(this)) "" else fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else
this.replace("(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), "\n")// 替换特定标签为换行符 this.replace("(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), "\n")// 替换特定标签为换行符
.replace("<[script>]*.*?>|&nbsp;".toRegex(), "")// 删除script标签对和空格转义符 .replace("<[script>]*.*?>|&nbsp;".toRegex(), "")// 删除script标签对和空格转义符
.replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字
.replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字 .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字
.replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 .replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行
fun String.splitNotBlank(delim: String) = if (!this.contains(delim)) sequenceOf(this) else fun String?.splitNotBlank(delim: String) = this?.run {
this.split(delim).asSequence().map { it.trim() }.filterNot { it.isBlank() } if (!this.contains(delim)) sequenceOf(this) else
this.split(delim).asSequence().map { it.trim() }.filterNot { it.isBlank() }
}
fun startWithIgnoreCase(src: String?, obj: String?): Boolean { fun String?.startWithIgnoreCase(start: String): Boolean {
if (src == null || obj == null) return false return if (this.isNullOrBlank()) false else startsWith(start, true)
return if (obj.length > src.length) false else src.substring(0, obj.length).equals(obj, ignoreCase = true)
} }
Loading…
Cancel
Save