problems = ["004794,004854,007691,007698,007736,009448,009453,009458,009460,010036,010039,010048,010050,010053,010055,010059:010060,010063,010065,010072,010076,010080,010088,011745,011780,011826,019281:019283,019806,019809,019831:019833,019836,020004:020008,020011:020012,020015,020019:020022,020028:020030,020033:020035,020038:020040,020044,020046:020048,020050:020052,020055,020060,020062:020063,020065:020066,020068,020070,020072:020074,020076:020080,020082:020083,020088,020090:020092,020094,020097:020111,020115:020116,020119:020120,020124,020127,020133,020137,020139:020142,020145,020150:020151,020153,020155:020156,020158:020160,020181:020182,020223,022183,022185:022188,022191:022194,022201,022211:022217,022221:022222,022225,022228,022230:022236,022647:022674,031404:031405"] #题号列表, 每个字符串表示一个分块的题目
notetitle = "2026届高一九月题目" #讲义标题, 也是文件标题
sectiontitles = ["课前","课后","测试"] #小节标题列表, 如果与题号列表长度不符则作为不设小节处理
grades = ["2024","2023","2026"] #届别, 留空表示使用记录都计入

colored = False #是否使用背景色块

consecutivenumbering = True #不同小节是否连续编号, True表示连续编号, False表示每小节从1开始编号
topandbottomusagestuple = (3,3) #表示保留得分率最高的使用记录与最低的使用记录的个数, 有负数表示不排列

showobjs = True #是否显示目标, True为显示, False为不显示
showtags = True #是否显示标签, True为显示, False为不显示
showans = True #是否显示答案, True为显示, False为不显示
showsolution = True #是否显示解答与提示, True为显示, False为不显示
showusages = True #是否显示使用记录, True为显示, False为不显示
showorigin = True #是否显示来源, True为显示, False为不显示
showremark = True #是否显示备注, True为显示, False为不显示
sectionname = "section" #按何种名称分组, 可选section, subsection, chapter等


from database_tools import *
import time

prodictpath = "../题库0.3/problems.json"
objdictpath = "../题库0.3/lessonobj.json"
raw_pro_dict = load_dict(prodictpath)
pro_dict = select_grade_from_pro_dict(raw_pro_dict,grades)
obj_dict = load_dict(objdictpath)

outputdir = "临时文件" #输出文件的目录
outputfilepath = os.path.join(outputdir,notetitle+".tex")
print("输出文件目录: %s\n输出文件名: %s"%(os.path.join(os.getcwd(),outputdir),notetitle+".tex"))

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)

starttime = time.time()

bodystring = GenerateTeacherBodyString(problems,sectiontitles,pro_dict,obj_dict,sectionname=sectionname, consecutivenumbering=consecutivenumbering, topandbottomusagestuple = topandbottomusagestuple, showobjs = showobjs, showtags = showtags, showans = showans, showsolution = showsolution, showusages = showusages, showorigin = showorigin, showremark = showremark, colored = colored) #生成.tex中的内容主体字符串, 用于替换模板中的相应部分

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

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

endtime = time.time()

print("总处理时间: %.3f秒"%(endtime-starttime))



