pull/497/head
gedoor 5 years ago
parent 1eae98fc27
commit ba0f6f06a3
  1. 4
      app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt
  2. 46
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 11
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  4. 31
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  6. 4
      app/src/main/res/values-zh-rHK/strings.xml
  7. 4
      app/src/main/res/values-zh-rTW/strings.xml
  8. 4
      app/src/main/res/values-zh/strings.xml
  9. 4
      app/src/main/res/values/strings.xml

@ -9,8 +9,6 @@ data class ContentRule(
var nextContentUrl: String? = null, var nextContentUrl: String? = null,
var webJs: String? = null, var webJs: String? = null,
var sourceRegex: String? = null, var sourceRegex: String? = null,
var replaceRegex: String? = null, var replaceRegex: String? = null, //正文获取后处理规则
var imageStyle: String? = null, //默认大小居中,FULL最大宽度 var imageStyle: String? = null, //默认大小居中,FULL最大宽度
var font: String? = null, //网页内包含的字体必须返回ByteArray
var correctFont: String? = null, //正确的字体必须返回QueryTTF
) : Parcelable ) : Parcelable

@ -1,14 +1,18 @@
package io.legado.app.help package io.legado.app.help
import android.net.Uri
import android.util.Base64 import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.App
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.CookieStore
import io.legado.app.help.http.HttpHelper
import io.legado.app.help.http.SSLHelper import io.legado.app.help.http.SSLHelper
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.runBlocking
import org.jsoup.Connection import org.jsoup.Connection
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.File import java.io.File
@ -230,11 +234,51 @@ interface JsExtensions {
/** /**
* 解析字体,返回字体解析类 * 解析字体,返回字体解析类
*/ */
fun queryTTF(font: ByteArray?): QueryTTF? { fun queryBase64TTF(base64: String?): QueryTTF? {
base64DecodeToByteArray(base64)?.let {
return QueryTTF(it)
}
return null
}
fun queryTTF(path: String): QueryTTF? {
val qTTF = CacheManager.getQueryTTF(path)
if (qTTF != null) {
return qTTF
}
val font: ByteArray? = when {
path.isAbsUrl() -> runBlocking {
var x = CacheManager.getByteArray(path)
if (x == null) {
x = HttpHelper.simpleGetBytesAsync(path)
x?.let {
CacheManager.put(path, it)
}
}
return@runBlocking x
}
path.isContentScheme() -> {
Uri.parse(path).readBytes(App.INSTANCE)
}
else -> {
File(path).readBytes()
}
}
font ?: return null font ?: return null
return QueryTTF(font) return QueryTTF(font)
} }
fun replaceFont(text: String, font1: QueryTTF, font2: QueryTTF, start: Int, end: Int): String {
val contentArray = text.toCharArray()
contentArray.forEachIndexed { index, s ->
if (s > start.toChar() && s < end.toChar()) {
val code = font2.GetCodeByGlyf(font1.GetGlyfByCode(s.toInt()))
contentArray[index] = code.toChar()
}
}
return contentArray.joinToString("")
}
/** /**
* 输出调试日志 * 输出调试日志
*/ */

@ -298,8 +298,8 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
return ArrayList() return ArrayList()
} }
fun getResult(ruleStr: String): Any? { fun getContent(ruleStr: String, text: String?): String {
if (ruleStr.isEmpty()) return null if (ruleStr.isEmpty()) return ""
val ruleList = splitSourceRule(ruleStr) val ruleList = splitSourceRule(ruleStr)
var result: Any? = null var result: Any? = null
content?.let { o -> content?.let { o ->
@ -312,7 +312,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
result.toString(), result.toString(),
sourceRule.rule.splitNotBlank("&&") sourceRule.rule.splitNotBlank("&&")
) )
Mode.Js -> evalJS(sourceRule.rule, result) Mode.Js -> evalJS(sourceRule.rule, result, text)
Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule) Mode.Json -> getAnalyzeByJSonPath(it).getList(sourceRule.rule)
Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule) Mode.XPath -> getAnalyzeByXPath(it).getElements(sourceRule.rule)
else -> getAnalyzeByJSoup(it).getElements(sourceRule.rule) else -> getAnalyzeByJSoup(it).getElements(sourceRule.rule)
@ -323,7 +323,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
} }
} }
} }
return result return result?.toString() ?: ""
} }
/** /**
@ -649,7 +649,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
/** /**
* 执行JS * 执行JS
*/ */
private fun evalJS(jsStr: String, result: Any?): Any? { private fun evalJS(jsStr: String, result: Any?, content: String? = null): Any? {
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["java"] = this bindings["java"] = this
bindings["cookie"] = CookieStore bindings["cookie"] = CookieStore
@ -659,6 +659,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
bindings["baseUrl"] = baseUrl bindings["baseUrl"] = baseUrl
bindings["chapter"] = chapter bindings["chapter"] = chapter
bindings["title"] = chapter?.title bindings["title"] = chapter?.title
bindings["content"] = content
return SCRIPT_ENGINE.eval(jsStr, bindings) return SCRIPT_ENGINE.eval(jsStr, bindings)
} }

