v3.35:修复 RSS 正文排版丢失(段落/列表/引用/表格/硬换行)
This commit is contained in:
@@ -34,5 +34,10 @@ class MdThemePlugin(private val context: Context) : AbstractMarkwonPlugin() {
|
|||||||
.thematicBreakColor(ContextCompat.getColor(context, R.color.divider))
|
.thematicBreakColor(ContextCompat.getColor(context, R.color.divider))
|
||||||
.thematicBreakHeight((1 * d).toInt())
|
.thematicBreakHeight((1 * d).toInt())
|
||||||
.linkColor(ContextCompat.getColor(context, R.color.brand))
|
.linkColor(ContextCompat.getColor(context, R.color.brand))
|
||||||
|
// 块级间距:标题、列表、代码块、引用与段落之间留白,
|
||||||
|
// 默认几乎贴在一起,长文读起来像一块砖
|
||||||
|
.blockMargin((10 * d).toInt())
|
||||||
|
// 列表标记与文字的缩进,避免 "-" 贴着正文左边缘
|
||||||
|
.bulletWidth((16 * d).toInt())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -352,8 +352,21 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
s = Regex("`[^`\\n]+`").replace(s) { m ->
|
s = Regex("`[^`\\n]+`").replace(s) { m ->
|
||||||
inlines.add(m.value); "\u0000I${inlines.size - 1}\u0000"
|
inlines.add(m.value); "\u0000I${inlines.size - 1}\u0000"
|
||||||
}
|
}
|
||||||
// 注释与 img(转成 markdown 图片,独立成行的后面会转成铺满整行的大图)
|
// 注释先清掉
|
||||||
s = s.replace(Regex("<!--[\\s\\S]*?-->"), "")
|
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 ->
|
s = Regex("<img\\b[^>]*>", RegexOption.IGNORE_CASE).replace(s) { m ->
|
||||||
val src = Regex("\\b(?:data-src|src)\\s*=\\s*['\"]([^'\"]+)['\"]", RegexOption.IGNORE_CASE)
|
val src = Regex("\\b(?:data-src|src)\\s*=\\s*['\"]([^'\"]+)['\"]", RegexOption.IGNORE_CASE)
|
||||||
.find(m.value)?.groupValues?.get(1).orEmpty()
|
.find(m.value)?.groupValues?.get(1).orEmpty()
|
||||||
@@ -361,15 +374,38 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
.find(m.value)?.groupValues?.get(1).orEmpty()
|
.find(m.value)?.groupValues?.get(1).orEmpty()
|
||||||
if (src.isBlank()) "" else "\n\n\n\n"
|
if (src.isBlank()) "" else "\n\n\n\n"
|
||||||
}
|
}
|
||||||
s = s.replace(Regex("<br\\s*/?>", RegexOption.IGNORE_CASE), "\n")
|
|
||||||
// 标题
|
// 标题
|
||||||
for (i in 1..6) {
|
for (i in 1..6) {
|
||||||
s = Regex("<h$i\\b[^>]*>([\\s\\S]*?)</h$i\\s*>", RegexOption.IGNORE_CASE)
|
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" }
|
.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\\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), "**")
|
s = s.replace(Regex("<(strong|b)\\b[^>]*>", RegexOption.IGNORE_CASE), "**")
|
||||||
.replace(Regex("</(strong|b)\\s*>", RegexOption.IGNORE_CASE), "**")
|
.replace(Regex("</(strong|b)\\s*>", RegexOption.IGNORE_CASE), "**")
|
||||||
@@ -435,7 +471,8 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
val tv = TextView(this).apply {
|
val tv = TextView(this).apply {
|
||||||
textSize = 15f
|
textSize = 15f
|
||||||
setTextColor(ContextCompat.getColor(context, R.color.text))
|
setTextColor(ContextCompat.getColor(context, R.color.text))
|
||||||
setLineSpacing(dp(4).toFloat(), 1f)
|
// 正文字距放宽一点:15sp 的中文正文按 1.0 行距排,段落一多就糊成一片
|
||||||
|
setLineSpacing(dp(7).toFloat(), 1.25f)
|
||||||
movementMethod = LinkMovementMethod.getInstance()
|
movementMethod = LinkMovementMethod.getInstance()
|
||||||
}
|
}
|
||||||
markwon.setMarkdown(tv, text)
|
markwon.setMarkdown(tv, text)
|
||||||
|
|||||||
@@ -548,14 +548,36 @@ object RssApi {
|
|||||||
// 头像 / 二维码小图
|
// 头像 / 二维码小图
|
||||||
val avatarImage = Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE)
|
val avatarImage = Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
var out = markdown.lines().filter { line ->
|
// 代码块先占位:逐行处理会 trim 掉行首缩进,代码缩进一旦丢了就没法看
|
||||||
|
val codeList = mutableListOf<String>()
|
||||||
|
val md = Regex("```[\\s\\S]*?```").replace(markdown) { m ->
|
||||||
|
codeList.add(m.value)
|
||||||
|
"\u0000C${codeList.size - 1}\u0000"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关键:空行是段落边界,必须保留。
|
||||||
|
// 之前这里把空行也 filter 掉了,所有段落粘成一段——正文才会变成
|
||||||
|
// 「密密麻麻一大坨文字」,净化写得再干净也没有排版。
|
||||||
|
val kept = ArrayList<String>()
|
||||||
|
var pendingBlank = false
|
||||||
|
for (line in md.lines()) {
|
||||||
val t = line.trim()
|
val t = line.trim()
|
||||||
if (t.isEmpty()) return@filter false
|
if (t.isEmpty()) {
|
||||||
if (anchorLine.matches(t) || urlOnly.matches(t)) return@filter false
|
if (kept.isNotEmpty()) pendingBlank = true
|
||||||
if (tocTitle.matches(t) || counterLine.matches(t) || avatarImage.matches(t)) return@filter false
|
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))
|
if (t.length < 100 && keyRe.containsMatchIn(t)) continue
|
||||||
}.joinToString("\n")
|
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*)+$"), "")
|
out = out.replace(Regex("(?:\\n\\s*(?:-{3,}|\\*{3,}|_{3,})\\s*)+$"), "")
|
||||||
@@ -565,7 +587,21 @@ object RssApi {
|
|||||||
.trim()
|
.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 轻量 HTML → Markdown,够阅读用,不追求完整 CommonMark */
|
/**
|
||||||
|
* 轻量 HTML → Markdown。
|
||||||
|
*
|
||||||
|
* 排版的关键不是「转成 markdown」,而是**保住原文的块结构**:
|
||||||
|
* 段落、标题、列表、引用、表格、图片各自独立成块,块与块之间留空行。
|
||||||
|
*
|
||||||
|
* 上一版只在 `</p>` 处断段,凡是拿 `<div>`、`<section>`、`<br>` 分段的站点
|
||||||
|
* (国内主题很常见)就全被压成一整段——这就是「净化后没有排版、密密麻麻」的真因。
|
||||||
|
*
|
||||||
|
* 三个占位符贯穿全程,因为文本节点里的源码换行必须折叠成空格(浏览器本来就这么显示),
|
||||||
|
* 不折叠的话 HTML 源码每 80 字一折行,读起来就是满屏断句:
|
||||||
|
* - `\u0001` → 段落分隔 `\n\n`
|
||||||
|
* - `\u0002` → 结构换行 `\n`(列表项、标题行)
|
||||||
|
* - `\u0003` → 硬换行(`<br>`,用 CommonMark 的行尾反斜杠)
|
||||||
|
*/
|
||||||
private fun htmlToMarkdown(raw: String): String {
|
private fun htmlToMarkdown(raw: String): String {
|
||||||
var s = raw
|
var s = raw
|
||||||
s = s.replace(Regex("<script[\\s\\S]*?</script>", RegexOption.IGNORE_CASE), "")
|
s = s.replace(Regex("<script[\\s\\S]*?</script>", RegexOption.IGNORE_CASE), "")
|
||||||
@@ -578,58 +614,150 @@ object RssApi {
|
|||||||
val codeBlocks = mutableListOf<String>()
|
val codeBlocks = mutableListOf<String>()
|
||||||
s = Regex("<pre\\b[^>]*>([\\s\\S]*?)</pre>", RegexOption.IGNORE_CASE).replace(s) { m ->
|
s = Regex("<pre\\b[^>]*>([\\s\\S]*?)</pre>", RegexOption.IGNORE_CASE).replace(s) { m ->
|
||||||
codeBlocks.add(unescape(stripTags(m.groupValues[1])).trim('\n'))
|
codeBlocks.add(unescape(stripTags(m.groupValues[1])).trim('\n'))
|
||||||
"\u0000P${codeBlocks.size - 1}\u0000"
|
"\u0001\u0000P${codeBlocks.size - 1}\u0000\u0001"
|
||||||
|
}
|
||||||
|
// 引用块整块处理:内部段落分隔要带上 ">",否则引用看起来只剩第一句
|
||||||
|
s = Regex("<blockquote\\b[^>]*>([\\s\\S]*?)</blockquote>", RegexOption.IGNORE_CASE).replace(s) { m ->
|
||||||
|
val inner = m.groupValues[1]
|
||||||
|
.replace(Regex("</p\\s*>|<br\\s*/?>", RegexOption.IGNORE_CASE), "\u0002> ")
|
||||||
|
"\u0001> " + inner.trim() + "\u0001"
|
||||||
}
|
}
|
||||||
// 懒加载图片:先取 data-* 里的地址
|
// 懒加载图片:先取 data-* 里的地址
|
||||||
s = s.replace(
|
s = s.replace(
|
||||||
Regex("<img[^>]*?(?:data-src|data-original|data-actualsrc|data-lazy-src)=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE),
|
Regex("<img[^>]*?(?:data-src|data-original|data-actualsrc|data-lazy-src)=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE),
|
||||||
) { m -> "<img src=\"${m.groupValues[1]}\">" }
|
) { m -> "<img src=\"${m.groupValues[1]}\">" }
|
||||||
for (level in 1..6) {
|
|
||||||
s = s.replace(
|
// 行内顺序很关键:先把 a / img 落成 markdown,再处理 strong / em,
|
||||||
Regex("<h$level[^>]*>([\\s\\S]*?)</h$level>", RegexOption.IGNORE_CASE),
|
// 否则 strong 内部的 stripTags 会把刚转好的链接啃成纯文字
|
||||||
) { m -> "\n\n${"#".repeat(level)} ${unescape(stripTags(m.groupValues[1])).trim()}\n\n" }
|
|
||||||
}
|
|
||||||
s = s.replace(Regex("<(strong|b)\\b[^>]*>([\\s\\S]*?)</\\1>", 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]*?)</\\1>", RegexOption.IGNORE_CASE)) { m ->
|
|
||||||
val t = stripTags(m.groupValues[2]).trim()
|
|
||||||
if (t.isEmpty()) "" else "*$t*"
|
|
||||||
}
|
|
||||||
s = s.replace(Regex("<code\\b[^>]*>([\\s\\S]*?)</code>", RegexOption.IGNORE_CASE)) { m ->
|
s = s.replace(Regex("<code\\b[^>]*>([\\s\\S]*?)</code>", RegexOption.IGNORE_CASE)) { m ->
|
||||||
val t = unescape(stripTags(m.groupValues[1]))
|
val t = unescape(stripTags(m.groupValues[1]))
|
||||||
if (t.isBlank()) "" else "`$t`"
|
if (t.isBlank()) "" else "`$t`"
|
||||||
}
|
}
|
||||||
// 链接与图片:属性单引号、双引号都认
|
|
||||||
s = s.replace(Regex("<a\\b[^>]*href=[\"']([^\"']+)[\"'][^>]*>([\\s\\S]*?)</a>", RegexOption.IGNORE_CASE)) { m ->
|
s = s.replace(Regex("<a\\b[^>]*href=[\"']([^\"']+)[\"'][^>]*>([\\s\\S]*?)</a>", RegexOption.IGNORE_CASE)) { m ->
|
||||||
val text = unescape(stripTags(m.groupValues[2])).trim()
|
val text = unescape(stripTags(m.groupValues[2])).trim()
|
||||||
if (text.isEmpty()) "" else "[$text](${m.groupValues[1].trim()})"
|
if (text.isEmpty()) "" else "[$text](${m.groupValues[1].trim()})"
|
||||||
}
|
}
|
||||||
s = s.replace(Regex("<img[^>]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m ->
|
s = s.replace(Regex("<img\\b[^>]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m ->
|
||||||
"\n\n\n\n"
|
val alt = unescape(attrValue(m.value, "alt")).replace(Regex("\\s+"), " ").trim()
|
||||||
|
"\u0001\u0001"
|
||||||
}
|
}
|
||||||
s = s.replace(Regex("<li\\b[^>]*>", RegexOption.IGNORE_CASE), "\n- ")
|
s = s.replace(Regex("<(strong|b)\\b[^>]*>([\\s\\S]*?)</\\1>", 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]*?)</\\1>", 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("<h$level[^>]*>([\\s\\S]*?)</h$level>", RegexOption.IGNORE_CASE),
|
||||||
|
) { m -> "\u0001${"#".repeat(level)} ${unescape(stripTags(m.groupValues[1])).trim()}\u0001" }
|
||||||
|
}
|
||||||
|
// 表格整块转成 markdown 表:留到最后处理只会被打散成一行行文字
|
||||||
|
val tables = mutableListOf<String>()
|
||||||
|
s = Regex("<table\\b[^>]*>([\\s\\S]*?)</table>", 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("<ol\\b[^>]*>([\\s\\S]*?)</ol>", RegexOption.IGNORE_CASE).replace(s) { m ->
|
||||||
|
var i = 0
|
||||||
|
val body = Regex("<li\\b[^>]*>([\\s\\S]*?)</li\\s*>", RegexOption.IGNORE_CASE)
|
||||||
|
.replace(m.groupValues[1]) { li ->
|
||||||
|
i++
|
||||||
|
"\u0002$i. " + li.groupValues[1]
|
||||||
|
}
|
||||||
|
"\u0001" + body.trim() + "\u0001"
|
||||||
|
}
|
||||||
|
s = s.replace(Regex("<li\\b[^>]*>", RegexOption.IGNORE_CASE), "\u0002- ")
|
||||||
s = s.replace(Regex("</li\\s*>", RegexOption.IGNORE_CASE), "")
|
s = s.replace(Regex("</li\\s*>", RegexOption.IGNORE_CASE), "")
|
||||||
s = s.replace(Regex("<blockquote\\b[^>]*>", RegexOption.IGNORE_CASE), "\n\n> ")
|
// 两个以上 <br> 当分段,单个当硬换行
|
||||||
s = s.replace(Regex("</blockquote\\s*>", RegexOption.IGNORE_CASE), "\n\n")
|
s = s.replace(Regex("(?:<br\\s*/?>\\s*){2,}", RegexOption.IGNORE_CASE), "\u0001")
|
||||||
s = s.replace(Regex("<br\\s*/?>", RegexOption.IGNORE_CASE), "\n")
|
s = s.replace(Regex("<br\\s*/?>", RegexOption.IGNORE_CASE), "\u0003")
|
||||||
// 分隔线没有阅读价值,直接去掉(后置的按行过滤还会再兜一次)
|
|
||||||
s = s.replace(Regex("<hr\\b[^>]*>", RegexOption.IGNORE_CASE), "")
|
s = s.replace(Regex("<hr\\b[^>]*>", RegexOption.IGNORE_CASE), "")
|
||||||
|
// 图注单独一行
|
||||||
|
s = s.replace(Regex("<figcaption\\b[^>]*>", RegexOption.IGNORE_CASE), "\u0001*")
|
||||||
|
s = s.replace(Regex("</figcaption\\s*>", RegexOption.IGNORE_CASE), "*\u0001")
|
||||||
|
// 块级容器开合都算段落边界:div / section / p 是原文最主要的断段来源
|
||||||
s = s.replace(
|
s = s.replace(
|
||||||
Regex("</(?:p|div|section|ul|ol|pre|h[1-6]|figure|figcaption|table|tr)\\s*>", RegexOption.IGNORE_CASE),
|
Regex(
|
||||||
"\n\n",
|
"</(?:p|div|section|article|ul|ol|pre|h[1-6]|figure|table|tr|center|main|dl|dd|dt)\\s*>",
|
||||||
|
RegexOption.IGNORE_CASE,
|
||||||
|
),
|
||||||
|
"\u0001",
|
||||||
)
|
)
|
||||||
s = s.replace(Regex("<td\\b[^>]*>", 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("</t[hd]\\s*>", RegexOption.IGNORE_CASE), " | ")
|
||||||
|
s = s.replace(Regex("</tr\\s*>", RegexOption.IGNORE_CASE), "\u0001")
|
||||||
|
|
||||||
s = stripTags(s)
|
s = stripTags(s)
|
||||||
s = unescape(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 ->
|
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()
|
return s.replace(Regex("\\n{3,}"), "\n\n").trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `<table>` → Markdown 表格。
|
||||||
|
*
|
||||||
|
* 只有一列的表格按段落输出(很多主题用 table 做布局,硬转成表格反而难看);
|
||||||
|
* 两列以上按标准 pipe 表输出,首行做表头、补一行分隔线。
|
||||||
|
*/
|
||||||
|
private fun tableToMarkdown(html: String): String {
|
||||||
|
val rows = Regex("<tr\\b[^>]*>([\\s\\S]*?)</tr\\s*>", RegexOption.IGNORE_CASE)
|
||||||
|
.findAll(html)
|
||||||
|
.map { tr ->
|
||||||
|
Regex("<t[hd]\\b[^>]*>([\\s\\S]*?)</t[hd]\\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
|
private fun unescape(s: String): String = s
|
||||||
.replace(" ", " ")
|
.replace(" ", " ")
|
||||||
|
|||||||
Reference in New Issue
Block a user