新增 系列讲义生成(替代原有的讲义生成)功能, 及辅助的一些json和database_tools

This commit is contained in:
weiye.wang 2024-02-05 22:05:29 +08:00
parent 7c3dfbc72e
commit aa9578f1b0
3 changed files with 161 additions and 0 deletions

View File

@ -887,6 +887,53 @@ def XeLaTeXCompile(filedir,filename): #在filedir目录中用XeLaTeX编译filena
flagsuc = False flagsuc = False
return flagsuc # 若第二次编译成功则返回True, 否则返回False return flagsuc # 若第二次编译成功则返回True, 否则返回False
def GenerateSectionBodyString2024(problems,sectiontitles,pro_dict,obj_dict,bk_dict,misc,consecutivenumbering= True): #生成学生版的.tex文件的主体内容
bodystring = ""
count = 0
for i in range(len(problems)):
if problems[i][0] in "0123456789":
idlist = generate_number_set(problems[i],pro_dict)
sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\\setcounter{{enumi}}{{{count if consecutivenumbering else 0}}}\n\n"
for id in idlist:
count += 1
sectionstring += generateLaTeXBodyContent(id,pro_dict,obj_dict,misc)
sectionstring += "\\end{enumerate}\n\n"
bodystring += sectionstring
if problems[i][0] == "K":
idlist = problems[i].split(",")
sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\n"
for objid in idlist:
sectionstring += f"\\item {objid} \\ {(obj_dict[objid]['content'] if objid in obj_dict else '目标编号有误')}\n\n"
sectionstring += "\\end{enumerate}\n\n"
bodystring += sectionstring
if problems[i][0] == "B":
idlist = problems[i].split(",")
sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\n"
for bkid in idlist:
if bkid in bk_dict:
bk = bk_dict[bkid]["content"]
else:
bk = bkid + " \\ 基础知识编号有误"
sectionstring += f"\\item {(bk)}\n\n"
sectionstring += "\\end{enumerate}\n\n"
bodystring += sectionstring
return bodystring #返回主题内容字符串
# if not objlist == []:
# output += "\\section{课时目标}\n\n"
# output += "\\begin{enumerate}\n\n"
# for objid in objlist:
# output += "\\item %s \\ %s \n\n"%(objid,objdict[objid]["content"])
# output += "\\end{enumerate}\n\n" #生成学习目标
# if not bnlist == []:
# output += "\\section{双基梳理}\n\n"
# basic_body = ""
# for bnid in bnlist:
# basic_body += "\\item %s\n\n"%basicknowledgedict[bnid]["content"]
# output += "\\begin{enumerate}\n\n %s\n\n\\end{enumerate}\n\n"%basic_body #生成基础知识梳理
def GenerateStudentBodyString(problems,sectiontitles,pro_dict,consecutivenumbering= True,answered= True,spaceflag=True): #生成学生版的.tex文件的主体内容 def GenerateStudentBodyString(problems,sectiontitles,pro_dict,consecutivenumbering= True,answered= True,spaceflag=True): #生成学生版的.tex文件的主体内容
bodystring = "" bodystring = ""
if len(problems) == len(sectiontitles): if len(problems) == len(sectiontitles):
@ -1499,6 +1546,47 @@ def select_grade_from_pro_dict(prodict,grades):
adict[id]["usages"] = new_usages.copy() adict[id]["usages"] = new_usages.copy()
return adict return adict
def GenerateSingleLessonNote2024(id,notesdict,pro_dict,obj_dict,bk_dict,templatepath,outputfilepath,misc,consecutivenumbering = False): #20231215版讲义生成
notetitle = id + r" \ " + notesdict["notes"][id]["name"]
structure = notesdict["structures"][id[0].upper()]["structure"]
note_contents = notesdict["notes"][id]
output = ""
sections_list = []
problems_list = []
for key in structure:
if not len(note_contents[key]) == 0:
sections_list.append(key)
problems_list.append(",".join(note_contents[key]))
rawoutput = GenerateSectionBodyString2024(problems=problems_list,sectiontitles=sections_list,pro_dict=pro_dict,obj_dict=obj_dict,bk_dict=bk_dict,misc=misc,consecutivenumbering= consecutivenumbering)
# rawoutput = GenerateStudentBodyString(problems=problems_list,sectiontitles=sections_list,pro_dict=metadict,consecutivenumbering= consecutivenumbering, answered= answered, spaceflag = True)
paragraphs = [p for p in rawoutput.split("\\section") if not p.strip() == ""]
for item in paragraphs:
sectionkey, content = re.findall(r"\{([\S]*)\}\n([\S\s]*)$",item)[0]
if not len(paragraphs) == 1:
output += "\\section{" + structure[sectionkey]["name"] + "}\n\n"
# if not structure[sectionkey]["spaceflag"] or answered:
# content = re.sub(r"\\vspace[\*]?\{[\S]*\}","\n",content)
output += content + "\n\n"
latex_raw = ReadTextFile(templatepath)
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文件
outputdir,filename = os.path.split(outputfilepath)
print(f"{filename}编译中...")
if XeLaTeXCompile(outputdir,filename):
print("编译成功")
return latex_data # 返回0
else:
print("编译失败")
return latex_data # 返回有错误的latex源代码
def GenerateSingleLessonNote(id,notesdict,metadict,templatepath,outputfilepath,consecutivenumbering = False, answered = False): #20231215版讲义生成 def GenerateSingleLessonNote(id,notesdict,metadict,templatepath,outputfilepath,consecutivenumbering = False, answered = False): #20231215版讲义生成
notetitle = id + r" \ " + notesdict["notes"][id]["name"] notetitle = id + r" \ " + notesdict["notes"][id]["name"]
structure = notesdict["structures"][id[0].upper()]["structure"] structure = notesdict["structures"][id[0].upper()]["structure"]