@ -10,7 +10,6 @@ import io.legado.app.help.BookHelp
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.htmlFormat import io.legado.app.utils.htmlFormat
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -36,21 +35,6 @@ object BookContent {
val nextUrlList = arrayListOf(baseUrl) val nextUrlList = arrayListOf(baseUrl)
val contentRule = bookSource.getContentRule() val contentRule = bookSource.getContentRule()
val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl) val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl)
val fontRule = contentRule.font
val correctFontRule = contentRule.correctFont
var font: ByteArray? = null
var cQueryTTF: QueryTTF? = null
fontRule?.let {
//todo 获取网页嵌入字体
font = analyzeRule.getResult(it) as? ByteArray
}
correctFontRule?.let {
//todo 获取正确字体
cQueryTTF = analyzeRule.getResult(it) as? QueryTTF
}
if (cQueryTTF == null && font != null) {
BookHelp.saveFont(book, bookChapter, font!!)
}
var contentData = analyzeContent( var contentData = analyzeContent(
book, baseUrl, body, contentRule, bookChapter, bookSource book, baseUrl, body, contentRule, bookChapter, bookSource
) )
@ -110,20 +94,7 @@ object BookContent {
var contentStr = content.toString().htmlFormat() var contentStr = content.toString().htmlFormat()
val replaceRegex = bookSource.ruleContent?.replaceRegex val replaceRegex = bookSource.ruleContent?.replaceRegex
if (!replaceRegex.isNullOrEmpty()) { if (!replaceRegex.isNullOrEmpty()) {
analyzeRule.setContent(contentStr).setBaseUrl(baseUrl) contentStr = analyzeRule.getContent(replaceRegex, contentStr)
analyzeRule.chapter = bookChapter
contentStr = analyzeRule.getString(replaceRegex)
}
if (cQueryTTF != null && font != null) {
val queryTTF = QueryTTF(font!!)
val contentArray = contentStr.toCharArray()
contentArray.forEachIndexed { index, s ->
if (s > 58000.toChar()) {
val code = cQueryTTF!!.GetCodeByGlyf(queryTTF.GetGlyfByCode(s.toInt()))
contentArray[index] = code.toChar()
}
}
contentStr = contentArray.joinToString("")
} }
Debug.log(bookSource.bookSourceUrl, "┌获取章节名称") Debug.log(bookSource.bookSourceUrl, "┌获取章节名称")
Debug.log(bookSource.bookSourceUrl, "${bookChapter.title}") Debug.log(bookSource.bookSourceUrl, "${bookChapter.title}")

@ -237,8 +237,6 @@ class BookSourceEditActivity :
add(EditEntity("sourceRegex", cr?.sourceRegex, R.string.rule_source_regex)) add(EditEntity("sourceRegex", cr?.sourceRegex, R.string.rule_source_regex))
add(EditEntity("replaceRegex", cr?.replaceRegex, R.string.rule_replace_regex)) add(EditEntity("replaceRegex", cr?.replaceRegex, R.string.rule_replace_regex))
add(EditEntity("imageStyle", cr?.imageStyle, R.string.rule_image_style)) add(EditEntity("imageStyle", cr?.imageStyle, R.string.rule_image_style))
add(EditEntity("font", cr?.font, R.string.rule_font))
add(EditEntity("correctFont", cr?.correctFont, R.string.rule_correct_font))
} }
//发现 //发现
val er = source?.getExploreRule() val er = source?.getExploreRule()
@ -342,8 +340,6 @@ class BookSourceEditActivity :
"sourceRegex" -> contentRule.sourceRegex = it.value "sourceRegex" -> contentRule.sourceRegex = it.value
"replaceRegex" -> contentRule.replaceRegex = it.value "replaceRegex" -> contentRule.replaceRegex = it.value
"imageStyle" -> contentRule.imageStyle = it.value "imageStyle" -> contentRule.imageStyle = it.value
"font" -> contentRule.font = it.value
"correctFont" -> contentRule.correctFont = it.value
} }
} }
source.ruleSearch = searchRule source.ruleSearch = searchRule

