From 7aa5bae17ce2593421b006d6e730656b4ebe25cb Mon Sep 17 00:00:00 2001 From: XIGE <710062962@qq.com> Date: Thu, 26 Nov 2026 18:56:40 +0800 Subject: [PATCH] =?UTF-8?q?v3.35=EF=BC=9A=E4=BF=AE=E5=A4=8D=20RSS=20?= =?UTF-8?q?=E6=AD=A3=E6=96=87=E6=8E=92=E7=89=88=E4=B8=A2=E5=A4=B1=EF=BC=88?= =?UTF-8?q?=E6=AE=B5=E8=90=BD/=E5=88=97=E8=A1=A8/=E5=BC=95=E7=94=A8/?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC/=E7=A1=AC=E6=8D=A2=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/shitoucuo/postpad/MdThemePlugin.kt | 5 + .../com/shitoucuo/postpad/ReaderActivity.kt | 47 ++++- src/main/java/com/shitoucuo/postpad/RssApi.kt | 196 +++++++++++++++--- 3 files changed, 209 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/shitoucuo/postpad/MdThemePlugin.kt b/src/main/java/com/shitoucuo/postpad/MdThemePlugin.kt index 182ebb2..a94e971 100644 --- a/src/main/java/com/shitoucuo/postpad/MdThemePlugin.kt +++ b/src/main/java/com/shitoucuo/postpad/MdThemePlugin.kt @@ -34,5 +34,10 @@ class MdThemePlugin(private val context: Context) : AbstractMarkwonPlugin() { .thematicBreakColor(ContextCompat.getColor(context, R.color.divider)) .thematicBreakHeight((1 * d).toInt()) .linkColor(ContextCompat.getColor(context, R.color.brand)) + // 块级间距:标题、列表、代码块、引用与段落之间留白, + // 默认几乎贴在一起,长文读起来像一块砖 + .blockMargin((10 * d).toInt()) + // 列表标记与文字的缩进,避免 "-" 贴着正文左边缘 + .bulletWidth((16 * d).toInt()) } } diff --git a/src/main/java/com/shitoucuo/postpad/ReaderActivity.kt b/src/main/java/com/shitoucuo/postpad/ReaderActivity.kt index 246e3f8..5d77765 100644 --- a/src/main/java/com/shitoucuo/postpad/ReaderActivity.kt +++ b/src/main/java/com/shitoucuo/postpad/ReaderActivity.kt @@ -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 = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE) + .replace(s) { m -> + val inner = m.groupValues[1] + .replace(Regex("|", 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("]*>", 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("", RegexOption.IGNORE_CASE), "\n") // 标题 for (i in 1..6) { s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE) .replace(s) { m -> "\n\n${"#".repeat(i)} ${m.groupValues[1].trim()}\n\n" } } - // 块级元素闭合转段落分隔;li 转列表项 - s = s.replace(Regex("|||", RegexOption.IGNORE_CASE), "\n\n") + // 有序列表先整块编号 + s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE).replace(s) { m -> + var n = 0 + val body = Regex("]*>([\\s\\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("\\s*]*>", RegexOption.IGNORE_CASE), "\n- ") s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "- ") + s = s.replace(Regex("", RegexOption.IGNORE_CASE), "") + s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "\n\n") + // 块级容器开合都算段落边界。只在

断段,
分段的正文会被压成一坨 + s = s.replace( + Regex("", RegexOption.IGNORE_CASE), + "\n\n", + ) + s = s.replace( + Regex("<(?:p|div|section|article|figure|figcaption|table|tr|center)\\b[^>]*>", RegexOption.IGNORE_CASE), + "\n\n", + ) + // 单个
是行内换行,用 CommonMark 的行尾反斜杠(双空格会被后面的空白清理吃掉) + s = s.replace(Regex("(?:\\s*){2,}", RegexOption.IGNORE_CASE), "\n\n") + s = s.replace(Regex("", RegexOption.IGNORE_CASE), "\\\n") // 行内样式 s = s.replace(Regex("<(strong|b)\\b[^>]*>", RegexOption.IGNORE_CASE), "**") .replace(Regex("", 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) diff --git a/src/main/java/com/shitoucuo/postpad/RssApi.kt b/src/main/java/com/shitoucuo/postpad/RssApi.kt index 3791fa8..b38c558 100644 --- a/src/main/java/com/shitoucuo/postpad/RssApi.kt +++ b/src/main/java/com/shitoucuo/postpad/RssApi.kt @@ -548,14 +548,36 @@ object RssApi { // 头像 / 二维码小图 val avatarImage = Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE) - var out = markdown.lines().filter { line -> + // 代码块先占位:逐行处理会 trim 掉行首缩进,代码缩进一旦丢了就没法看 + val codeList = mutableListOf() + val md = Regex("```[\\s\\S]*?```").replace(markdown) { m -> + codeList.add(m.value) + "\u0000C${codeList.size - 1}\u0000" + } + + // 关键:空行是段落边界,必须保留。 + // 之前这里把空行也 filter 掉了,所有段落粘成一段——正文才会变成 + // 「密密麻麻一大坨文字」,净化写得再干净也没有排版。 + val kept = ArrayList() + var pendingBlank = false + for (line in md.lines()) { val t = line.trim() - if (t.isEmpty()) return@filter false - if (anchorLine.matches(t) || urlOnly.matches(t)) return@filter false - if (tocTitle.matches(t) || counterLine.matches(t) || avatarImage.matches(t)) return@filter false + if (t.isEmpty()) { + if (kept.isNotEmpty()) pendingBlank = true + continue + } + if (anchorLine.matches(t) || urlOnly.matches(t)) continue + if (tocTitle.matches(t) || counterLine.matches(t) || avatarImage.matches(t)) continue // 只过滤短行:正文里整段讨论「评论」「版权」的段落不受影响 - !(t.length < 100 && keyRe.containsMatchIn(t)) - }.joinToString("\n") + if (t.length < 100 && keyRe.containsMatchIn(t)) continue + if (pendingBlank) { + kept.add("") + pendingBlank = false + } + kept.add(t) + } + var out = kept.joinToString("\n") + codeList.forEachIndexed { i, code -> out = out.replace("\u0000C$i\u0000", code) } // 末尾的分隔线没有意义,去掉 out = out.replace(Regex("(?:\\n\\s*(?:-{3,}|\\*{3,}|_{3,})\\s*)+$"), "") @@ -565,7 +587,21 @@ object RssApi { .trim() } - /** 轻量 HTML → Markdown,够阅读用,不追求完整 CommonMark */ + /** + * 轻量 HTML → Markdown。 + * + * 排版的关键不是「转成 markdown」,而是**保住原文的块结构**: + * 段落、标题、列表、引用、表格、图片各自独立成块,块与块之间留空行。 + * + * 上一版只在 `

` 处断段,凡是拿 `
`、`
`、`
` 分段的站点 + * (国内主题很常见)就全被压成一整段——这就是「净化后没有排版、密密麻麻」的真因。 + * + * 三个占位符贯穿全程,因为文本节点里的源码换行必须折叠成空格(浏览器本来就这么显示), + * 不折叠的话 HTML 源码每 80 字一折行,读起来就是满屏断句: + * - `\u0001` → 段落分隔 `\n\n` + * - `\u0002` → 结构换行 `\n`(列表项、标题行) + * - `\u0003` → 硬换行(`
`,用 CommonMark 的行尾反斜杠) + */ private fun htmlToMarkdown(raw: String): String { var s = raw s = s.replace(Regex("", RegexOption.IGNORE_CASE), "") @@ -578,58 +614,150 @@ object RssApi { val codeBlocks = mutableListOf() s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE).replace(s) { m -> codeBlocks.add(unescape(stripTags(m.groupValues[1])).trim('\n')) - "\u0000P${codeBlocks.size - 1}\u0000" + "\u0001\u0000P${codeBlocks.size - 1}\u0000\u0001" + } + // 引用块整块处理:内部段落分隔要带上 ">",否则引用看起来只剩第一句 + s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE).replace(s) { m -> + val inner = m.groupValues[1] + .replace(Regex("|", RegexOption.IGNORE_CASE), "\u0002> ") + "\u0001> " + inner.trim() + "\u0001" } // 懒加载图片:先取 data-* 里的地址 s = s.replace( Regex("]*?(?:data-src|data-original|data-actualsrc|data-lazy-src)=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE), ) { m -> "" } - for (level in 1..6) { - s = s.replace( - Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE), - ) { m -> "\n\n${"#".repeat(level)} ${unescape(stripTags(m.groupValues[1])).trim()}\n\n" } - } - s = s.replace(Regex("<(strong|b)\\b[^>]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> - val t = stripTags(m.groupValues[2]).trim() - if (t.isEmpty()) "" else "**$t**" - } - s = s.replace(Regex("<(em|i)\\b[^>]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> - val t = stripTags(m.groupValues[2]).trim() - if (t.isEmpty()) "" else "*$t*" - } + + // 行内顺序很关键:先把 a / img 落成 markdown,再处理 strong / em, + // 否则 strong 内部的 stripTags 会把刚转好的链接啃成纯文字 s = s.replace(Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> val t = unescape(stripTags(m.groupValues[1])) if (t.isBlank()) "" else "`$t`" } - // 链接与图片:属性单引号、双引号都认 s = s.replace(Regex("]*href=[\"']([^\"']+)[\"'][^>]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> val text = unescape(stripTags(m.groupValues[2])).trim() if (text.isEmpty()) "" else "[$text](${m.groupValues[1].trim()})" } - s = s.replace(Regex("]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m -> - "\n\n![](${m.groupValues[1]})\n\n" + s = s.replace(Regex("]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m -> + val alt = unescape(attrValue(m.value, "alt")).replace(Regex("\\s+"), " ").trim() + "\u0001![$alt](${m.groupValues[1]})\u0001" } - s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "\n- ") + s = s.replace(Regex("<(strong|b)\\b[^>]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> + val t = unescape(stripTags(m.groupValues[2])).trim() + if (t.isEmpty()) "" else "**$t**" + } + s = s.replace(Regex("<(em|i)\\b[^>]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE)) { m -> + val t = unescape(stripTags(m.groupValues[2])).trim() + if (t.isEmpty()) "" else "*$t*" + } + for (level in 1..6) { + s = s.replace( + Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE), + ) { m -> "\u0001${"#".repeat(level)} ${unescape(stripTags(m.groupValues[1])).trim()}\u0001" } + } + // 表格整块转成 markdown 表:留到最后处理只会被打散成一行行文字 + val tables = mutableListOf() + s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE).replace(s) { m -> + val md = tableToMarkdown(m.groupValues[1]) + if (md.isBlank()) "" else { + tables.add(md) + "\u0001\u0000T${tables.size - 1}\u0000\u0001" + } + } + // 有序列表整块编号,否则只能统一成无序项 + s = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE).replace(s) { m -> + var i = 0 + val body = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE) + .replace(m.groupValues[1]) { li -> + i++ + "\u0002$i. " + li.groupValues[1] + } + "\u0001" + body.trim() + "\u0001" + } + s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "\u0002- ") s = s.replace(Regex("", RegexOption.IGNORE_CASE), "") - s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "\n\n> ") - s = s.replace(Regex("", RegexOption.IGNORE_CASE), "\n\n") - s = s.replace(Regex("", RegexOption.IGNORE_CASE), "\n") - // 分隔线没有阅读价值,直接去掉(后置的按行过滤还会再兜一次) + // 两个以上
当分段,单个当硬换行 + s = s.replace(Regex("(?:\\s*){2,}", RegexOption.IGNORE_CASE), "\u0001") + s = s.replace(Regex("", RegexOption.IGNORE_CASE), "\u0003") s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "") + // 图注单独一行 + s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), "\u0001*") + s = s.replace(Regex("", RegexOption.IGNORE_CASE), "*\u0001") + // 块级容器开合都算段落边界:div / section / p 是原文最主要的断段来源 s = s.replace( - Regex("", RegexOption.IGNORE_CASE), - "\n\n", + Regex( + "", + RegexOption.IGNORE_CASE, + ), + "\u0001", ) - s = s.replace(Regex("]*>", RegexOption.IGNORE_CASE), " ") + s = s.replace( + Regex( + "<(?:p|div|section|article|ul|ol|figure|center|main|dl|dd|dt|tr)\\b[^>]*>", + RegexOption.IGNORE_CASE, + ), + "\u0001", + ) + // 表格残留(没被整块接住的):单元格用 | 分隔,行仍独立 + s = s.replace(Regex("", RegexOption.IGNORE_CASE), " | ") + s = s.replace(Regex("", RegexOption.IGNORE_CASE), "\u0001") + s = stripTags(s) s = unescape(s) + // 到这里剩下的换行都是 HTML 源码里的排版折行,不是语义分段 + s = s.replace(Regex("[\\r\\n\\t\\u000B\\f]+"), " ") + s = s.replace(Regex(" {2,}"), " ") + s = s.replace("\u0001", "\n\n").replace("\u0003", "\\\n").replace("\u0002", "\n") codeBlocks.forEachIndexed { i, code -> - s = s.replace("\u0000P$i\u0000", "\n\n```\n$code\n```\n\n") + s = s.replace("\u0000P$i\u0000", "```\n$code\n```") } - // 收拢空行 + tables.forEachIndexed { i, table -> + s = s.replace("\u0000T$i\u0000", table) + } + // 收拢:行尾空白、行首缩进(代码块已占位)、连续空行 + s = s.replace(Regex("[ \\t]+\\n"), "\n") + s = s.lines().joinToString("\n") { it.trim() } return s.replace(Regex("\\n{3,}"), "\n\n").trim() } + /** + * `` → Markdown 表格。 + * + * 只有一列的表格按段落输出(很多主题用 table 做布局,硬转成表格反而难看); + * 两列以上按标准 pipe 表输出,首行做表头、补一行分隔线。 + */ + private fun tableToMarkdown(html: String): String { + val rows = Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE) + .findAll(html) + .map { tr -> + Regex("]*>([\\s\\S]*?)", RegexOption.IGNORE_CASE) + .findAll(tr.groupValues[1]) + .map { cell -> + unescape(stripTags(cell.groupValues[1])) + .replace(Regex("[\\r\\n\\t]+"), " ") + .replace(Regex(" {2,}"), " ") + .trim() + } + .toList() + } + .filter { it.isNotEmpty() } + .toList() + if (rows.isEmpty()) return "" + val cols = rows.maxOf { it.size } + if (cols < 2) return rows.joinToString("\n\n") { it.joinToString(" ") } + val sb = StringBuilder() + rows.forEachIndexed { index, cells -> + sb.append("| ") + sb.append((0 until cols).joinToString(" | ") { cells.getOrElse(it) { "" } }) + sb.append(" |\n") + if (index == 0) { + sb.append("| ") + sb.append((0 until cols).joinToString(" | ") { "---" }) + sb.append(" |\n") + } + } + return sb.toString().trim() + } + /** 常见实体还原:正文里露出 & /   之类的原始写法很难看 */ private fun unescape(s: String): String = s .replace(" ", " ")