Merge pull request #490 from gedoor/master

update
pull/493/head
Antecer 4 years ago committed by GitHub
commit 2385c3ebc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 35
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  3. 20
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  4. 9
      app/src/main/java/io/legado/app/ui/filepicker/FilePicker.kt
  5. 2
      app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt
  6. 2
      app/src/main/java/io/legado/app/ui/filepicker/adapter/FileAdapter.kt

@ -7,6 +7,7 @@ import io.legado.app.help.http.CookieStore
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.utils.* import io.legado.app.utils.*
import org.jsoup.Connection import org.jsoup.Connection
import org.jsoup.Jsoup import org.jsoup.Jsoup
@ -226,6 +227,14 @@ interface JsExtensions {
return File(path).readBytes() return File(path).readBytes()
} }
/**
* 解析字体,返回字体解析类
*/
fun queryTTF(font: ByteArray?): QueryTTF? {
font ?: return null
return QueryTTF(font)
}
/** /**
* 输出调试日志 * 输出调试日志
*/ */

@ -10,8 +10,10 @@ 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 io.legado.app.utils.toStringArray
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -34,13 +36,21 @@ object BookContent {
val content = StringBuilder() val content = StringBuilder()
val nextUrlList = arrayListOf(baseUrl) val nextUrlList = arrayListOf(baseUrl)
val contentRule = bookSource.getContentRule() val contentRule = bookSource.getContentRule()
contentRule.font?.let { val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl)
//todo 获取字体 val fontRule = contentRule.font
val analyzeRule = AnalyzeRule(book) val correctFontRule = contentRule.correctFont
analyzeRule.setContent(body).setBaseUrl(baseUrl) var font: ByteArray? = null
analyzeRule.getByteArray(it)?.let { font -> var correctFont: ByteArray? = null
BookHelp.saveFont(book, bookChapter, font) fontRule?.let {
} //todo 获取网页嵌入字体
font = analyzeRule.getByteArray(it)
}
correctFontRule?.let {
//todo 获取正确字体
correctFont = analyzeRule.getByteArray(it)
}
if (correctFont == 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
@ -101,11 +111,20 @@ 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()) {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(contentStr).setBaseUrl(baseUrl) analyzeRule.setContent(contentStr).setBaseUrl(baseUrl)
analyzeRule.chapter = bookChapter analyzeRule.chapter = bookChapter
contentStr = analyzeRule.getString(replaceRegex) contentStr = analyzeRule.getString(replaceRegex)
} }
if (correctFont != null && font != null) {
val queryTTF = QueryTTF(font!!)
val cQueryTTF = QueryTTF(correctFont!!)
val contentArray = contentStr.toStringArray()
contentArray.forEachIndexed { index, s ->
val code = cQueryTTF.GetCodeByGlyf(queryTTF.GetGlyfByCode(s.toInt()))
contentArray[index] = code.toString()
}
contentStr = contentArray.joinToString("")
}
Debug.log(bookSource.bookSourceUrl, "┌获取章节名称") Debug.log(bookSource.bookSourceUrl, "┌获取章节名称")
Debug.log(bookSource.bookSourceUrl, "${bookChapter.title}") Debug.log(bookSource.bookSourceUrl, "${bookChapter.title}")
Debug.log(bookSource.bookSourceUrl, "┌获取正文内容") Debug.log(bookSource.bookSourceUrl, "┌获取正文内容")

