修复 Gitea 网页端行内公式与加粗不渲染

根因(读 Gitea 解析器 modules/markup/markdown/math/inline_parser.go 源码确认):
行内公式收尾 $ 的后一个字节必须属于 {空格,换行,行尾,'.','!','?',',',';',':',')','$'},
开头 $ 的左邻不能是 ASCII 字母数字或上述标点。中文与全角标点均为多字节字符
(单字节值 >= 0x80),全部落空,于是整对 $...$ 被当作普通文本原样显示。
本地 PDF 走 KaTeX,无此限制,所以只有网页异常。

处理:
- 新增 .workbuddy/tools/md2pdf/fix_gitea_math.py(检测 + 修复,可 --check/--apply)
- 在收尾 $ 与紧跟的中文字符之间补一个半角空格:23 个文件 1889 处
- 顺带修复加粗失效(收尾 ** 被标点前导、被汉字后继,不满足 CommonMark
  right-flanking 规则):10 处,星号不再外露
- 新增 rebuild_prob_pdfs.sh,重生成概率论第3/5/6讲全部 12 个 PDF

范围:概率论在学练习 + 每日小练 + 知识点。试卷/模拟卷/多元函数积分学
仍有 1484 处待修(同规则,可随时跑 --scope 之外的 --apply)。
This commit is contained in:
2026-10-16 17:53:49 +08:00
parent ab10f27f07
commit a750fb2ca6
32 changed files with 1171 additions and 909 deletions
+224
View File
@@ -0,0 +1,224 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
fix_gitea_math.py —— 修复 Gitea 网页端 Markdown 公式/加粗不渲染的问题
【问题一:行内公式不渲染】
Gitea 的解析器 modules/markup/markdown/math/inline_parser.go 规则写死:
收尾 $ 的后一个字节必须 ∈ { 空格, 换行, 行尾, '.', '!', '?', ',', ';', ':', ')', '$' }
开头 $ 的左邻不能是 ASCII 字母/数字或 . ! ? , ; :
中文字与全角标点(,。、;())都是多字节字符,单字节值 ≥ 0x80,全部落空,
于是整对 $...$ 被当作普通文本原样显示。
→ 修法:在收尾 $ 与紧跟的非空白字符之间补一个半角空格。
【问题二:加粗失效】
CommonMark 要求收尾 ** 必须是 right-flanking:
不被空白前导,且(不被标点前导 或 被标点前导但被空白/标点后继)。
形如 **…"一致性"**的 的写法(收尾 ** 前是标点、后是汉字的)不满足条件,
加粗失效,星号会被原样显示(网页与 PDF 皆然)。
→ 修法:在收尾 ** 后补一个半角空格。
用法:
python fix_gitea_math.py --check # 全文统计
python fix_gitea_math.py --check --scope=prob # 只看概率论在学练习/小练/知识点
python fix_gitea_math.py --apply --scope=prob # 就地修复
"""
import os
import re
import subprocess
import sys
import unicodedata
ROOT = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..'))
SCOPES = {
'prob': [
'基于考研的学习/数学一/在学练习/',
'基于考研的学习/数学一/每日小练/',
'基于考研的学习/数学一/知识点/',
],
}
def iter_md_files(scope=None):
"""只处理 git 已跟踪的 .md(排除 参考仓库),与网页上能看到的内容一致。"""
out = subprocess.run(['git', 'ls-files', '-z', '--', '*.md'],
cwd=ROOT, capture_output=True, check=True).stdout.decode('utf-8')
prefixes = SCOPES.get(scope) if scope else None
for rel in out.split('\0'):
if not rel or rel.startswith('参考仓库/') or rel.startswith('.workbuddy/'):
continue
if prefixes and not any(rel.startswith(p) for p in prefixes):
continue
yield os.path.join(ROOT, rel.replace('/', os.sep))
# ------------------------------------------------------------------ 遮罩
def mask_code(text):
"""遮罩围栏代码块与行内代码,返回等长字符串(被遮位置为 \\x00)。"""
n = len(text)
mask = [''] * n
def paint(a, b):
for i in range(max(0, a), min(b, n)):
mask[i] = '\x00'
for m in re.finditer(r'(`+)([^\n]*?)\1', text):
paint(m.start(), m.end())
# 围栏代码块:行级状态机(开/闭成对,绝不会误遮到文件末尾)
in_fence = False
pos = 0
for line in text.split('\n'):
ln = len(line)
if re.match(r'^[ \t]*(```|~~~)', line):
in_fence = not in_fence
paint(pos, pos + ln)
elif in_fence:
paint(pos, pos + ln)
pos += ln + 1
return ''.join(c if c else text[i] for i, c in enumerate(mask))
def mask_code_and_math(text):
"""在 mask_code 基础上再遮罩公式(用于加粗判定,避免把公式里的 ** 当粗体)。"""
base = mask_code(text)
mask = list(base)
for a, b in find_math_any(text):
if base[a] == '\x00':
continue
for i in range(a, min(b + 1, len(text))):
mask[i] = '\x00'
return ''.join(mask)
# ------------------------------------------------------------------ 公式
def find_math_any(text):
"""返回 (open_idx, close_idx_inclusive) 列表,绝不跨行。"""
spans = []
i, n = 0, len(text)
while i < n:
c = text[i]
if c == '\\' and i + 1 < n and text[i + 1] == '$':
i += 2
continue
if c == '$' and i + 1 < n and text[i + 1] == '$':
end = text.find('$$', i + 2)
if end != -1 and '\n' not in text[i:end]:
spans.append((i, end + 1))
i = end + 2
continue
if c == '$':
j, close = i + 1, -1
while j < n and text[j] != '\n':
if text[j] == '\\':
j += 2
continue
if text[j] == '$':
close = j
break
j += 1
if close != -1 and close > i + 1:
spans.append((i, close))
i = close + 1
continue
i += 1
return spans
# Gitea 认可的收尾字符(单字节)
END_OK = set('.!?,;:') | {')', ' ', '\n', '$'}
# Gitea 不认可的开头左邻(ASCII 字母数字与下列标点)
BAD_OPEN_PREV = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!?,;:')
def is_punct(ch):
return unicodedata.category(ch)[0] in ('P', 'S')
def fix_math(text, mask):
inserts = []
for start, end in find_math_any(text):
if mask[start] == '\x00':
continue
if start > 0 and mask[start - 1] != '\x00' and text[start - 1] in BAD_OPEN_PREV:
inserts.append((start, '公式前'))
pos = end + 1
if pos < len(text) and mask[pos] != '\x00' and text[pos] not in END_OK:
inserts.append((pos, '公式后'))
return inserts
# ------------------------------------------------------------------ 加粗
def fix_bold(text, mask):
"""收尾 ** 被标点前导、被非空白非标点后继 → 右侧补空格,使其成为 right-flanking。"""
stars = [m.start() for m in re.finditer(r'\*\*', text) if mask[m.start()] != '\x00']
inserts = []
is_open = True
for idx in stars:
if is_open:
is_open = False
continue
is_open = True
prev = text[idx - 1] if idx > 0 else ''
nxt = text[idx + 2] if idx + 2 < len(text) else ''
if not prev or not nxt:
continue
if mask[idx - 1] == '\x00' or mask[idx + 2] == '\x00':
continue
if is_punct(prev) and not nxt.isspace() and not is_punct(nxt):
inserts.append((idx + 2, '加粗后'))
return inserts
# ------------------------------------------------------------------ 主流程
def fix_text(text):
mc = mask_code(text)
mcm = mask_code_and_math(text)
inserts = fix_bold(text, mcm) + fix_math(text, mc)
if not inserts:
return text, []
seen = set()
uniq = []
for pos, kind in inserts:
if (pos, kind) in seen:
continue
seen.add((pos, kind))
uniq.append((pos, kind))
for pos, _ in sorted(uniq, reverse=True):
text = text[:pos] + ' ' + text[pos:]
return text, sorted(uniq, key=lambda x: x[0])
def main():
apply = '--apply' in sys.argv
scope = None
for a in sys.argv:
if a.startswith('--scope='):
scope = a.split('=', 1)[1]
total_files = total_hits = 0
report = []
for path in sorted(iter_md_files(scope)):
raw = open(path, encoding='utf-8').read()
new, changes = fix_text(raw)
if changes:
total_files += 1
total_hits += len(changes)
rel = os.path.relpath(path, ROOT)
report.append((rel, len(changes)))
if apply:
open(path, 'w', encoding='utf-8', newline='').write(new)
print(f'== {"已修复" if apply else "待修复(仅统计)"} ==')
for rel, cnt in report:
print(f' {cnt:4d} {rel}')
print(f'--- 共 {total_files} 个文件,{total_hits} 处需要修 ---')
if __name__ == '__main__':
main()
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# 重新生成概率论在学练习(第3/5/6讲)的全部 PDF
# 用法:bash .workbuddy/tools/md2pdf/rebuild_prob_pdfs.sh
set -u
REPO="D:/考研/第二代数学笔记"
NODE="C:/Users/Wang Ling/.workbuddy/binaries/node/versions/22.22.2-6/node.exe"
TOOL="$REPO/.workbuddy/tools/md2pdf/md2pdf.js"
BASE="$REPO/基于考研的学习/数学一/在学练习"
build () { # $1=目录 $2=源md $3=输出pdf $4=标题 $5=可选 mode
cd "$BASE/$1" || return 1
if [ "${5:-}" = "answer" ]; then
"$NODE" "$TOOL" --in "$2" --out "$3" --title "$4" --mode answer
else
"$NODE" "$TOOL" --in "$2" --out "$3" --title "$4"
fi
}
D3="概率论-第3讲-多维随机变量及其分布"
D5="概率论-第5讲-大数定律与中心极限定理"
D6="概率论-第6讲-数理统计"
build "$D3" "01-考点手册.md" "PDF/概率论第3讲-考点手册.pdf" "概率论 第3讲 · 考点手册"
build "$D3" "02-试题.md" "PDF/概率论第3讲-试题-答题版.pdf" "概率论 第3讲 · 试题(答题版)" answer
build "$D3" "03-解析.md" "PDF/概率论第3讲-解析.pdf" "概率论 第3讲 · 解析"
build "$D3" "04-答疑笔记.md" "PDF/概率论第3讲-答疑笔记.pdf" "概率论 第3讲 · 答疑笔记"
build "$D3" "05-试题-中高难度.md" "PDF/概率论第3讲-试题-中高难度-答题版.pdf" "概率论 第3讲 · 试题(中高难度卷)" answer
build "$D3" "06-解析-中高难度.md" "PDF/概率论第3讲-解析-中高难度.pdf" "概率论 第3讲 · 中高难度卷 · 解析"
build "$D5" "01-考点手册.md" "PDF/概率论第5讲-考点手册.pdf" "概率论 第5讲 · 大数定律与中心极限定理 · 考点手册"
build "$D5" "02-试题.md" "PDF/概率论第5讲-试题-答题版.pdf" "概率论 第5讲 · 大数定律与中心极限定理(答题版)" answer
build "$D5" "03-解析.md" "PDF/概率论第5讲-解析.pdf" "概率论 第5讲 · 大数定律与中心极限定理 · 解析"
build "$D6" "01-考点手册.md" "PDF/概率论第6讲-考点手册.pdf" "概率论 第6讲 · 数理统计 · 考点手册"
build "$D6" "02-试题.md" "PDF/概率论第6讲-试题-答题版.pdf" "概率论 第6讲 · 数理统计(答题版)" answer
build "$D6" "03-解析.md" "PDF/概率论第6讲-解析.pdf" "概率论 第6讲 · 数理统计 · 解析"
echo "全部完成"