feat(tools): fix_gitea_math 增加 front matter 检查
新增 fix_frontmatter():把信息块内常见符号($+$ $=$ $\to$ 等)转纯文本, 并去掉残留 $ 与反引号。此前这类问题只能靠 verify_gitea_math.py 抓网页发现。
This commit is contained in:
@@ -246,6 +246,34 @@ def fix_text(text):
|
||||
return text, sorted(uniq, key=lambda x: x[0])
|
||||
|
||||
|
||||
FM_MAP = [('$+$', '+'), ('$-$', '-'), ('$=$', '='), (r'$\to$', '→'),
|
||||
(r'$\times$', '×'), ('$<$', '<'), ('$>$', '>')]
|
||||
|
||||
|
||||
def fix_frontmatter(text):
|
||||
"""Gitea 把 front matter 渲染成表格时**不解析 markdown**,
|
||||
因此信息块里的 ``$...$`` 与反引号会原样露出(网页抓取才看得见)。
|
||||
这里把常见符号转成纯字符,并去掉残留的 ``$`` 与反引号。"""
|
||||
if not text.startswith('---'):
|
||||
return text, 0
|
||||
end = text.find('\n---', 3)
|
||||
if end < 0:
|
||||
return text, 0
|
||||
head, tail = text[:end], text[end:]
|
||||
hits = 0
|
||||
lines = []
|
||||
for ln in head.split('\n'):
|
||||
if '$' not in ln and '`' not in ln:
|
||||
lines.append(ln)
|
||||
continue
|
||||
hits += 1
|
||||
for a, b in FM_MAP:
|
||||
ln = ln.replace(a, b)
|
||||
ln = ln.replace('$', '').replace('`', '')
|
||||
lines.append(ln)
|
||||
return '\n'.join(lines) + tail, hits
|
||||
|
||||
|
||||
def main():
|
||||
apply = '--apply' in sys.argv
|
||||
scope = None
|
||||
@@ -258,6 +286,10 @@ def main():
|
||||
for path in sorted(iter_md_files(scope)):
|
||||
raw = open(path, encoding='utf-8').read()
|
||||
new, changes = fix_text(raw)
|
||||
new2, fm_hits = fix_frontmatter(new)
|
||||
if fm_hits:
|
||||
changes = list(changes) + [(-1, '信息块') for _ in range(fm_hits)]
|
||||
new = new2
|
||||
if changes:
|
||||
total_files += 1
|
||||
total_hits += len(changes)
|
||||
|
||||
Reference in New Issue
Block a user