@ -412,8 +412,6 @@
<string name="rule_next_content">正文下一頁 URL 規則 (nextContentUrl)</string> <string name="rule_next_content">正文下一頁 URL 規則 (nextContentUrl)</string>
<string name="rule_web_js">webJs</string> <string name="rule_web_js">webJs</string>
<string name="rule_source_regex">資源正則 (sourceRegex)</string> <string name="rule_source_regex">資源正則 (sourceRegex)</string>
<string name="rule_font">网页内嵌字體(font)</string>
<string name="rule_correct_font">正确字体(font)</string>
<string name="source_icon">圖標 (sourceIcon)</string> <string name="source_icon">圖標 (sourceIcon)</string>
<string name="r_articles">列表規則 (ruleArticles)</string> <string name="r_articles">列表規則 (ruleArticles)</string>
@ -732,7 +730,7 @@
<string name="restore_ignore_summary">恢復時忽略一些內容不恢復,方便不同手機配置不同</string> <string name="restore_ignore_summary">恢復時忽略一些內容不恢復,方便不同手機配置不同</string>
<string name="read_config">閱讀界面設置</string> <string name="read_config">閱讀界面設置</string>
<string name="rule_image_style">图片样式(imageStyle)</string> <string name="rule_image_style">图片样式(imageStyle)</string>
<string name="rule_replace_regex">替换规则(replaceRegex)</string> <string name="rule_replace_regex">正文获取后处理规则(replaceRegex)</string>
<string name="group_name">分組名稱</string> <string name="group_name">分組名稱</string>
<string name="note_content">備註內容</string> <string name="note_content">備註內容</string>
<string name="replace_enable_default_t">默认启用替换净化</string> <string name="replace_enable_default_t">默认启用替换净化</string>

