filepath = "d:/temp/g1ans1.txt" #已输入答案的文件名
metadatafilepath = "文本文件/metadata.txt" #metadata.txt所在位置

from database_tools import *


data = ReadTextFile("d:/temp/g1ans1.txt")
for regex in (r"\\section\{[\s\S]*?\}",r"\\begin\{enumerate\}",r"\\end\{enumerate\}",r"\\setcounter\{enumi\}\{[\s\S]*?\}",r"\\newpage"):
    data = re.sub(regex,"",data) #文件预处理
plist = GenerateProblemListFromString(data)
outputstr = "ans\n\n"
for p in plist:
    id = re.findall(r"\((\d{6})\)",p[0])[0]
    ans = re.findall(r"\\textcolor{red}{\\textcolor{red}\{([\s\S]*?)\}\}$",p[0].strip())[0]
    if not ans == "暂无答案":
        outputstr += "%s\n%s\n\n"%(id,ans)

#测试是否均合法
template = ReadTextFile("模板文件/题目编辑.txt")
latexdata = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",template,[outputstr])
SaveTextFile(latexdata,"临时文件/answer.tex")
if XeLaTeXCompile("临时文件","answer.tex") == True:
    print("转换成功, 保存至 %s"%metadatafilepath)
    SaveTextFile(outputstr,metadatafilepath)
else:
    print("转换失败, 请检查原文件 %s"%filepath)