lessonsidstr = "K01,K04,K0201:K0226" #用字符串表示课时代码范围
notetitle = "高一上学期课时及目标" #作为文件名和文件标题
outputdir = "临时文件"

from database_tools import *

outputfilepath = os.path.join(outputdir,notetitle+".tex")
lessonsdictpath = "../题库0.3/LessonsCut.json" #课时分割字典所在路径
objdictpath = "../题库0.3/lessonobj.json" #课时目标字典所在路径



lessons_dict = load_dict(lessonsdictpath)
obj_dict = load_dict(objdictpath)

lessons_id_list = generate_lessonid_list(lessonsidstr,lessons_dict)
output = ""
for lessonid in lessons_id_list: # 生成用来替换的课时名和目标字符串
    output += "\\section{%s \ %s}\n\n\\begin{enumerate}\n\n"%(lessonid,lessons_dict[lessonid]["name"])
    for objid in obj_dict:
        if objid.startswith(lessonid):
            output += "\\item %s \\ %s \n\n"%(objid,obj_dict[objid]["content"])
    output += "\\end{enumerate}\n\n"

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)

latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,(notetitle,output)) #替换标题和bodystring
SaveTextFile(latex_data,outputfilepath) #保存.tex文件

if XeLaTeXCompile(outputdir,notetitle+".tex"):
    print("编译成功")
else:
    print("编译失败")
