starting_id = "018067" # 初始题号
length = 50 # 题目链长度
outputfilename = "测试题目链"
outputfilepath = "临时文件"

from database_tools import *

starting_id = str(starting_id).zfill(6)
if not outputfilename.endswith(".tex"):
    outputfilename += ".tex"


pro_dict = load_dict("../题库0.3/problems.json")

id_string = generate_problem_series(starting_id,length,pro_dict)

latex_raw = ReadTextFile("模板文件/讲义模板.txt")
latex_raw = latex_raw.replace(r"学号\blank{50} \ 姓名\blank{80}","上海市控江中学")

if sys.platform != "win32": #非win系统用默认字体
    latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw)
    latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw)


bodystring = GenerateStudentBodyString([id_string],["关联题目链"],pro_dict,answered = True,spaceflag = False) #生成.tex中的内容主体字符串, 用于替换模板中的相应部分

latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,(outputfilename[:-4],bodystring)) #替换标题和bodystring
SaveTextFile(latex_data,os.path.join(outputfilepath,outputfilename)) #保存.tex文件

if XeLaTeXCompile(outputfilepath,outputfilename):
    print("编译成功")
else:
    print("编译失败")