v3.39(versionCode 60)
This commit is contained in:
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,5 +1,19 @@
|
|||||||
# 变更记录
|
# 变更记录
|
||||||
|
|
||||||
|
## 2026-11-27 · v3.39(versionCode 60)
|
||||||
|
|
||||||
|
- 阅读页代码块改为独立代码卡:左侧行号、语法高亮、内容可横向滚动,顶部标注语言;长按整段复制。
|
||||||
|
- 新增本地语法高亮 `CodeHighlight`:一趟扫描识别注释(`//`、`#`、`/* */`)、字符串(含三引号跨行)、数字、关键字、注解与函数调用;配色分深浅两套并跟随夜间模式。未引入 Prism4j,不增加依赖和包体。
|
||||||
|
- 行号与代码放进同一个 TextView:中文注释回退字体导致行高变化时也不会错位半行。
|
||||||
|
- `RssApi` 从 `<pre><code class="language-xxx">` 提取语言标注,随围栏代码块一起带到阅读页。
|
||||||
|
- bluehe.cn(sujian 主题)按站点规则取正文容器 `.song`:页头封面、标题、发布时间、分类/阅读量/评论数,以及文末版权许可、继续阅读、上一篇下一篇不再混进正文。
|
||||||
|
- 站点页面的「分类 · 阅读 · 评论」提取后补进顶栏 meta 行,正文里不再重复。
|
||||||
|
- 修复 `a` 标签内只包一张图(灯箱)时整块被丢弃、正文配图跟着消失的问题(HTML 转 Markdown 与阅读页 HTML 兜底两处)。
|
||||||
|
- 噪音区块补充 `nextnav` / `prevnext` / `keepreading` / `post-pic` 等命名,`pagination2` 这类带序号的按前缀匹配;行级过滤补充「继续阅读 · KEEP READING」「许可:」「CC BY」「本文原创」和日期打头的 meta 行。
|
||||||
|
- 顶栏菜单文案:「查看原文」改为「原文」,「作者主页」改为「主页」。
|
||||||
|
- AI 摘要框的标签最多显示三个(AI 返回顺序即重要性)。
|
||||||
|
- 修复浅色模式下图片放大后顶部留一条白:对话框窗口压黑、内容延伸到系统栏并隐藏状态栏,深浅色都是整屏黑底。
|
||||||
|
|
||||||
## 2026-11-27 · v3.38(versionCode 59)
|
## 2026-11-27 · v3.38(versionCode 59)
|
||||||
|
|
||||||
- 阅读页右上角「查看原文」改为「更多」,点开是下拉菜单:查看原文、作者主页(没有原文链接时该项不出现)。
|
- 阅读页右上角「查看原文」改为「更多」,点开是下拉菜单:查看原文、作者主页(没有原文链接时该项不出现)。
|
||||||
|
|||||||
226
src/main/java/com/shitoucuo/postpad/CodeHighlight.kt
Normal file
226
src/main/java/com/shitoucuo/postpad/CodeHighlight.kt
Normal file
@@ -0,0 +1,226 @@
|
|||||||
|
package com.shitoucuo.postpad
|
||||||
|
|
||||||
|
import android.text.Spannable
|
||||||
|
import android.text.SpannableStringBuilder
|
||||||
|
import android.text.style.ForegroundColorSpan
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码块语法高亮。
|
||||||
|
*
|
||||||
|
* 没有引 Prism4j:它要额外依赖 + 每种语言一个定义包,离线构建拿不到、包体也要涨几 MB。
|
||||||
|
* 这里一趟扫描覆盖日常阅读会遇到的大部分代码(C 系 / Java / Kotlin / JS / TS /
|
||||||
|
* Python / Go / Rust / Shell / SQL / JSON / 配置片段):注释、字符串、数字、
|
||||||
|
* 关键字、注解各有颜色,认不出的按正文色。
|
||||||
|
*
|
||||||
|
* 配色分深浅两套并由调用方传入:深色底上用亮色、浅色底上用深色,
|
||||||
|
* 固定一套配色总有一半场景是糊的。
|
||||||
|
*/
|
||||||
|
object CodeHighlight {
|
||||||
|
|
||||||
|
class Palette(
|
||||||
|
val keyword: Int,
|
||||||
|
val string: Int,
|
||||||
|
val comment: Int,
|
||||||
|
val number: Int,
|
||||||
|
val annotation: Int,
|
||||||
|
val function: Int,
|
||||||
|
)
|
||||||
|
|
||||||
|
/** 浅色底:深色字 + 高对比强调色(参考 GitHub Light) */
|
||||||
|
val LIGHT = Palette(
|
||||||
|
keyword = 0xFF8250DF.toInt(),
|
||||||
|
string = 0xFF0A7D33.toInt(),
|
||||||
|
comment = 0xFF7A828E.toInt(),
|
||||||
|
number = 0xFFB35900.toInt(),
|
||||||
|
annotation = 0xFFB25F35.toInt(),
|
||||||
|
function = 0xFF0550AE.toInt(),
|
||||||
|
)
|
||||||
|
|
||||||
|
/** 深色底:近白字 + 提亮的强调色(参考 One Dark) */
|
||||||
|
val DARK = Palette(
|
||||||
|
keyword = 0xFFC792EA.toInt(),
|
||||||
|
string = 0xFF9ECE6A.toInt(),
|
||||||
|
comment = 0xFF6F7784.toInt(),
|
||||||
|
number = 0xFFE0AF68.toInt(),
|
||||||
|
annotation = 0xFFD4814F.toInt(),
|
||||||
|
function = 0xFF7DCFFF.toInt(),
|
||||||
|
)
|
||||||
|
|
||||||
|
/** C 系预处理指令:`#include` 不是注释 */
|
||||||
|
private val PREPROC = listOf(
|
||||||
|
"include", "define", "undef", "ifdef", "ifndef", "endif", "pragma", "error", "import",
|
||||||
|
)
|
||||||
|
|
||||||
|
private val KEYWORDS = setOf(
|
||||||
|
// 通用 / C 系
|
||||||
|
"if", "else", "for", "while", "do", "switch", "case", "default", "break", "continue",
|
||||||
|
"return", "goto", "try", "catch", "finally", "throw", "throws", "new", "delete", "this",
|
||||||
|
"super", "null", "nil", "true", "false", "void", "static", "const", "constexpr", "final",
|
||||||
|
"abstract", "interface", "class", "struct", "enum", "union", "typedef", "namespace",
|
||||||
|
"using", "public", "private", "protected", "internal", "extends", "implements",
|
||||||
|
"instanceof", "typeof", "sizeof", "extern", "volatile", "synchronized", "transient",
|
||||||
|
"native", "package", "import", "export", "from", "as", "and", "or", "not", "is", "in",
|
||||||
|
// Kotlin / Java / C# / Swift / TS
|
||||||
|
"fun", "val", "var", "when", "out", "object", "data", "sealed", "companion", "init",
|
||||||
|
"suspend", "inline", "reified", "lateinit", "by", "override", "open", "operator",
|
||||||
|
"infix", "tailrec", "crossinline", "noinline", "typealias", "where", "field",
|
||||||
|
"let", "run", "apply", "also", "with", "async", "await", "yield", "function", "def",
|
||||||
|
"lambda", "string", "int", "long", "float", "double", "bool", "boolean", "char", "byte",
|
||||||
|
"short", "unsigned", "undefined", "never", "any", "unknown", "readonly", "declare",
|
||||||
|
"type", "keyof", "readonly", "override", "get", "set", "of",
|
||||||
|
// Python / Ruby / Lua / Shell
|
||||||
|
"elif", "except", "raise", "pass", "self", "cls", "global", "nonlocal", "assert", "del",
|
||||||
|
"None", "True", "False", "end", "then", "fi", "esac", "done", "local", "source",
|
||||||
|
"echo", "print", "require", "include_once", "elseif", "unless", "until",
|
||||||
|
// Go / Rust
|
||||||
|
"func", "defer", "go", "chan", "select", "range", "impl", "mut", "pub", "crate", "match",
|
||||||
|
"loop", "unsafe", "dyn", "trait", "use", "mod", "ref", "Some", "Ok", "Err", "fn",
|
||||||
|
// SQL
|
||||||
|
"select", "from", "where", "insert", "update", "delete", "join", "group", "order",
|
||||||
|
"limit", "create", "table", "alter", "drop", "index", "values", "into", "set",
|
||||||
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 逐行高亮。
|
||||||
|
*
|
||||||
|
* 按行返回是为了让调用方在每行前面接行号——行号与代码放进同一个 TextView,
|
||||||
|
* 中文注释回退字体时也不会出现「行号比代码高半行」的错位。
|
||||||
|
* 块注释与三引号字符串的状态跨行传递,所以这里由同一个 Lexer 顺序处理。
|
||||||
|
*/
|
||||||
|
fun highlightLines(code: String, palette: Palette): List<CharSequence> {
|
||||||
|
val lexer = Lexer(palette)
|
||||||
|
return code.split('\n').map { lexer.line(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Lexer(private val p: Palette) {
|
||||||
|
|
||||||
|
private var inBlockComment = false
|
||||||
|
private var tripleQuote: String? = null
|
||||||
|
|
||||||
|
fun line(src: String): CharSequence {
|
||||||
|
val out = SpannableStringBuilder(src)
|
||||||
|
var i = 0
|
||||||
|
val n = src.length
|
||||||
|
while (i < n) {
|
||||||
|
// 上一行遗留的块注释
|
||||||
|
if (inBlockComment) {
|
||||||
|
val end = src.indexOf("*/", i)
|
||||||
|
if (end < 0) {
|
||||||
|
mark(out, i, n, p.comment)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
mark(out, i, end + 2, p.comment)
|
||||||
|
inBlockComment = false
|
||||||
|
i = end + 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 上一行遗留的三引号字符串
|
||||||
|
val pendingQuote = tripleQuote
|
||||||
|
if (pendingQuote != null) {
|
||||||
|
val end = src.indexOf(pendingQuote, i)
|
||||||
|
if (end < 0) {
|
||||||
|
mark(out, i, n, p.string)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
mark(out, i, end + 3, p.string)
|
||||||
|
tripleQuote = null
|
||||||
|
i = end + 3
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val c = src[i]
|
||||||
|
// 块注释开头
|
||||||
|
if (c == '/' && i + 1 < n && src[i + 1] == '*') {
|
||||||
|
val end = src.indexOf("*/", i + 2)
|
||||||
|
if (end < 0) {
|
||||||
|
mark(out, i, n, p.comment)
|
||||||
|
inBlockComment = true
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
mark(out, i, end + 2, p.comment)
|
||||||
|
i = end + 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 行注释
|
||||||
|
if (c == '/' && i + 1 < n && src[i + 1] == '/') {
|
||||||
|
mark(out, i, n, p.comment)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
// 三引号字符串(Kotlin """ / Python '''):允许跨行
|
||||||
|
if ((c == '"' || c == '\'') && i + 2 < n && src[i + 1] == c && src[i + 2] == c) {
|
||||||
|
val q = StringBuilder().append(c).append(c).append(c).toString()
|
||||||
|
val end = src.indexOf(q, i + 3)
|
||||||
|
if (end < 0) {
|
||||||
|
mark(out, i, n, p.string)
|
||||||
|
tripleQuote = q
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
mark(out, i, end + 3, p.string)
|
||||||
|
i = end + 3
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 普通字符串
|
||||||
|
if (c == '"' || c == '\'' || c == '`') {
|
||||||
|
var j = i + 1
|
||||||
|
while (j < n) {
|
||||||
|
if (src[j] == '\\' && j + 1 < n) {
|
||||||
|
j += 2
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (src[j] == c) {
|
||||||
|
j++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
mark(out, i, j, p.string)
|
||||||
|
i = j
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// # 注释 / 预处理指令(只在行首或空白后生效,避免切到 a#b)
|
||||||
|
if (c == '#' && (i == 0 || src[i - 1].isWhitespace())) {
|
||||||
|
val rest = src.substring(i + 1).trimStart()
|
||||||
|
if (PREPROC.any { rest.startsWith(it) }) {
|
||||||
|
mark(out, i, n, p.annotation)
|
||||||
|
} else {
|
||||||
|
mark(out, i, n, p.comment)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
// 数字
|
||||||
|
if (c.isDigit() && (i == 0 || !(src[i - 1].isLetterOrDigit() || src[i - 1] == '_'))) {
|
||||||
|
var j = i
|
||||||
|
while (j < n && (src[j].isLetterOrDigit() || src[j] == '.' || src[j] == '_')) j++
|
||||||
|
mark(out, i, j, p.number)
|
||||||
|
i = j
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 标识符 / 关键字 / 函数名 / 注解
|
||||||
|
if (c.isLetter() || c == '_' || c == '@' || c == '$') {
|
||||||
|
var j = if (c == '@') i + 1 else i
|
||||||
|
while (j < n && (src[j].isLetterOrDigit() || src[j] == '_' || src[j] == '$')) j++
|
||||||
|
val word = src.substring(if (c == '@') i + 1 else i, j)
|
||||||
|
when {
|
||||||
|
c == '@' -> mark(out, i, j, p.annotation)
|
||||||
|
word in KEYWORDS -> mark(out, i, j, p.keyword)
|
||||||
|
// 后面紧跟左括号:函数 / 方法调用
|
||||||
|
j < n && src[j] == '(' -> mark(out, i, j, p.function)
|
||||||
|
}
|
||||||
|
i = if (j > i) j else i + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun mark(out: SpannableStringBuilder, start: Int, end: Int, color: Int) {
|
||||||
|
if (end > start && start >= 0) {
|
||||||
|
out.setSpan(
|
||||||
|
ForegroundColorSpan(color), start, end,
|
||||||
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,30 @@
|
|||||||
package com.shitoucuo.postpad
|
package com.shitoucuo.postpad
|
||||||
|
|
||||||
|
import android.content.ClipData
|
||||||
|
import android.content.ClipboardManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.res.Configuration
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.Outline
|
import android.graphics.Outline
|
||||||
|
import android.graphics.Typeface
|
||||||
|
import android.graphics.drawable.ColorDrawable
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
|
import android.text.Spannable
|
||||||
|
import android.text.SpannableStringBuilder
|
||||||
import android.text.method.LinkMovementMethod
|
import android.text.method.LinkMovementMethod
|
||||||
|
import android.text.style.ForegroundColorSpan
|
||||||
import android.util.LruCache
|
import android.util.LruCache
|
||||||
import android.view.Gravity
|
import android.view.Gravity
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.ViewOutlineProvider
|
import android.view.ViewOutlineProvider
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
|
import android.widget.HorizontalScrollView
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
import android.widget.LinearLayout
|
import android.widget.LinearLayout
|
||||||
import android.widget.ListView
|
import android.widget.ListView
|
||||||
@@ -26,6 +35,9 @@ import android.widget.Toast
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.view.WindowCompat
|
||||||
|
import androidx.core.view.WindowInsetsCompat
|
||||||
|
import androidx.core.view.WindowInsetsControllerCompat
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import io.noties.markwon.Markwon
|
import io.noties.markwon.Markwon
|
||||||
import io.noties.markwon.image.ImagesPlugin
|
import io.noties.markwon.image.ImagesPlugin
|
||||||
@@ -63,6 +75,9 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
private var metaAuthor: String = ""
|
private var metaAuthor: String = ""
|
||||||
private var metaDate: Long = 0L
|
private var metaDate: Long = 0L
|
||||||
|
|
||||||
|
/** 站点页面上的补充元信息(分类 · 阅读 · 评论),原文抓到后才有 */
|
||||||
|
private var metaExtra: String = ""
|
||||||
|
|
||||||
/** 来源站点信息:作者主页按站点过滤,并用站点首页做「打开网站」 */
|
/** 来源站点信息:作者主页按站点过滤,并用站点首页做「打开网站」 */
|
||||||
private var siteUrl: String = ""
|
private var siteUrl: String = ""
|
||||||
private var urlId: Long = 0L
|
private var urlId: Long = 0L
|
||||||
@@ -217,6 +232,11 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
metaAuthor = article.author
|
metaAuthor = article.author
|
||||||
renderMeta()
|
renderMeta()
|
||||||
}
|
}
|
||||||
|
// 分类 / 阅读量 / 评论数同样收进顶栏,正文里不再重复
|
||||||
|
if (article.metaExtra.isNotBlank()) {
|
||||||
|
metaExtra = article.metaExtra
|
||||||
|
renderMeta()
|
||||||
|
}
|
||||||
val body = stripLeadingNoise(article.markdown.ifBlank { excerpt })
|
val body = stripLeadingNoise(article.markdown.ifBlank { excerpt })
|
||||||
renderWithBrief(body.ifBlank { excerpt.ifBlank { "(原文里没有抓到正文)" } })
|
renderWithBrief(body.ifBlank { excerpt.ifBlank { "(原文里没有抓到正文)" } })
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@@ -245,6 +265,11 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
if (isNotEmpty()) append(" · ")
|
if (isNotEmpty()) append(" · ")
|
||||||
append(formatDate(metaDate))
|
append(formatDate(metaDate))
|
||||||
}
|
}
|
||||||
|
// 分类 · 阅读 · 评论:站点详情页才有的信息,补在最后
|
||||||
|
if (metaExtra.isNotBlank()) {
|
||||||
|
if (isNotEmpty()) append(" · ")
|
||||||
|
append(metaExtra)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tvMeta.text = text
|
tvMeta.text = text
|
||||||
tvMeta.visibility = if (text.isBlank()) View.GONE else View.VISIBLE
|
tvMeta.visibility = if (text.isBlank()) View.GONE else View.VISIBLE
|
||||||
@@ -366,7 +391,7 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
.trim()
|
.trim()
|
||||||
.length
|
.length
|
||||||
|
|
||||||
/** AI 给的标签优先,再并上条目自带的,去重后最多 8 个 */
|
/** AI 给的标签优先,再并上条目自带的,去重后最多 3 个 */
|
||||||
private fun mergeTags(aiTags: List<String>): List<String> {
|
private fun mergeTags(aiTags: List<String>): List<String> {
|
||||||
val out = ArrayList<String>()
|
val out = ArrayList<String>()
|
||||||
(aiTags + itemTags).forEach { raw ->
|
(aiTags + itemTags).forEach { raw ->
|
||||||
@@ -374,7 +399,8 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
if (tag.isEmpty() || tag.length > 14) return@forEach
|
if (tag.isEmpty() || tag.length > 14) return@forEach
|
||||||
if (out.none { it.equals(tag, true) }) out.add(tag)
|
if (out.none { it.equals(tag, true) }) out.add(tag)
|
||||||
}
|
}
|
||||||
return out.take(8)
|
// 摘要框只放最重要的三个:AI 返回的顺序就是重要性,条目自带标签补在后面
|
||||||
|
return out.take(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun requestBrief() {
|
private fun requestBrief() {
|
||||||
@@ -665,11 +691,20 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
.replace(Regex("<(em|i)\\b[^>]*>", RegexOption.IGNORE_CASE), "*")
|
.replace(Regex("<(em|i)\\b[^>]*>", RegexOption.IGNORE_CASE), "*")
|
||||||
.replace(Regex("</(em|i)\\s*>", RegexOption.IGNORE_CASE), "*")
|
.replace(Regex("</(em|i)\\s*>", RegexOption.IGNORE_CASE), "*")
|
||||||
.replace(Regex("<blockquote\\b[^>]*>", RegexOption.IGNORE_CASE), "\n> ")
|
.replace(Regex("<blockquote\\b[^>]*>", RegexOption.IGNORE_CASE), "\n> ")
|
||||||
// 链接
|
// 链接:只包着一张图的(灯箱),链接没有文字,直接留内层,别把图吃掉
|
||||||
s = Regex(
|
s = Regex(
|
||||||
"<a\\b[^>]*href\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>([\\s\\S]*?)</a\\s*>",
|
"<a\\b[^>]*href\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>([\\s\\S]*?)</a\\s*>",
|
||||||
RegexOption.IGNORE_CASE,
|
RegexOption.IGNORE_CASE,
|
||||||
).replace(s) { m -> "[${m.groupValues[2]}](${m.groupValues[1]})" }
|
).replace(s) { m ->
|
||||||
|
val inner = m.groupValues[2]
|
||||||
|
val text = inner.replace(Regex("<[^>]*>"), "").trim()
|
||||||
|
when {
|
||||||
|
text.isNotEmpty() -> "[$text](${m.groupValues[1]})"
|
||||||
|
inner.contains("![") || Regex("<img\\b", RegexOption.IGNORE_CASE).containsMatchIn(inner) ->
|
||||||
|
inner
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
// 剩余未知标签一律剥掉(含自闭合),正文不再露出 <span> 之类
|
// 剩余未知标签一律剥掉(含自闭合),正文不再露出 <span> 之类
|
||||||
s = s.replace(Regex("</?[a-zA-Z][^>]*>"), "")
|
s = s.replace(Regex("</?[a-zA-Z][^>]*>"), "")
|
||||||
s = s.replace(Regex("\\n{3,}"), "\n\n")
|
s = s.replace(Regex("\\n{3,}"), "\n\n")
|
||||||
@@ -743,6 +778,9 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
raw = tidyBlank(raw)
|
raw = tidyBlank(raw)
|
||||||
// 站里标题常写成 "#标题"(# 后无空格,CommonMark 不识别),补上空格;代码块内不动
|
// 站里标题常写成 "#标题"(# 后无空格,CommonMark 不识别),补上空格;代码块内不动
|
||||||
raw = ensureHeadingSpaces(raw)
|
raw = ensureHeadingSpaces(raw)
|
||||||
|
// 代码块单独成卡(行号 + 高亮):先抽成占位符,剩下的文字段才交给 Markwon
|
||||||
|
val codeBlocks = ArrayList<CodeBlock>()
|
||||||
|
raw = extractCodeBlocks(raw, codeBlocks)
|
||||||
var body = normalizeFlexTokens(raw)
|
var body = normalizeFlexTokens(raw)
|
||||||
// 收集文末引用定义行(站里的定义行带前导空格),供引用式图片解析
|
// 收集文末引用定义行(站里的定义行带前导空格),供引用式图片解析
|
||||||
val refs = Regex("(?m)^\\s*\\[([^\\]]+)\\]:\\s*(\\S+)")
|
val refs = Regex("(?m)^\\s*\\[([^\\]]+)\\]:\\s*(\\S+)")
|
||||||
@@ -759,20 +797,145 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
// 删掉引用定义行(文末 [1]: url 之类),不在正文里显示图片地址
|
// 删掉引用定义行(文末 [1]: url 之类),不在正文里显示图片地址
|
||||||
body = body.replace(Regex("(?m)^\\s*\\[[^\\]]+\\]:\\s*\\S+.*$"), "")
|
body = body.replace(Regex("(?m)^\\s*\\[[^\\]]+\\]:\\s*\\S+.*$"), "")
|
||||||
bodyContainer.removeAllViews()
|
bodyContainer.removeAllViews()
|
||||||
val tokenRe = Regex("%%(FLEXP|IMG)%%\\s*([\\s\\S]*?)\\s*%%/\\1%%")
|
val tokenRe = Regex("%%(FLEXP|IMG)%%\\s*([\\s\\S]*?)\\s*%%/\\1%%|%%CODE(\\d+)%%")
|
||||||
var last = 0
|
var last = 0
|
||||||
for (m in tokenRe.findAll(body)) {
|
for (m in tokenRe.findAll(body)) {
|
||||||
addTextSegment(body.substring(last, m.range.first).trim())
|
addTextSegment(body.substring(last, m.range.first).trim())
|
||||||
val urls = m.groupValues[2].lines().map { it.trim() }.filter { it.isNotEmpty() }
|
val codeIndex = m.groupValues[3]
|
||||||
when (m.groupValues[1]) {
|
if (codeIndex.isNotEmpty()) {
|
||||||
"IMG" -> urls.forEach { addFullImage(it) }
|
codeBlocks.getOrNull(codeIndex.toIntOrNull() ?: -1)
|
||||||
else -> if (urls.isNotEmpty()) addFlexPhotoRow(urls)
|
?.let { addCodeBlock(it.lang, it.code) }
|
||||||
|
} else {
|
||||||
|
val urls = m.groupValues[2].lines().map { it.trim() }.filter { it.isNotEmpty() }
|
||||||
|
when (m.groupValues[1]) {
|
||||||
|
"IMG" -> urls.forEach { addFullImage(it) }
|
||||||
|
else -> if (urls.isNotEmpty()) addFlexPhotoRow(urls)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
last = m.range.last + 1
|
last = m.range.last + 1
|
||||||
}
|
}
|
||||||
addTextSegment(body.substring(last).trim())
|
addTextSegment(body.substring(last).trim())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 抽出来的围栏代码块:语言标注 + 原始代码 */
|
||||||
|
private class CodeBlock(val lang: String, val code: String)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把 ``` 围栏代码块换成 `%%CODEn%%` 占位符。
|
||||||
|
*
|
||||||
|
* 交给 Markwon 渲染只能得到一段等宽文字,没有行号也没有高亮;
|
||||||
|
* 抽出来是为了按代码卡单独排版(行号列 + 高亮 + 横向滚动)。
|
||||||
|
* 围栏必须整行,正文里行内的反引号不会被误抽。
|
||||||
|
*/
|
||||||
|
private fun extractCodeBlocks(markdown: String, sink: MutableList<CodeBlock>): String =
|
||||||
|
Regex("(?m)^[ \\t]*```([^\\n`]*)\\n([\\s\\S]*?)^[ \\t]*```[ \\t]*$").replace(markdown) { m ->
|
||||||
|
val code = m.groupValues[2].trimEnd('\n')
|
||||||
|
if (code.isBlank()) {
|
||||||
|
""
|
||||||
|
} else {
|
||||||
|
sink.add(CodeBlock(m.groupValues[1].trim(), code))
|
||||||
|
"\n%%CODE${sink.size - 1}%%\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码卡:顶部语言标注 + 行号 + 语法高亮,内容可横向滚动。
|
||||||
|
*
|
||||||
|
* 行号与代码放进同一个 TextView:中文注释回退字体时行高会变,
|
||||||
|
* 两个 TextView 并排就一定会错位半行,合成一个文本永远不会错。
|
||||||
|
*/
|
||||||
|
private fun addCodeBlock(lang: String, code: String) {
|
||||||
|
if (code.isBlank()) return
|
||||||
|
val dark = isNightMode()
|
||||||
|
val palette = if (dark) CodeHighlight.DARK else CodeHighlight.LIGHT
|
||||||
|
val card = LinearLayout(this).apply {
|
||||||
|
orientation = LinearLayout.VERTICAL
|
||||||
|
setBackgroundResource(R.drawable.bg_code_card)
|
||||||
|
clipToOutline = true
|
||||||
|
}
|
||||||
|
if (lang.isNotBlank() && lang.length <= 16) {
|
||||||
|
val label = TextView(this).apply {
|
||||||
|
text = lang.lowercase()
|
||||||
|
textSize = 10f
|
||||||
|
letterSpacing = 0.08f
|
||||||
|
setTextColor(ContextCompat.getColor(context, R.color.code_lang))
|
||||||
|
setPadding(dp(14), dp(9), dp(14), 0)
|
||||||
|
}
|
||||||
|
card.addView(label)
|
||||||
|
}
|
||||||
|
val body = TextView(this).apply {
|
||||||
|
typeface = Typeface.MONOSPACE
|
||||||
|
textSize = 12.5f
|
||||||
|
includeFontPadding = false
|
||||||
|
setLineSpacing(0f, 1.42f)
|
||||||
|
setTextColor(ContextCompat.getColor(context, R.color.code_text))
|
||||||
|
text = buildCodeText(code, palette)
|
||||||
|
setPadding(dp(14), dp(10), dp(14), dp(12))
|
||||||
|
// 长按整段复制:代码卡里拖选很容易选到行号,整段复制更实用
|
||||||
|
setOnLongClickListener {
|
||||||
|
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||||
|
cm.setPrimaryClip(ClipData.newPlainText("code", code))
|
||||||
|
toast("代码已复制")
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val scroll = HorizontalScrollView(this).apply {
|
||||||
|
isHorizontalScrollBarEnabled = false
|
||||||
|
isFillViewport = true
|
||||||
|
addView(
|
||||||
|
body,
|
||||||
|
ViewGroup.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
card.addView(
|
||||||
|
scroll,
|
||||||
|
LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
bodyContainer.addView(
|
||||||
|
card,
|
||||||
|
LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
).apply {
|
||||||
|
topMargin = dp(10)
|
||||||
|
bottomMargin = dp(2)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 行号 + 高亮代码拼成一段 Spannable,行号用弱化色右对齐 */
|
||||||
|
private fun buildCodeText(code: String, palette: CodeHighlight.Palette): CharSequence {
|
||||||
|
val lines = code.split('\n')
|
||||||
|
val highlights = CodeHighlight.highlightLines(code, palette)
|
||||||
|
val linenoColor = ContextCompat.getColor(this, R.color.code_lineno)
|
||||||
|
val width = lines.size.toString().length
|
||||||
|
val sb = SpannableStringBuilder()
|
||||||
|
lines.forEachIndexed { index, _ ->
|
||||||
|
val gutter = (index + 1).toString().padStart(width, ' ') + " "
|
||||||
|
val start = sb.length
|
||||||
|
sb.append(gutter)
|
||||||
|
sb.setSpan(
|
||||||
|
ForegroundColorSpan(linenoColor), start, sb.length,
|
||||||
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
|
||||||
|
)
|
||||||
|
sb.append(highlights.getOrElse(index) { "" })
|
||||||
|
if (index < lines.size - 1) sb.append('\n')
|
||||||
|
}
|
||||||
|
return sb
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 当前是否深色:代码高亮配色跟主题走,不是写死一套 */
|
||||||
|
private fun isNightMode(): Boolean {
|
||||||
|
val mode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
|
||||||
|
return mode == Configuration.UI_MODE_NIGHT_YES
|
||||||
|
}
|
||||||
|
|
||||||
private fun addTextSegment(text: String) {
|
private fun addTextSegment(text: String) {
|
||||||
if (text.isBlank()) return
|
if (text.isBlank()) return
|
||||||
val tv = TextView(this).apply {
|
val tv = TextView(this).apply {
|
||||||
@@ -989,6 +1152,23 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
dialog.show()
|
dialog.show()
|
||||||
|
/* 看图要整屏纯黑。Dialog 的 window 默认不会盖住状态栏,浅色主题下顶上就
|
||||||
|
留一条白(「顶部一片空白、不是全黑」的来源)。这里把 window 背景压黑、
|
||||||
|
内容延伸到系统栏,再直接把状态栏隐藏,深色浅色都是同一块黑底。 */
|
||||||
|
dialog.window?.let { w ->
|
||||||
|
w.setBackgroundDrawable(ColorDrawable(Color.BLACK))
|
||||||
|
w.navigationBarColor = Color.BLACK
|
||||||
|
WindowCompat.setDecorFitsSystemWindows(w, false)
|
||||||
|
w.setLayout(
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||||
|
)
|
||||||
|
WindowInsetsControllerCompat(w, w.decorView).apply {
|
||||||
|
hide(WindowInsetsCompat.Type.statusBars())
|
||||||
|
systemBarsBehavior =
|
||||||
|
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
||||||
|
}
|
||||||
|
}
|
||||||
val url = urls[index]
|
val url = urls[index]
|
||||||
// 全屏查看按屏幕长边采样即可;原实现 targetSize=0 解原图,
|
// 全屏查看按屏幕长边采样即可;原实现 targetSize=0 解原图,
|
||||||
// 一张 4000×3000 的照片就是 48MB,连开两张低端机必崩。
|
// 一张 4000×3000 的照片就是 48MB,连开两张低端机必崩。
|
||||||
|
|||||||
@@ -231,23 +231,26 @@ object RssApi {
|
|||||||
suspend fun article(link: String): Article = withContext(Dispatchers.IO) {
|
suspend fun article(link: String): Article = withContext(Dispatchers.IO) {
|
||||||
if (!link.startsWith("http", true)) return@withContext Article("")
|
if (!link.startsWith("http", true)) return@withContext Article("")
|
||||||
val html = fetchPage(link)
|
val html = fetchPage(link)
|
||||||
|
/* 页面上的补充元信息(分类 / 阅读量 / 评论数):顶栏已经有来源、作者、日期,
|
||||||
|
这几项站点各不相同,只能按站点从页面里取,取不到就空着。 */
|
||||||
|
val extra = extractMetaExtra(html, link)
|
||||||
/* 纯前端渲染的站点(Vue / React 单页应用)HTML 里只有一个挂载点
|
/* 纯前端渲染的站点(Vue / React 单页应用)HTML 里只有一个挂载点
|
||||||
`<div id="app"></div>`,正文由 JS 在浏览器里拼出来,直接抓 HTML 永远是空壳。
|
`<div id="app"></div>`,正文由 JS 在浏览器里拼出来,直接抓 HTML 永远是空壳。
|
||||||
这类站点通常仍开放 /feed/,而 content:encoded 里就是全文——
|
这类站点通常仍开放 /feed/,而 content:encoded 里就是全文——
|
||||||
从自家 RSS 取是站点公开提供的渠道,不用碰它的接口签名。 */
|
从自家 RSS 取是站点公开提供的渠道,不用碰它的接口签名。 */
|
||||||
if (isSpaShell(html)) {
|
if (isSpaShell(html)) {
|
||||||
val full = feedFullText(link)
|
val full = feedFullText(link)
|
||||||
if (full.isNotBlank()) return@withContext Article(full)
|
if (full.isNotBlank()) return@withContext Article(full, "", extra)
|
||||||
}
|
}
|
||||||
val author = extractAuthor(html)
|
val author = extractAuthor(html)
|
||||||
val main = selectMainContent(html)
|
val main = selectMainContent(html, link)
|
||||||
val markdown = pruneNoiseLines(htmlToMarkdown(flexPhotoTokens(pruneNoiseBlocks(main))))
|
val markdown = pruneNoiseLines(htmlToMarkdown(flexPhotoTokens(pruneNoiseBlocks(main))))
|
||||||
// 正文短得不像文章(空壳站、或正文容器没被认出来):再试一次 feed 全文
|
// 正文短得不像文章(空壳站、或正文容器没被认出来):再试一次 feed 全文
|
||||||
if (markdown.length < 80) {
|
if (markdown.length < 80) {
|
||||||
val full = feedFullText(link)
|
val full = feedFullText(link)
|
||||||
if (full.isNotBlank()) return@withContext Article(full, author)
|
if (full.isNotBlank()) return@withContext Article(full, author, extra)
|
||||||
}
|
}
|
||||||
Article(markdown, author)
|
Article(markdown, author, extra)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchPage(url: String): String {
|
private fun fetchPage(url: String): String {
|
||||||
@@ -441,7 +444,12 @@ object RssApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 原文正文与作者 */
|
/** 原文正文与作者 */
|
||||||
data class Article(val markdown: String, val author: String = "")
|
data class Article(
|
||||||
|
val markdown: String,
|
||||||
|
val author: String = "",
|
||||||
|
/** 页面上额外的元信息(分类 · 阅读 · 评论),顶栏补在日期后面 */
|
||||||
|
val metaExtra: String = "",
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 作者提取:优先结构化数据(meta / JSON-LD),再退到 class="author" 之类。
|
* 作者提取:优先结构化数据(meta / JSON-LD),再退到 class="author" 之类。
|
||||||
@@ -483,7 +491,7 @@ object RssApi {
|
|||||||
* 或者在 header 里放一条摘要式 article;取「最长」能稳定命中真正的正文。
|
* 或者在 header 里放一条摘要式 article;取「最长」能稳定命中真正的正文。
|
||||||
* 选中正文容器后,页头、页脚、侧栏、目录、标签云、评论、版权在结构上就已经不在里面了。
|
* 选中正文容器后,页头、页脚、侧栏、目录、标签云、评论、版权在结构上就已经不在里面了。
|
||||||
*/
|
*/
|
||||||
private fun selectMainContent(html: String): String {
|
private fun selectMainContent(html: String, link: String): String {
|
||||||
val s = html
|
val s = html
|
||||||
.replace(Regex("<script[\\s\\S]*?</script>", RegexOption.IGNORE_CASE), "")
|
.replace(Regex("<script[\\s\\S]*?</script>", RegexOption.IGNORE_CASE), "")
|
||||||
.replace(Regex("<style[\\s\\S]*?</style>", RegexOption.IGNORE_CASE), "")
|
.replace(Regex("<style[\\s\\S]*?</style>", RegexOption.IGNORE_CASE), "")
|
||||||
@@ -499,10 +507,34 @@ object RssApi {
|
|||||||
.replace(Regex("<form[\\s\\S]*?</form>", RegexOption.IGNORE_CASE), "")
|
.replace(Regex("<form[\\s\\S]*?</form>", RegexOption.IGNORE_CASE), "")
|
||||||
.replace(Regex("<!--[\\s\\S]*?-->"), "")
|
.replace(Regex("<!--[\\s\\S]*?-->"), "")
|
||||||
|
|
||||||
|
val openRe = Regex("<(div|section|article|main)\\b([^>]*)>", RegexOption.IGNORE_CASE)
|
||||||
|
|
||||||
|
/* 0) 站点钉死的正文容器优先。
|
||||||
|
有些主题(bluehe.cn 的 sujian)不按 entry-content 那套命名,正文之外还裹着
|
||||||
|
封面、标题、阅读量、版权、上一篇下一篇;通用关键词一个都命中不了,最后只能
|
||||||
|
退回整块 <article>,页头页尾就全进了正文。这类站点直接按域名指定容器,
|
||||||
|
命中即返回——阈值放到 20 字,短文也不能因为「不够像文章」退回整页。 */
|
||||||
|
val siteTokens = siteContentTokens(link)
|
||||||
|
if (siteTokens.isNotEmpty()) {
|
||||||
|
var siteBest = ""
|
||||||
|
var siteLen = 0
|
||||||
|
for (m in openRe.findAll(s)) {
|
||||||
|
val cls = attrValue(m.groupValues[2], "class").lowercase()
|
||||||
|
if (cls.isEmpty() || siteTokens.none { cls.contains(it) }) continue
|
||||||
|
val end = findBlockEnd(s, m.range.first, m.groupValues[1]) ?: continue
|
||||||
|
val block = s.substring(m.range.first, end)
|
||||||
|
val len = stripTags(block).trim().length
|
||||||
|
if (len > siteLen) {
|
||||||
|
siteLen = len
|
||||||
|
siteBest = block
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (siteLen >= 20) return siteBest
|
||||||
|
}
|
||||||
|
|
||||||
// 1) 明确标注了正文容器的
|
// 1) 明确标注了正文容器的
|
||||||
var best = ""
|
var best = ""
|
||||||
var bestLen = 0
|
var bestLen = 0
|
||||||
val openRe = Regex("<(div|section|article|main)\\b([^>]*)>", RegexOption.IGNORE_CASE)
|
|
||||||
for (m in openRe.findAll(s)) {
|
for (m in openRe.findAll(s)) {
|
||||||
val cls = attrValue(m.groupValues[2], "class").lowercase()
|
val cls = attrValue(m.groupValues[2], "class").lowercase()
|
||||||
if (cls.isEmpty() || CONTENT_TOKENS.none { cls.contains(it) }) continue
|
if (cls.isEmpty() || CONTENT_TOKENS.none { cls.contains(it) }) continue
|
||||||
@@ -548,6 +580,54 @@ object RssApi {
|
|||||||
"post-text", "entry-text", "article-text", "the-content",
|
"post-text", "entry-text", "article-text", "the-content",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 站点专用正文容器(按域名后缀匹配)。
|
||||||
|
*
|
||||||
|
* 只收录「通用规则一定挑不中、且页头页尾噪音特别多」的站点;
|
||||||
|
* 值是 class 里的词元,命中即用该容器,不再往下走通用逻辑。
|
||||||
|
*/
|
||||||
|
private val SITE_CONTENT_TOKENS = mapOf(
|
||||||
|
// sujian 主题:<div class="post_left"><div class="song words">正文</div></div>
|
||||||
|
"bluehe.cn" to listOf("song"),
|
||||||
|
)
|
||||||
|
|
||||||
|
private fun siteContentTokens(link: String): List<String> {
|
||||||
|
val host = runCatching { java.net.URI(link).host.orEmpty().lowercase() }.getOrDefault("")
|
||||||
|
if (host.isEmpty()) return emptyList()
|
||||||
|
return SITE_CONTENT_TOKENS.entries
|
||||||
|
.firstOrNull { host == it.key || host.endsWith(".${it.key}") }
|
||||||
|
?.value.orEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 页面上「分类 · 阅读 · 评论」这类补充元信息。
|
||||||
|
*
|
||||||
|
* 顶栏已经有来源、作者、日期,这里只补站点自己写死的那几项;
|
||||||
|
* 取不到就返回空串,不要为了凑数去猜。
|
||||||
|
*/
|
||||||
|
private fun extractMetaExtra(html: String, link: String): String {
|
||||||
|
val host = runCatching { java.net.URI(link).host.orEmpty().lowercase() }.getOrDefault("")
|
||||||
|
if (host.isEmpty()) return ""
|
||||||
|
val block = when {
|
||||||
|
host == "bluehe.cn" || host.endsWith(".bluehe.cn") ->
|
||||||
|
Regex(
|
||||||
|
"<div[^>]+class=[\"'][^\"']*post_pic_info[^\"']*[\"'][^>]*>([\\s\\S]*?)</div\\s*>",
|
||||||
|
RegexOption.IGNORE_CASE,
|
||||||
|
).find(html)?.groupValues?.get(1)
|
||||||
|
else -> null
|
||||||
|
} ?: return ""
|
||||||
|
return unescape(stripTags(block))
|
||||||
|
.replace(Regex("\\s+"), " ")
|
||||||
|
.split('•', '·', '|')
|
||||||
|
.map { it.trim() }
|
||||||
|
.filter { it.isNotEmpty() && it.length <= 16 }
|
||||||
|
// 日期项顶栏已经有了,不重复显示
|
||||||
|
.filterNot { Regex("\\d{4}[-/.年]\\d{1,2}").containsMatchIn(it) }
|
||||||
|
.distinct()
|
||||||
|
.take(3)
|
||||||
|
.joinToString(" · ")
|
||||||
|
}
|
||||||
|
|
||||||
/** 取属性值:class / id 可能用单引号也可能用双引号 */
|
/** 取属性值:class / id 可能用单引号也可能用双引号 */
|
||||||
private fun attrValue(attrs: String, name: String): String =
|
private fun attrValue(attrs: String, name: String): String =
|
||||||
Regex("\\b$name\\s*=\\s*(['\"])(.*?)\\1", RegexOption.IGNORE_CASE)
|
Regex("\\b$name\\s*=\\s*(['\"])(.*?)\\1", RegexOption.IGNORE_CASE)
|
||||||
@@ -703,8 +783,16 @@ object RssApi {
|
|||||||
"qrcode", "footer", "header", "nav", "menu", "meta", "author", "prev", "next",
|
"qrcode", "footer", "header", "nav", "menu", "meta", "author", "prev", "next",
|
||||||
"rank", "hot", "like", "vote", "praise", "digg", "sns", "follow", "readmore",
|
"rank", "hot", "like", "vote", "praise", "digg", "sns", "follow", "readmore",
|
||||||
"postmeta", "entrymeta", "sticky",
|
"postmeta", "entrymeta", "sticky",
|
||||||
|
// 「继续阅读 / 上一篇下一篇」区块常见的命名
|
||||||
|
"nextnav", "prevnav", "prevnext", "nextcard", "nextpost", "prevpost",
|
||||||
|
"keepreading", "readnext", "postnavi",
|
||||||
|
// 页头大封面(标题、日期、阅读量通常也压在这块里)
|
||||||
|
"post-pic", "postpic",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/** 前缀命中即算噪音:pagination2 / post-navigation-3 这类带序号、带后缀的命名 */
|
||||||
|
private val NOISE_PREFIX = listOf("pagination", "postnav", "pa-nextnav", "post-navigation")
|
||||||
|
|
||||||
/** 首词命中就删,不看后缀(评论内容再长也是评论) */
|
/** 首词命中就删,不看后缀(评论内容再长也是评论) */
|
||||||
private val STRONG_NOISE = setOf(
|
private val STRONG_NOISE = setOf(
|
||||||
"comment", "comments", "respond", "disqus", "related", "share", "social",
|
"comment", "comments", "respond", "disqus", "related", "share", "social",
|
||||||
@@ -724,6 +812,8 @@ object RssApi {
|
|||||||
return raw.split(Regex("\\s+")).any { token ->
|
return raw.split(Regex("\\s+")).any { token ->
|
||||||
if (token.isBlank()) return@any false
|
if (token.isBlank()) return@any false
|
||||||
if (token in NOISE_TOKENS) return@any true
|
if (token in NOISE_TOKENS) return@any true
|
||||||
|
// 带序号 / 后缀的命名(pagination2、pa-nextnav--pair)按前缀算
|
||||||
|
if (NOISE_PREFIX.any { token.startsWith(it) }) return@any true
|
||||||
val parts = token.split('-').filter { it.isNotEmpty() }
|
val parts = token.split('-').filter { it.isNotEmpty() }
|
||||||
if (parts.size < 2) return@any false
|
if (parts.size < 2) return@any false
|
||||||
if (parts.first() in STRONG_NOISE) return@any true
|
if (parts.first() in STRONG_NOISE) return@any true
|
||||||
@@ -739,8 +829,9 @@ object RssApi {
|
|||||||
private fun pruneNoiseLines(markdown: String): String {
|
private fun pruneNoiseLines(markdown: String): String {
|
||||||
val keys = listOf(
|
val keys = listOf(
|
||||||
"上一篇", "下一篇", "相关文章", "相关阅读", "相关推荐", "猜你喜欢", "延伸阅读",
|
"上一篇", "下一篇", "相关文章", "相关阅读", "相关推荐", "猜你喜欢", "延伸阅读",
|
||||||
|
"继续阅读", "KEEP READING", "推荐阅读", "热门文章",
|
||||||
"本文链接", "原文链接", "文章地址", "本文地址", "永久链接", "转载请", "版权声明",
|
"本文链接", "原文链接", "文章地址", "本文地址", "永久链接", "转载请", "版权声明",
|
||||||
"版权所有", "All rights reserved", "Copyright ©", "© 20",
|
"版权所有", "本文原创", "许可:", "许可:", "CC BY", "All rights reserved", "Copyright ©", "© 20",
|
||||||
"发表评论", "写下你的评论", "暂无评论", "条评论", "评论已关闭", "取消回复",
|
"发表评论", "写下你的评论", "暂无评论", "条评论", "评论已关闭", "取消回复",
|
||||||
"邮箱地址不会被公开", "必填项已用", "在此浏览器中保存", "提交评论", "发表回复",
|
"邮箱地址不会被公开", "必填项已用", "在此浏览器中保存", "提交评论", "发表回复",
|
||||||
"标签:", "标签:", "Tags:", "Keywords:", "关键词:", "关键词:", "分类:", "分类:",
|
"标签:", "标签:", "Tags:", "Keywords:", "关键词:", "关键词:", "分类:", "分类:",
|
||||||
@@ -765,6 +856,11 @@ object RssApi {
|
|||||||
)
|
)
|
||||||
// 头像 / 二维码小图
|
// 头像 / 二维码小图
|
||||||
val avatarImage = Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE)
|
val avatarImage = Regex("^!\\[[^\\]]*\\]\\([^)]*(?:avatar|gravatar|qrcode)[^)]*\\)$", RegexOption.IGNORE_CASE)
|
||||||
|
// 日期打头、用分隔点串起分类/阅读/评论的 meta 行
|
||||||
|
// (2026-11-26 11:49 • 叙述 • 337 阅 • 16 评):顶栏已经显示日期,这行没有正文
|
||||||
|
val datedMeta = Regex(
|
||||||
|
"^\\d{4}[-/.年]\\d{1,2}[-/.月]\\d{1,2}日?(?:\\s+\\d{1,2}:\\d{2})?\\s*[·•|]\\s*\\S.{0,60}$",
|
||||||
|
)
|
||||||
|
|
||||||
// 代码块先占位:逐行处理会 trim 掉行首缩进,代码缩进一旦丢了就没法看
|
// 代码块先占位:逐行处理会 trim 掉行首缩进,代码缩进一旦丢了就没法看
|
||||||
val codeList = mutableListOf<String>()
|
val codeList = mutableListOf<String>()
|
||||||
@@ -786,6 +882,7 @@ object RssApi {
|
|||||||
}
|
}
|
||||||
if (anchorLine.matches(t) || urlOnly.matches(t)) continue
|
if (anchorLine.matches(t) || urlOnly.matches(t)) continue
|
||||||
if (tocTitle.matches(t) || counterLine.matches(t) || avatarImage.matches(t)) continue
|
if (tocTitle.matches(t) || counterLine.matches(t) || avatarImage.matches(t)) continue
|
||||||
|
if (datedMeta.matches(t)) continue
|
||||||
// 只过滤短行:正文里整段讨论「评论」「版权」的段落不受影响
|
// 只过滤短行:正文里整段讨论「评论」「版权」的段落不受影响
|
||||||
if (t.length < 100 && keyRe.containsMatchIn(t)) continue
|
if (t.length < 100 && keyRe.containsMatchIn(t)) continue
|
||||||
if (pendingBlank) {
|
if (pendingBlank) {
|
||||||
@@ -829,9 +926,15 @@ object RssApi {
|
|||||||
s = s.replace(Regex("<form[\\s\\S]*?</form>", RegexOption.IGNORE_CASE), "")
|
s = s.replace(Regex("<form[\\s\\S]*?</form>", RegexOption.IGNORE_CASE), "")
|
||||||
|
|
||||||
// 代码块先抽出来占位:里面的标签、实体与换行不能被后续替换破坏
|
// 代码块先抽出来占位:里面的标签、实体与换行不能被后续替换破坏
|
||||||
val codeBlocks = mutableListOf<String>()
|
val codeBlocks = mutableListOf<Pair<String, 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'))
|
val inner = m.groupValues[1]
|
||||||
|
// <pre><code class="language-kotlin">:这句标注决定阅读页代码卡顶部显示什么语言
|
||||||
|
val lang = Regex(
|
||||||
|
"class=[\"'][^\"']*(?:language|lang)-([\\w+#.-]{1,16})",
|
||||||
|
RegexOption.IGNORE_CASE,
|
||||||
|
).find(inner)?.groupValues?.get(1)?.lowercase().orEmpty()
|
||||||
|
codeBlocks.add(lang to unescape(stripTags(inner)).trim('\n'))
|
||||||
"\u0001\u0000P${codeBlocks.size - 1}\u0000\u0001"
|
"\u0001\u0000P${codeBlocks.size - 1}\u0000\u0001"
|
||||||
}
|
}
|
||||||
// 引用块整块处理:内部段落分隔要带上 ">",否则引用看起来只剩第一句
|
// 引用块整块处理:内部段落分隔要带上 ">",否则引用看起来只剩第一句
|
||||||
@@ -853,7 +956,14 @@ object RssApi {
|
|||||||
}
|
}
|
||||||
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()})"
|
when {
|
||||||
|
text.isNotEmpty() -> "[$text](${m.groupValues[1].trim()})"
|
||||||
|
// 只包着一张图的链接(灯箱 / 点图看原图):链接自己没有文字,
|
||||||
|
// 早先直接整块丢掉,正文里的配图就跟着消失了——保留内层交给图片规则
|
||||||
|
Regex("<img\\b", RegexOption.IGNORE_CASE).containsMatchIn(m.groupValues[2]) ->
|
||||||
|
m.groupValues[2]
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s = s.replace(Regex("<img\\b[^>]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m ->
|
s = s.replace(Regex("<img\\b[^>]*src=[\"']([^\"']+)[\"'][^>]*?>", RegexOption.IGNORE_CASE)) { m ->
|
||||||
val alt = unescape(attrValue(m.value, "alt")).replace(Regex("\\s+"), " ").trim()
|
val alt = unescape(attrValue(m.value, "alt")).replace(Regex("\\s+"), " ").trim()
|
||||||
@@ -925,8 +1035,8 @@ object RssApi {
|
|||||||
s = s.replace(Regex("[\\r\\n\\t\\u000B\\f]+"), " ")
|
s = s.replace(Regex("[\\r\\n\\t\\u000B\\f]+"), " ")
|
||||||
s = s.replace(Regex(" {2,}"), " ")
|
s = s.replace(Regex(" {2,}"), " ")
|
||||||
s = s.replace("\u0001", "\n\n").replace("\u0003", "\\\n").replace("\u0002", "\n")
|
s = s.replace("\u0001", "\n\n").replace("\u0003", "\\\n").replace("\u0002", "\n")
|
||||||
codeBlocks.forEachIndexed { i, code ->
|
codeBlocks.forEachIndexed { i, (lang, code) ->
|
||||||
s = s.replace("\u0000P$i\u0000", "```\n$code\n```")
|
s = s.replace("\u0000P$i\u0000", "```$lang\n$code\n```")
|
||||||
}
|
}
|
||||||
tables.forEachIndexed { i, table ->
|
tables.forEachIndexed { i, table ->
|
||||||
s = s.replace("\u0000T$i\u0000", table)
|
s = s.replace("\u0000T$i\u0000", table)
|
||||||
|
|||||||
10
src/main/res/drawable/bg_code_card.xml
Normal file
10
src/main/res/drawable/bg_code_card.xml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- 代码卡底:圆角 + 1dp 描边,深浅两套颜色都由 @color 的 night 变体提供 -->
|
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:shape="rectangle">
|
||||||
|
<solid android:color="@color/code_bg" />
|
||||||
|
<stroke
|
||||||
|
android:width="1dp"
|
||||||
|
android:color="@color/code_stroke" />
|
||||||
|
<corners android:radius="10dp" />
|
||||||
|
</shape>
|
||||||
@@ -27,6 +27,13 @@
|
|||||||
|
|
||||||
<color name="ripple_on_brand">#33FFFFFF</color>
|
<color name="ripple_on_brand">#33FFFFFF</color>
|
||||||
<color name="ripple_surface">#2EFFFFFF</color>
|
<color name="ripple_surface">#2EFFFFFF</color>
|
||||||
|
|
||||||
|
<!-- 代码卡:夜间压到比页面还深一档,代码区自然「凹」下去 -->
|
||||||
|
<color name="code_bg">#16181C</color>
|
||||||
|
<color name="code_stroke">#262A30</color>
|
||||||
|
<color name="code_text">#E6E1D8</color>
|
||||||
|
<color name="code_lineno">#5D646F</color>
|
||||||
|
<color name="code_lang">#7A8089</color>
|
||||||
<color name="mark_stone">#EDE7DB</color>
|
<color name="mark_stone">#EDE7DB</color>
|
||||||
<color name="mark_stone_deep">#D3CABA</color>
|
<color name="mark_stone_deep">#D3CABA</color>
|
||||||
<color name="mark_roof">#D4814F</color>
|
<color name="mark_roof">#D4814F</color>
|
||||||
|
|||||||
@@ -34,6 +34,13 @@
|
|||||||
<color name="ripple_on_brand">#33FFFFFF</color>
|
<color name="ripple_on_brand">#33FFFFFF</color>
|
||||||
<color name="ripple_surface">#14000000</color>
|
<color name="ripple_surface">#14000000</color>
|
||||||
|
|
||||||
|
<!-- 代码卡:浅色底用暖灰,跟纸面底色分开一档又不刺眼 -->
|
||||||
|
<color name="code_bg">#F7F5F0</color>
|
||||||
|
<color name="code_stroke">#E4DED2</color>
|
||||||
|
<color name="code_text">#24292F</color>
|
||||||
|
<color name="code_lineno">#A8A196</color>
|
||||||
|
<color name="code_lang">#8C8578</color>
|
||||||
|
|
||||||
<!-- 图标
|
<!-- 图标
|
||||||
底色回到上一版那种蓝:圆形渐变打底,白石厝压在上面,
|
底色回到上一版那种蓝:圆形渐变打底,白石厝压在上面,
|
||||||
石缝用浅蓝,门洞取中间那层蓝,缩到 48dp 也还认得出是石头房子。 -->
|
石缝用浅蓝,门洞取中间那层蓝,缩到 48dp 也还认得出是石头房子。 -->
|
||||||
|
|||||||
@@ -127,7 +127,7 @@
|
|||||||
<string name="mail_all_accounts">全部邮箱</string>
|
<string name="mail_all_accounts">全部邮箱</string>
|
||||||
<string name="feed_loading">加载中…</string>
|
<string name="feed_loading">加载中…</string>
|
||||||
<string name="feed_no_more">没有更多了</string>
|
<string name="feed_no_more">没有更多了</string>
|
||||||
<string name="open_original">查看原文</string>
|
<string name="open_original">原文</string>
|
||||||
<string name="read_mark">标为已读</string>
|
<string name="read_mark">标为已读</string>
|
||||||
<string name="unread_mark">标为未读</string>
|
<string name="unread_mark">标为未读</string>
|
||||||
<string name="read_done">已标为已读</string>
|
<string name="read_done">已标为已读</string>
|
||||||
@@ -199,7 +199,7 @@
|
|||||||
|
|
||||||
<!-- 阅读页:更多菜单 / 作者主页 / 标签 / AI 摘要 -->
|
<!-- 阅读页:更多菜单 / 作者主页 / 标签 / AI 摘要 -->
|
||||||
<string name="more">更多</string>
|
<string name="more">更多</string>
|
||||||
<string name="author_page">作者主页</string>
|
<string name="author_page">主页</string>
|
||||||
<string name="tag_page">标签浏览</string>
|
<string name="tag_page">标签浏览</string>
|
||||||
<string name="open_site">打开网站</string>
|
<string name="open_site">打开网站</string>
|
||||||
<string name="article_loading">正在读取…</string>
|
<string name="article_loading">正在读取…</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user