@ -413,8 +413,6 @@
<string name="rule_next_content">正文下一頁URL規則(nextContentUrl)</string> <string name="rule_next_content">正文下一頁URL規則(nextContentUrl)</string>
<string name="rule_web_js">webJs</string> <string name="rule_web_js">webJs</string>
<string name="rule_source_regex">資源正則(sourceRegex)</string> <string name="rule_source_regex">資源正則(sourceRegex)</string>
<string name="rule_font">网页内嵌字體(font)</string>
<string name="rule_correct_font">正确字体(font)</string>
<string name="source_icon">圖示(sourceIcon)</string> <string name="source_icon">圖示(sourceIcon)</string>
<string name="r_articles">列表規則(ruleArticles)</string> <string name="r_articles">列表規則(ruleArticles)</string>
@ -732,7 +730,7 @@
<string name="restore_ignore_summary">復原時忽略一些內容不復原,方便不同手機配置不同</string> <string name="restore_ignore_summary">復原時忽略一些內容不復原,方便不同手機配置不同</string>
<string name="read_config">閱讀介面設定</string> <string name="read_config">閱讀介面設定</string>
<string name="rule_image_style">圖片樣式(imageStyle)</string> <string name="rule_image_style">圖片樣式(imageStyle)</string>
<string name="rule_replace_regex">取代規則(replaceRegex)</string> <string name="rule_replace_regex">正文获取后处理规则(replaceRegex)</string>
<string name="group_name">分組名稱</string> <string name="group_name">分組名稱</string>
<string name="note_content">備註內容</string> <string name="note_content">備註內容</string>
<string name="replace_enable_default_t">預設啟用取代淨化</string> <string name="replace_enable_default_t">預設啟用取代淨化</string>

@ -418,8 +418,6 @@
<string name="rule_next_content">正文下一页URL规则(nextContentUrl)</string> <string name="rule_next_content">正文下一页URL规则(nextContentUrl)</string>
<string name="rule_web_js">webJs</string> <string name="rule_web_js">webJs</string>
<string name="rule_source_regex">资源正则(sourceRegex)</string> <string name="rule_source_regex">资源正则(sourceRegex)</string>
<string name="rule_font">网页内嵌字体(font)</string>
<string name="rule_correct_font">正确字体(font)</string>
<string name="source_icon">图标(sourceIcon)</string> <string name="source_icon">图标(sourceIcon)</string>
<string name="r_articles">列表规则(ruleArticles)</string> <string name="r_articles">列表规则(ruleArticles)</string>
@ -737,7 +735,7 @@
<string name="restore_ignore_summary">恢复时忽略一些内容不恢复,方便不同手机配置不同</string> <string name="restore_ignore_summary">恢复时忽略一些内容不恢复,方便不同手机配置不同</string>
<string name="read_config">阅读界面设置</string> <string name="read_config">阅读界面设置</string>
<string name="rule_image_style">图片样式(imageStyle)</string> <string name="rule_image_style">图片样式(imageStyle)</string>
<string name="rule_replace_regex">替换规则(replaceRegex)</string> <string name="rule_replace_regex">正文获取后处理规则(replaceRegex)</string>
<string name="group_name">分组名称</string> <string name="group_name">分组名称</string>
<string name="note_content">备注内容</string> <string name="note_content">备注内容</string>
<string name="replace_enable_default_t">默认启用替换净化</string> <string name="replace_enable_default_t">默认启用替换净化</string>

@ -420,10 +420,8 @@
<string name="rule_next_content">正文下一页URL规则(nextContentUrl)</string> <string name="rule_next_content">正文下一页URL规则(nextContentUrl)</string>
<string name="rule_web_js">webJs</string> <string name="rule_web_js">webJs</string>
<string name="rule_source_regex">资源正则(sourceRegex)</string> <string name="rule_source_regex">资源正则(sourceRegex)</string>
<string name="rule_replace_regex">替换规则(replaceRegex)</string> <string name="rule_replace_regex">正文获取后处理规则(replaceRegex)</string>
<string name="rule_image_style">图片样式(imageStyle)</string> <string name="rule_image_style">图片样式(imageStyle)</string>
<string name="rule_font">网页内嵌字体(font)</string>
<string name="rule_correct_font">正确字体(font)</string>
<string name="source_icon">图标(sourceIcon)</string> <string name="source_icon">图标(sourceIcon)</string>
<string name="r_articles">列表规则(ruleArticles)</string> <string name="r_articles">列表规则(ruleArticles)</string>

Loading…
Cancel
Save