v3.29–v3.34:RSS 阅读页净化、批量选择与左滑、未读角标、邮件多账户聚合与排版修复

- 同步 HomeActivity/MailDetailActivity/MainActivity/ReaderActivity/RssApi 及 activity_home、activity_main 布局
- 更新 CHANGELOG 至 v3.34(versionCode 55)
This commit is contained in:
XIGE
2026-11-26 18:22:03 +08:00
parent d959ffed68
commit 1373fc014b
8 changed files with 623 additions and 201 deletions

View File

@@ -49,6 +49,11 @@ class ReaderActivity : AppCompatActivity() {
private var originalUrl: String = ""
/** 顶栏 meta 的三段信息:来源站点、作者、发布时间 */
private var metaSource: String = ""
private var metaAuthor: String = ""
private var metaDate: Long = 0L
companion object {
private const val EXTRA_KIND = "kind"
private const val EXTRA_ID = "id"
@@ -120,13 +125,9 @@ class ReaderActivity : AppCompatActivity() {
originalUrl = intent.getStringExtra(EXTRA_LINK).orEmpty()
tvTitle.text = title.ifBlank { getString(R.string.reader_untitled) }
tvMeta.text = buildString {
if (source.isNotEmpty()) append(source)
if (date > 0) {
if (isNotEmpty()) append(" · ")
append(formatDate(date))
}
}
metaSource = source
metaDate = date
renderMeta()
findViewById<ImageView>(R.id.btnReaderBack).setOnClickListener { finish() }
btnOriginal.setOnClickListener {
@@ -176,8 +177,13 @@ class ReaderActivity : AppCompatActivity() {
progress.visibility = TextView.VISIBLE
lifecycleScope.launch {
try {
val markdown = withContext(Dispatchers.IO) { RssApi.article(originalUrl) }
val body = stripDuplicateHeader(markdown.ifBlank { excerpt })
val article = withContext(Dispatchers.IO) { RssApi.article(originalUrl) }
// 作者提到顶栏,正文里那条「作者:xxx」就不必再出现
if (article.author.isNotBlank()) {
metaAuthor = article.author
renderMeta()
}
val body = stripLeadingNoise(article.markdown.ifBlank { excerpt })
render(body.ifBlank { excerpt.ifBlank { "(原文里没有抓到正文)" } })
} catch (e: Exception) {
// 抓不到原文时用站端摘要兜底,至少能读个大概
@@ -189,37 +195,123 @@ class ReaderActivity : AppCompatActivity() {
}
/**
* 原文页面的 article 标签里常带着站名标题和发布日期,
* 顶栏已经显示了这两样,开头再出现一遍就去掉;
* 开头的分类 / 标签 / 来源等 meta 行也一并清掉,只留正文。
* 顶栏信息行:来源 · 作者 · 发布时间。
*
* 「只留标题、作者、正文」——标题在页面最上方,作者与来源、时间收在这一行,
* 正文里就不再重复这些信息。
*/
private fun stripDuplicateHeader(markdown: String): String {
var s = markdown.trim()
val heading = Regex("^#{1,6}\\s*(.+?)[\\r\\n]+")
heading.find(s)?.let { m ->
val h = m.groupValues[1].trim()
val title = tvTitle.text.toString().trim()
if (title.isEmpty() || h == title || title.contains(h) || h.contains(title)) {
s = s.substring(m.range.last + 1).trim()
private fun renderMeta() {
val text = buildString {
if (metaSource.isNotBlank()) append(metaSource)
if (metaAuthor.isNotBlank()) {
if (isNotEmpty()) append(" · ")
append(metaAuthor)
}
if (metaDate > 0) {
if (isNotEmpty()) append(" · ")
append(formatDate(metaDate))
}
}
// 开头的日期行:2026-06-08、2026年6月8日、2026/06/08 09:46 等,可能连续几行 meta
val dateLine = Regex("^[\\r\\n]*\\d{4}[-/年]\\d{1,2}[-/月]\\d{1,2}日?[^\\n]{0,30}[\\r\\n]+")
while (true) {
val next = dateLine.replaceFirst(s, "").trim()
if (next == s || next.isEmpty()) break
s = next
tvMeta.text = text
tvMeta.visibility = if (text.isBlank()) View.GONE else View.VISIBLE
}
/**
* 正文开头的重复信息全部剥掉。
*
* 原文页面的 article 里常把「标题 + 日期 + 分类 + 作者」再写一遍,顶栏已经有了,
* 重复显示等于把同一段信息看两次。这里逐行往下剥,直到第一行是真正的正文:
* 重复标题(含 `# 标题`、`**标题**`、`标题 - 站点名`)、日期行、分类/标签/作者/阅读量
* 等 meta 行、头像小图、开头的分隔线都算噪音。
*/
private fun stripLeadingNoise(markdown: String): String {
val title = tvTitle.text.toString().trim()
var s = markdown.trim()
var guard = 0
while (guard++ < 16) {
val lines = s.lines()
val idx = lines.indexOfFirst { it.isNotBlank() }
if (idx < 0) return ""
if (!isHeaderNoise(lines[idx].trim(), title)) break
s = lines.drop(idx + 1).joinToString("\n").trim()
}
// 开头的 meta 行:作者 / 分类 / 标签 / 来源 / 字数 / 阅读次数
val metaLine = Regex(
"^[\\r\\n]*(作者|分类|标签|来源|发布于|发表于|更新于|字数|阅读|浏览|字体大小)[::\\s][^\\n]{0,40}[\\r\\n]+",
)
while (true) {
val next = metaLine.replaceFirst(s, "").trim()
if (next == s || next.isEmpty()) break
s = next
// 正文中部再出现一次同名一级标题,同样是重复
s = removeRedundantH1(s, title)
return stripTrailingNoise(s)
}
/** 这一行是不是「顶栏已经显示过」的信息 */
private fun isHeaderNoise(line: String, title: String): Boolean {
if (line.isEmpty()) return false
val plain = line
.replace(Regex("^#{1,6}\\s*"), "")
.replace(Regex("^\\*\\*(.*)\\*\\*$"), "$1")
.replace(Regex("^__(.*)__$"), "$1")
.replace(Regex("^\\[(.*)\\]\\([^)]*\\)$"), "$1")
.trim()
if (plain.isEmpty()) return true
if (Regex("^(?:-{3,}|\\*{3,}|_{3,})$").matches(plain)) return true
// 1) 重复标题
if (title.length >= 4) {
val t = title.lowercase()
val p = plain.lowercase()
if (p == t) return true
// 「主标题 - 站点名」「标题 | 分类」这类拼接也算重复
if (p.length <= t.length + 24 && (p.startsWith(t) || p.contains(t))) return true
}
return s.trim()
// 2) 日期行:2026-06-08 / 2026年6月8日 / 2026/06/08 09:46
if (plain.length < 70 &&
Regex("^\\d{4}[-/年.]\\d{1,2}[-/月.]\\d{1,2}日?(?:[^\\n]{0,40})$").matches(plain)
) return true
// 3) 一行里既带日期又带「·」分隔的 meta(日期 · 分类 · 阅读数)
if (plain.length < 90 && plain.contains('·') &&
Regex("\\d{4}[-/年]\\d{1,2}").containsMatchIn(plain)
) return true
// 4) 分类 / 标签 / 作者 / 来源 / 字数 / 阅读
if (Regex("^(?:作者|撰文|文|分类|标签|来源|发布于|发表于|更新于|字数|阅读|浏览|编辑|责编|摄影)[::\\s].{0,40}$")
.containsMatchIn(plain)
) return true
// 5) 阅读时长 / 字数统计
if (Regex("^(?:约?\\s*\\d+\\s*分钟(?:阅读)?|\\d+\\s*字(?:数)?|阅读\\s*\\d+|\\d+\\s*阅读)$")
.matches(plain)
) return true
// 6) 头像 / 二维码小图
if (Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE)
.matches(line)
) return true
return false
}
/** 正文里与标题同名的一级标题直接删除 */
private fun removeRedundantH1(markdown: String, title: String): String {
if (title.isBlank()) return markdown
val t = title.lowercase()
val h1 = Regex("^#\\s+(.+)$")
return markdown.lines().filterNot { line ->
val m = h1.find(line.trim())
m != null && m.groupValues[1].trim().lowercase() == t
}.joinToString("\n")
}
/** 结尾的分隔线与「一串标签」行没有阅读价值 */
private fun stripTrailingNoise(markdown: String): String {
val s = markdown
.replace(Regex("(?:\\n\\s*(?:-{3,}|\\*{3,}|_{3,})\\s*)+$"), "")
.trimEnd()
val lines = s.lines().toMutableList()
while (lines.isNotEmpty()) {
val last = lines.last().trim()
if (last.isEmpty()) {
lines.removeAt(lines.size - 1)
continue
}
if (Regex("^(?:#[^\\s#]+\\s*){2,}$").matches(last)) {
lines.removeAt(lines.size - 1)
continue
}
break
}
return lines.joinToString("\n").trimEnd()
}
private fun String.ifNotBlank(): String? = takeIf { isNotBlank() }