@ -22,6 +22,8 @@ import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.backgroundColor import io.legado.app.lib.theme.backgroundColor
import io.legado.app.ui.book.source.debug.BookSourceDebugActivity import io.legado.app.ui.book.source.debug.BookSourceDebugActivity
import io.legado.app.ui.filepicker.FilePicker
import io.legado.app.ui.filepicker.FilePickerDialog
import io.legado.app.ui.login.SourceLogin import io.legado.app.ui.login.SourceLogin
import io.legado.app.ui.qrcode.QrCodeActivity import io.legado.app.ui.qrcode.QrCodeActivity
import io.legado.app.ui.widget.KeyboardToolPop import io.legado.app.ui.widget.KeyboardToolPop
@ -33,11 +35,13 @@ import kotlin.math.abs
class BookSourceEditActivity : class BookSourceEditActivity :
VMBaseActivity<BookSourceEditViewModel>(R.layout.activity_book_source_edit, false), VMBaseActivity<BookSourceEditViewModel>(R.layout.activity_book_source_edit, false),
FilePickerDialog.CallBack,
KeyboardToolPop.CallBack { KeyboardToolPop.CallBack {
override val viewModel: BookSourceEditViewModel override val viewModel: BookSourceEditViewModel
get() = getViewModel(BookSourceEditViewModel::class.java) get() = getViewModel(BookSourceEditViewModel::class.java)
private val qrRequestCode = 101 private val qrRequestCode = 101
private val selectPathRequestCode = 102
private val adapter = BookSourceEditAdapter() private val adapter = BookSourceEditAdapter()
private val sourceEntities: ArrayList<EditEntity> = ArrayList() private val sourceEntities: ArrayList<EditEntity> = ArrayList()
private val searchEntities: ArrayList<EditEntity> = ArrayList() private val searchEntities: ArrayList<EditEntity> = ArrayList()
@ -234,7 +238,7 @@ class BookSourceEditActivity :
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("font", cr?.font, R.string.rule_font))
add(EditEntity("correctFont", cr?.font, R.string.rule_correct_font)) add(EditEntity("correctFont", cr?.correctFont, R.string.rule_correct_font))
} }
//发现 //发现
val er = source?.getExploreRule() val er = source?.getExploreRule()
@ -339,7 +343,7 @@ class BookSourceEditActivity :
"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 "font" -> contentRule.font = it.value
"correctFont" -> contentRule.font = it.value "correctFont" -> contentRule.correctFont = it.value
} }
} }
source.ruleSearch = searchRule source.ruleSearch = searchRule
@ -382,12 +386,13 @@ class BookSourceEditActivity :
} }
private fun showHelpDialog() { private fun showHelpDialog() {
val items = arrayListOf("插入URL参数", "书源教程", "正则教程") val items = arrayListOf("插入URL参数", "书源教程", "正则教程", "选择文件")
selector(getString(R.string.help), items) { _, index -> selector(getString(R.string.help), items) { _, index ->
when (index) { when (index) {
0 -> insertText(AppConst.urlOption) 0 -> insertText(AppConst.urlOption)
1 -> openUrl("https://alanskycn.gitee.io/teachme/Rule/source.html") 1 -> openUrl("https://alanskycn.gitee.io/teachme/Rule/source.html")
2 -> showRegexHelp() 2 -> showRegexHelp()
3 -> FilePicker.selectFile(this, selectPathRequestCode)
} }
} }
} }
@ -420,6 +425,15 @@ class BookSourceEditActivity :
} }
} }
} }
selectPathRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentScheme()) {
sendText(uri.toString())
} else {
sendText(uri.path.toString())
}
}
}
} }
} }

@ -119,7 +119,7 @@ object FilePicker {
activity: BaseActivity, activity: BaseActivity,
requestCode: Int, requestCode: Int,
title: String = activity.getString(R.string.select_file), title: String = activity.getString(R.string.select_file),
allowExtensions: Array<String>, allowExtensions: Array<String> = arrayOf(),
otherActions: List<String>? = null, otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
@ -177,7 +177,7 @@ object FilePicker {
fragment: Fragment, fragment: Fragment,
requestCode: Int, requestCode: Int,
title: String = fragment.getString(R.string.select_file), title: String = fragment.getString(R.string.select_file),
allowExtensions: Array<String>, allowExtensions: Array<String> = arrayOf(),
otherActions: List<String>? = null, otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null otherFun: ((action: String) -> Unit)? = null
) { ) {
@ -267,12 +267,17 @@ object FilePicker {
private fun typesOfExtensions(allowExtensions: Array<String>): Array<String> { private fun typesOfExtensions(allowExtensions: Array<String>): Array<String> {
val types = hashSetOf<String>() val types = hashSetOf<String>()
if (allowExtensions.isNullOrEmpty()) {
types.add("*/*")
} else {
allowExtensions.forEach { allowExtensions.forEach {
when (it) { when (it) {
"*" -> types.add("*/*")
"txt", "xml" -> types.add("text/*") "txt", "xml" -> types.add("text/*")
else -> types.add("application/$it") else -> types.add("application/$it")
} }
} }
}
return types.toTypedArray() return types.toTypedArray()
} }
} }

@ -169,7 +169,7 @@ class FilePickerDialog : DialogFragment(),
fileItem?.path?.let { path -> fileItem?.path?.let { path ->
if (mode == DIRECTORY) { if (mode == DIRECTORY) {
toast("这是文件夹选择,不能选择文件,点击右上角的确定选择文件夹") toast("这是文件夹选择,不能选择文件,点击右上角的确定选择文件夹")
} else if (allowExtensions == null || } else if (allowExtensions.isNullOrEmpty() ||
allowExtensions?.contains(FileUtils.getExtension(path)) == true allowExtensions?.contains(FileUtils.getExtension(path)) == true
) { ) {
setData(path) setData(path)

@ -97,7 +97,7 @@ class FileAdapter(context: Context, val callBack: CallBack) :
text_view.setTextColor(disabledTextColor) text_view.setTextColor(disabledTextColor)
} else { } else {
callBack.allowExtensions?.let { callBack.allowExtensions?.let {
if (it.contains(FileUtils.getExtension(item.path))) { if (it.isEmpty() || it.contains(FileUtils.getExtension(item.path))) {
text_view.setTextColor(primaryTextColor) text_view.setTextColor(primaryTextColor)
} else { } else {
text_view.setTextColor(disabledTextColor) text_view.setTextColor(disabledTextColor)

Loading…
Cancel
Save