From 64a2ca0321e422e91898eeb87dfa16d03152f5f0 Mon Sep 17 00:00:00 2001 From: NFC666 <2954834129@qq.com> Date: Mon, 26 Oct 2026 13:05:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(tools):=20fix=5Fgitea=5Fmath=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20front=20matter=20=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 fix_frontmatter():把信息块内常见符号($+$ $=$ $\to$ 等)转纯文本, 并去掉残留 $ 与反引号。此前这类问题只能靠 verify_gitea_math.py 抓网页发现。 --- .workbuddy/tools/md2pdf/fix_gitea_math.py | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/.workbuddy/tools/md2pdf/fix_gitea_math.py b/.workbuddy/tools/md2pdf/fix_gitea_math.py index 36544e2..eb8fd4e 100644 --- a/.workbuddy/tools/md2pdf/fix_gitea_math.py +++ b/.workbuddy/tools/md2pdf/fix_gitea_math.py @@ -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)