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. 17
      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.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.*
import org.jsoup.Connection
import org.jsoup.Jsoup
@ -226,6 +227,14 @@ interface JsExtensions {
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.analyzeRule.AnalyzeRule
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.htmlFormat
import io.legado.app.utils.toStringArray
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withContext
@ -34,13 +36,21 @@ object BookContent {
val content = StringBuilder()
val nextUrlList = arrayListOf(baseUrl)
val contentRule = bookSource.getContentRule()
contentRule.font?.let {
//todo 获取字体
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(body).setBaseUrl(baseUrl)
analyzeRule.getByteArray(it)?.let { font ->
BookHelp.saveFont(book, bookChapter, font)
}
val analyzeRule = AnalyzeRule(book).setContent(body, baseUrl)
val fontRule = contentRule.font
val correctFontRule = contentRule.correctFont
var font: ByteArray? = null
var correctFont: ByteArray? = null
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(
book, baseUrl, body, contentRule, bookChapter, bookSource
@ -101,11 +111,20 @@ object BookContent {
var contentStr = content.toString().htmlFormat()
val replaceRegex = bookSource.ruleContent?.replaceRegex
if (!replaceRegex.isNullOrEmpty()) {
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(contentStr).setBaseUrl(baseUrl)
analyzeRule.chapter = bookChapter
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, "${bookChapter.title}")
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.backgroundColor
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.qrcode.QrCodeActivity
import io.legado.app.ui.widget.KeyboardToolPop
@ -33,11 +35,13 @@ import kotlin.math.abs
class BookSourceEditActivity :
VMBaseActivity<BookSourceEditViewModel>(R.layout.activity_book_source_edit, false),
FilePickerDialog.CallBack,
KeyboardToolPop.CallBack {
override val viewModel: BookSourceEditViewModel
get() = getViewModel(BookSourceEditViewModel::class.java)
private val qrRequestCode = 101
private val selectPathRequestCode = 102
private val adapter = BookSourceEditAdapter()
private val sourceEntities: 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("imageStyle", cr?.imageStyle, R.string.rule_image_style))
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()
@ -339,7 +343,7 @@ class BookSourceEditActivity :
"replaceRegex" -> contentRule.replaceRegex = it.value
"imageStyle" -> contentRule.imageStyle = it.value
"font" -> contentRule.font = it.value
"correctFont" -> contentRule.font = it.value
"correctFont" -> contentRule.correctFont = it.value
}
}
source.ruleSearch = searchRule
@ -382,12 +386,13 @@ class BookSourceEditActivity :
}
private fun showHelpDialog() {
val items = arrayListOf("插入URL参数", "书源教程", "正则教程")
val items = arrayListOf("插入URL参数", "书源教程", "正则教程", "选择文件")
selector(getString(R.string.help), items) { _, index ->
when (index) {
0 -> insertText(AppConst.urlOption)
1 -> openUrl("https://alanskycn.gitee.io/teachme/Rule/source.html")
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,
requestCode: Int,
title: String = activity.getString(R.string.select_file),
allowExtensions: Array<String>,
allowExtensions: Array<String> = arrayOf(),
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
@ -177,7 +177,7 @@ object FilePicker {
fragment: Fragment,
requestCode: Int,
title: String = fragment.getString(R.string.select_file),
allowExtensions: Array<String>,
allowExtensions: Array<String> = arrayOf(),
otherActions: List<String>? = null,
otherFun: ((action: String) -> Unit)? = null
) {
@ -267,10 +267,15 @@ object FilePicker {
private fun typesOfExtensions(allowExtensions: Array<String>): Array<String> {
val types = hashSetOf<String>()
allowExtensions.forEach {
when (it) {
"txt", "xml" -> types.add("text/*")
else -> types.add("application/$it")
if (allowExtensions.isNullOrEmpty()) {
types.add("*/*")
} else {
allowExtensions.forEach {
when (it) {
"*" -> types.add("*/*")
"txt", "xml" -> types.add("text/*")
else -> types.add("application/$it")
}
}
}
return types.toTypedArray()

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

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

Loading…
Cancel
Save