feat: 题面转干净markdown(标题/代码块/LaTeX);读题自动同步本地题库支持提交

这个提交包含在:
langxm
2026-10-14 21:18:13 +08:00
父节点 17ebf03ede
当前提交 2d6f9a71b1
共修改 3 个文件,包含 52 行新增和 8 行删除
+20 -6
查看文件
@@ -32,25 +32,39 @@ def _get_db():
return _db
def _strip_html(s: str) -> str:
def _html_to_md(s: str) -> str:
"""粗转 HTML → Markdown,保留标题/代码块/样例结构。"""
# katex: 取 <annotation> 里的 TeX
s = re.sub(r'<annotation[^>]*>(.*?)</annotation>', r'$\1$', s)
s = re.sub(r'<span class="katex[^"]*">.*?</span>', '', s) # 去掉渲染后的 katex-html
# 代码块
s = re.sub(r'<pre><code[^>]*>(.*?)</code></pre>', lambda m: '\n```\n' + m.group(1).strip() + '\n```\n', s, flags=re.S)
s = re.sub(r'<code[^>]*>(.*?)</code>', r'`\1`', s, flags=re.S)
# 标题
for i in range(6, 0, -1):
s = re.sub(rf'<h{i}[^>]*>(.*?)</h{i}>', r'\n' + '#' * i + r' \1\n', s, flags=re.S)
# 段落/列表
s = re.sub(r'<br\s*/?>', '\n', s)
s = re.sub(r'</(p|div|h[1-6]|li)>', '\n', s)
s = re.sub(r'</p>', '\n\n', s)
s = re.sub(r'<li[^>]*>', '- ', s)
s = re.sub(r'</li>', '\n', s)
# 去剩余标签
s = re.sub(r'<[^>]+>', '', s)
return re.sub(r'\n{3,}', '\n\n', s).strip()
# 清理空行
s = re.sub(r'\n{3,}', '\n\n', s)
return s.strip()
def _parse_content(raw) -> str:
"""content 可能是 dict 或 JSON 字符串,取 zh 字段再剥 HTML。"""
"""content 可能是 dict 或 JSON 字符串,取 zh 字段再转 markdown。"""
if isinstance(raw, str):
try:
raw = json.loads(raw)
except Exception:
return _strip_html(raw)
return _html_to_md(raw)
if isinstance(raw, dict):
raw = raw.get("zh") or raw.get("en") or ""
return _strip_html(str(raw))
return _html_to_md(str(raw))
def get_problem(pid_input: str) -> dict | None: