database_tools中新增根据新的讲义模板json生成讲义的功能GenerateSingleLessonNote

This commit is contained in:
wangweiye7840 2023-12-15 12:36:38 +08:00
parent d464c10936
commit 382c27be06
1 changed files with 41 additions and 2 deletions

View File

@ -727,8 +727,9 @@ def StudentsGetAfterContent(id,prodict,answered,spaceflag): #生成学生版讲
if answered:
string += "答案: \\textcolor{red}{%s}\n\n"%(prodict[id]["ans"] if prodict[id]["ans"] != "" else "暂无答案")
if spaceflag:
if prodict[id]["space"] != "":
string += "\\vspace*{%s}\n\n"%prodict[id]["space"]
if "space" in prodict[id]:
if prodict[id]["space"] != "":
string += "\\vspace*{%s}\n\n"%prodict[id]["space"]
return string #生成学生讲义后的答案及空格
def XeLaTeXCompile(filedir,filename): #在filedir目录中用XeLaTeX编译filename文件两次能编译出正确的引用和页码
@ -1256,7 +1257,45 @@ def select_grade_from_pro_dict(prodict,grades):
adict[id]["usages"] = new_usages.copy()
return adict
def GenerateSingleLessonNote(id,notesdict,metadict,templatepath,outputfilepath,consecutivenumbering = False, answered = False): #20231215版讲义生成
notetitle = id+ r" \ " + notesdict["notes"][id]["name"]
structure = notesdict["structures"][id[0].upper()]
note_contents = notesdict["notes"][id]
print(structure)
output = ""
sections_list = []
problems_list = []
for key in structure:
print(structure[key])
if not len(note_contents[key]) == 0:
sections_list.append(key)
problems_list.append(",".join(note_contents[key]))
rawoutput = GenerateStudentBodyString(problems=problems_list,secidontitles=sections_list,pro_dict=metadict,consecutivenumbering= False, answered= False, 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]
output += "\\section{" + structure[sectionkey]["name"] + "}\n\n"
if not structure[sectionkey]["spaceflag"]:
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)
if XeLaTeXCompile(outputdir,filename):
print("编译成功")
return 0 # 返回0
else:
print("编译失败")
return latex_data # 返回有错误的latex源代码
if __name__ == "__main__":