116 lines
6.5 KiB
Python
116 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import re
|
|
|
|
def fix_latex_escapes_in_file(file_path):
|
|
"""批量修复JSON文件中的LaTeX反斜杠转义问题"""
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# 如果已经可以解析,就不需要修复
|
|
try:
|
|
json.loads(content)
|
|
print(f"✓ {file_path} - JSON格式已经正确")
|
|
return True
|
|
except json.JSONDecodeError:
|
|
pass
|
|
|
|
# 定义所有需要转义的LaTeX命令
|
|
latex_commands = [
|
|
'frac', 'sqrt', 'sin', 'cos', 'tan', 'cot', 'sec', 'csc',
|
|
'arcsin', 'arccos', 'arctan', 'log', 'ln', 'lg',
|
|
'alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta',
|
|
'iota', 'kappa', 'lambda', 'mu', 'nu', 'xi', 'omicron', 'pi',
|
|
'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega',
|
|
'Gamma', 'Delta', 'Theta', 'Lambda', 'Xi', 'Pi', 'Sigma', 'Phi',
|
|
'Psi', 'Omega', 'mathbf', 'mathcal', 'mathbb', 'mathfrak', 'mathsf',
|
|
'mathrm', 'mathit', 'mathrm', 'mathsf', 'mathtt', 'vec', 'overrightarrow',
|
|
'overleftarrow', 'overline', 'underline', 'widehat', 'widetilde',
|
|
'bar', 'dot', 'ddot', 'acute', 'grave', 'breve', 'check', 'hat',
|
|
'tilde', 'left', 'right', 'begin', 'end', 'quad', 'qquad', 'hspace',
|
|
'vspace', 'noindent', 'centering', 'item', 'cdot', 'times', 'div',
|
|
'pm', 'mp', 'le', 'ge', 'ne', 'neq', 'approx', 'equiv', 'sim',
|
|
'simeq', 'cong', 'propto', 'parallel', 'perp', 'in', 'notin', 'subset',
|
|
'supset', 'subseteq', 'supseteq', 'cup', 'cap', 'setminus', 'emptyset',
|
|
'varnothing', 'infty', 'partial', 'nabla', 'exists', 'forall', 'neg',
|
|
'wedge', 'vee', 'land', 'lor', 'implies', 'iff', 'Rightarrow', 'Leftarrow',
|
|
'Leftrightarrow', 'mapsto', 'int', 'oint', 'sum', 'prod', 'coprod',
|
|
'bigcup', 'bigcap', 'bigvee', 'bigwedge', 'lim', 'limsup', 'liminf',
|
|
'max', 'min', 'sup', 'inf', 'arg', 'det', 'dim', 'ker', 'hom',
|
|
'Pr', 'exp', 'ln', 'log', 'lg', 'sinh', 'cosh', 'tanh', 'coth',
|
|
'arcsin', 'arccos', 'arctan', 'arcsinh', 'arccosh', 'arctanh',
|
|
'overbrace', 'underbrace', 'overline', 'underline', 'overrightarrow',
|
|
'overleftarrow', 'dots', 'cdots', 'vdots', 'ddots', 'ldots',
|
|
'text', 'mbox', 'mathrm', 'mathsf', 'mathtt', 'mathit', 'mathbf',
|
|
'mathcal', 'mathfrak', 'mathbb', 'mathscr', 'mathrsfs', 'mathsf',
|
|
'boldsymbol', 'boldmath', 'unboldmath', 'displaystyle', 'textstyle',
|
|
'scriptstyle', 'scriptscriptstyle', 'limits', 'nolimits', 'Big', 'big',
|
|
'Bigg', 'bigg', 'left', 'right', 'Bigl', 'bigl', 'Biggl', 'biggl',
|
|
'Bigr', 'bigr', 'Biggr', 'biggr', 'langle', 'rangle', 'lfloor',
|
|
'rfloor', 'lceil', 'rceil', 'vert', 'Vert', 'mid', 'parallel',
|
|
'backslash', 'slash', 'setminus', 'smallsetminus', 'complement',
|
|
'prime', 'circ', 'star', 'ast', 'dagger', 'ddagger', 'aleph',
|
|
'beth', 'gimel', 'daleth', 'ell', 'wp', 'Re', 'Im', 'angle',
|
|
'measuredangle', 'sphericalangle', 'top', 'bot', 'flat', 'natural',
|
|
'sharp', 'clubsuit', 'diamondsuit', 'heartsuit', 'spadesuit',
|
|
'checkmark', 'maltese', 'degree', 'celsius', 'percent', 'promille',
|
|
'pertenthousand', 'minus', 'plus', 'pm', 'mp', 'times', 'div',
|
|
'cdot', 'centerdot', 'ast', 'star', 'circ', 'bullet', 'cap', 'cup',
|
|
'uplus', 'sqcap', 'sqcup', 'vee', 'wedge', 'land', 'lor', 'setminus',
|
|
'smallsetminus', 'triangleleft', 'triangleright', 'diamond', 'bigtriangleup',
|
|
'bigtriangledown', 'oplus', 'ominus', 'otimes', 'oslash', 'odot',
|
|
'circledcirc', 'circleddash', 'box', 'boxbox', 'triangle', 'bigtriangle',
|
|
'triangleleft', 'triangleright', 'lhd', 'rhd', 'unlhd', 'unrhd',
|
|
'oplus', 'ominus', 'otimes', 'oslash', 'odot', 'circledcirc', 'circleddash',
|
|
'intercal', 'curlywedge', 'curlyvee', 'veebar', 'doublebarwedge', 'pitchfork',
|
|
'lessdot', 'gtrdot', 'cdot', 'ldots', 'cdots', 'vdots', 'ddots',
|
|
'bar', 'barwedge', 'veebar', 'doublebarwedge', 'bar', 'bar',
|
|
'overline', 'underline', 'widehat', 'widetilde', 'overleftarrow',
|
|
'overrightarrow', 'overbrace', 'underbrace', 'overline', 'underline',
|
|
'acute', 'grave', 'check', 'breve', 'acutemath', 'gravemath',
|
|
'checkmath', 'breveemath', 'hat', 'tilde', 'bar', 'dot', 'ddot',
|
|
'mathring', 'mathring', 'mathring', 'mathring', 'mathring',
|
|
'textstyle', 'displaystyle', 'scriptstyle', 'scriptscriptstyle',
|
|
'limits', 'nolimits', 'int', 'oint', 'iint', 'iiint', 'iiiint',
|
|
'idotsint', 'oint', 'oint', 'oint', 'oint', 'oint',
|
|
'sum', 'prod', 'coprod', 'bigcap', 'bigcup', 'bigsqcup', 'bigvee',
|
|
'bigwedge', 'bigoplus', 'bigotimes', 'bigodot', 'bigsqcap', 'bigsqcup',
|
|
'biguplus', 'bigvee', 'bigwedge', 'sum', 'prod', 'coprod'
|
|
]
|
|
|
|
# 修复所有LaTeX命令
|
|
fixed_content = content
|
|
for cmd in latex_commands:
|
|
# 修复模式:\command -> \\command
|
|
pattern = r'(?<!\\)\\' + cmd # 不以反斜杠开头的反斜杠后跟命令
|
|
replacement = r'\\\\' + cmd # 替换为双反斜杠
|
|
fixed_content = re.sub(pattern, replacement, fixed_content)
|
|
|
|
# 尝试解析修复后的内容
|
|
json.loads(fixed_content)
|
|
print(f"✓ {file_path} - 修复成功")
|
|
|
|
# 写回文件
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.write(fixed_content)
|
|
return True
|
|
|
|
except json.JSONDecodeError as e:
|
|
print(f"✗ {file_path} - 修复失败: 第{e.lineno}行第{e.colno}列: {str(e)}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"✗ {file_path} - 处理出错: {str(e)}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
files_to_fix = [
|
|
'knowledge-选择性必修第一章-空间向量与立体几何.json',
|
|
'knowledge-选择性必修第二章-直线和圆的方程.json',
|
|
'knowledge-选择性必修第五章-一元函数的导数及其应用.json',
|
|
'knowledge-选择性必修第八章-成对数据的统计分析.json',
|
|
'knowledge-选择性必修第四章-数列.json'
|
|
]
|
|
|
|
for file_path in files_to_fix:
|
|
fix_latex_escapes_in_file(file_path) |