import os,re,json
"""这里编辑题号(列表)后将在vscode中打开窗口, 编辑后保存关闭"""
problems = "14758"
editor = "王伟叶"

def generate_number_set(string,dict):
    string = re.sub(r"[\n\s]","",string)
    string_list = string.split(",")
    numbers_list = []
    for s in string_list:
        if not ":" in s:
            numbers_list.append(s.zfill(6))
        else:
            start,end = s.split(":")
            for ind in range(int(start),int(end)+1):
                numbers_list.append(str(ind).zfill(6))
    return numbers_list

with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
    database = f.read()
pro_dict = json.loads(database)

idlist = generate_number_set(problems,pro_dict)

output = "编辑者: " + editor + "\n\n\\begin{enumerate}\n\n"

for id in idlist:
    output += "\\item (%s) "%str(id).zfill(6) + pro_dict[id]["content"] + "\n\n"

output += "\\end{enumerate}"

with open("模板文件/题目编辑.txt","r",encoding="u8") as f:
    texdata = f.read()

newtexdata = texdata.replace("待替换",output)

with open("临时文件/problems_edit.tex","w",encoding="u8") as f:
    f.write(newtexdata)
 
os.system(r"code -g 临时文件/problems_edit.tex")