View File

@ -32,5 +32,22 @@
"备注": true, "备注": true,
"届别": [] "届别": []
} }
},
"系列讲义生成.py": {
"讲义标题格式": "[EWGVI]202601\\d{2}",
"输出路径": "临时文件",
"教师版": true,
"字段显示设置": {
"题后空间": true,
"课时目标": true,
"题目标签": true,
"答案": true,
"解答与提示": true,
"使用记录": [3,-1],
"使用记录说明": "[a,b]表示显示最好的a个和最差b个, 有-2表示不显示, 无-2但有-1表示全部显示",
"来源": true,
"备注": true,
"届别": []
}
} }
} }

View File

@ -0,0 +1,56 @@
# lessonpattern = r"U20240501" # 正则表达式, 数据库中讲义的编号([A-Z][0-9]{4}[(01)|(02)][[0-9]{2}), 字母表示类型, 四位数字表示届别, 2位数字表示学期及其他, 2位数字表示序号
# outputdir = "d:/temp/26届材料" # 输出文件夹, 不建议修改
# answered = True # 设置是否编译答案
from database_tools import *
configjson = load_dict("文本文件/config.json")["系列讲义生成.py"]
lessonpattern = configjson["讲义标题格式"] # 正则表达式, 数据库中讲义的编号([A-Z][0-9]{4}[(01)|(02)][[0-9]{2}), 字母表示类型, 四位数字表示届别, 2位数字表示学期及其他, 2位数字表示序号
outputdir = configjson["输出路径"] # 输出文件夹, 不建议修改
# answered = configjson["提供答案"] # 设置是否编译答案
jsonpath = "../备课组" #有json文件的根目录, 文件名需为"校本材料.json"
jsondicts = []
for loc,dirs,files in os.walk(jsonpath):
if "校本材料.json" in files:
jsondicts.append(load_dict(os.path.join(loc,"校本材料.json")))
pro_dict = load_dict("../题库0.3/problems.json")
obj_dict = load_dict("../题库0.3/LessonObj.json")
basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json")
dictionaries = {} #合并字典
for t in (obj_dict,basicknowledge_dict,pro_dict):
dictionaries.update(t)
papernames = []
multitexdata = []
for notes_dict in jsondicts:
for lessonid in notes_dict["notes"]:
if re.findall(lessonpattern,lessonid) != []:
filename = notes_dict["notes"][lessonid]["id"]+notes_dict["notes"][lessonid]["filename"]+".tex"
papertype = lessonid[0]
consecutivenumbering = notes_dict["structures"][papertype]["consecutivenumbering"]
texdata = GenerateSingleLessonNote2024(id = lessonid,notesdict=notes_dict,pro_dict=pro_dict,obj_dict=obj_dict,bk_dict=basicknowledge_dict,templatepath=".\模板文件\讲义模板.txt",outputfilepath = os.path.join(outputdir,filename),misc=configjson,consecutivenumbering = consecutivenumbering)
# texdata = GenerateSingleLessonNote(id = lessonid,notesdict = notes_dict, metadict = dictionaries, templatepath = ".\模板文件\讲义模板.txt", outputfilepath = os.path.join(outputdir,filename),consecutivenumbering=consecutivenumbering,answered=answered)
papernames.append(notes_dict["notes"][lessonid]["id"]+" \\ "+notes_dict["notes"][lessonid]["name"])
multitexdata.append(re.findall(r"\\begin{center}\n{\\bf\\large \\papername}\n\\end{center}([\s\S]*\\end\{enumerate\})",texdata)[0])
merged = ""
for i in range(len(papernames)):
merged += "\n\n\\chapter{"+papernames[i]+"}\n\n\n"
merged += multitexdata[i]
mergedtext = StringSubstitute(r"<<待替换[\d]+>>",ReadTextFile("./模板文件/合集模板.txt"),[merged])
SaveTextFile(mergedtext,os.path.join(outputdir,f"合集{GetDate()}.tex"))
tocompile = input("需要编译合集吗?(Y/[N]):")
if tocompile[0].upper() == "Y":
XeLaTeXCompile(outputdir,f"合集{GetDate()}.tex")