v3.35:修复 RSS 正文排版丢失(段落/列表/引用/表格/硬换行)

This commit is contained in:
XIGE
2026-11-26 18:56:40 +08:00
parent 1373fc014b
commit 7aa5bae17c
3 changed files with 209 additions and 39 deletions

View File

@@ -352,8 +352,21 @@ class ReaderActivity : AppCompatActivity() {
s = Regex("`[^`\\n]+`").replace(s) { m ->
inlines.add(m.value); "\u0000I${inlines.size - 1}\u0000"
}
// 注释与 img(转成 markdown 图片,独立成行的后面会转成铺满整行的大图)
// 注释先清掉
s = s.replace(Regex("<!--[\\s\\S]*?-->"), "")
// 引用块整块处理:逐行补 ">",否则引用只剩第一句带样式
s = Regex("<blockquote\\b[^>]*>([\\s\\S]*?)</blockquote\\s*>", RegexOption.IGNORE_CASE)
.replace(s) { m ->
val inner = m.groupValues[1]
.replace(Regex("</p\\s*>|<br\\s*/?>", RegexOption.IGNORE_CASE), "\n")
.replace(Regex("<[^>]*>"), "")
val quoted = inner.lines()
.map { it.trim() }
.filter { it.isNotEmpty() }
.joinToString("\n") { "> $it" }
if (quoted.isEmpty()) "" else "\n\n$quoted\n\n"
}
// img(转成 markdown 图片,独立成行的后面会转成铺满整行的大图)
s = Regex("<img\\b[^>]*>", RegexOption.IGNORE_CASE).replace(s) { m ->
val src = Regex("\\b(?:data-src|src)\\s*=\\s*['\"]([^'\"]+)['\"]", RegexOption.IGNORE_CASE)
.find(m.value)?.groupValues?.get(1).orEmpty()
@@ -361,15 +374,38 @@ class ReaderActivity : AppCompatActivity() {
.find(m.value)?.groupValues?.get(1).orEmpty()
if (src.isBlank()) "" else "\n\n![$alt]($src)\n\n"
}
s = s.replace(Regex("<br\\s*/?>", RegexOption.IGNORE_CASE), "\n")
// 标题
for (i in 1..6) {
s = Regex("<h$i\\b[^>]*>([\\s\\S]*?)</h$i\\s*>", RegexOption.IGNORE_CASE)
.replace(s) { m -> "\n\n${"#".repeat(i)} ${m.groupValues[1].trim()}\n\n" }
}
// 块级元素闭合转段落分隔;li 转列表项
s = s.replace(Regex("</p\\s*>|</div\\s*>|</figure\\s*>|</li\\s*>", RegexOption.IGNORE_CASE), "\n\n")
// 有序列表先整块编号
s = Regex("<ol\\b[^>]*>([\\s\\S]*?)</ol\\s*>", RegexOption.IGNORE_CASE).replace(s) { m ->
var n = 0
val body = Regex("<li\\b[^>]*>([\\s\\S]*?)</li\\s*>", RegexOption.IGNORE_CASE)
.replace(m.groupValues[1]) { li ->
n++
"\n$n. " + li.groupValues[1]
}
"\n\n" + body.trim() + "\n\n"
}
// 无序列表:相邻项之间只留一个换行,列表才是紧凑的
s = s.replace(Regex("</li\\s*>\\s*<li\\b[^>]*>", RegexOption.IGNORE_CASE), "\n- ")
s = s.replace(Regex("<li\\b[^>]*>", RegexOption.IGNORE_CASE), "- ")
s = s.replace(Regex("</li\\s*>", RegexOption.IGNORE_CASE), "")
s = s.replace(Regex("</?ul\\b[^>]*>", RegexOption.IGNORE_CASE), "\n\n")
// 块级容器开合都算段落边界。只在 </p> 断段,<div> 分段的正文会被压成一坨
s = s.replace(
Regex("</(?:p|div|section|article|figure|figcaption|table|tr)\\s*>", RegexOption.IGNORE_CASE),
"\n\n",
)
s = s.replace(
Regex("<(?:p|div|section|article|figure|figcaption|table|tr|center)\\b[^>]*>", RegexOption.IGNORE_CASE),
"\n\n",
)
// 单个 <br> 是行内换行,用 CommonMark 的行尾反斜杠(双空格会被后面的空白清理吃掉)
s = s.replace(Regex("(?:<br\\s*/?>\\s*){2,}", RegexOption.IGNORE_CASE), "\n\n")
s = s.replace(Regex("<br\\s*/?>", RegexOption.IGNORE_CASE), "\\\n")
// 行内样式
s = s.replace(Regex("<(strong|b)\\b[^>]*>", RegexOption.IGNORE_CASE), "**")
.replace(Regex("</(strong|b)\\s*>", RegexOption.IGNORE_CASE), "**")
@@ -435,7 +471,8 @@ class ReaderActivity : AppCompatActivity() {
val tv = TextView(this).apply {
textSize = 15f
setTextColor(ContextCompat.getColor(context, R.color.text))
setLineSpacing(dp(4).toFloat(), 1f)
// 正文字距放宽一点:15sp 的中文正文按 1.0 行距排,段落一多就糊成一片
setLineSpacing(dp(7).toFloat(), 1.25f)
movementMethod = LinkMovementMethod.getInstance()
}
markwon.setMarkdown(tv, text)