From 382c27be06c163ce2ef5ef389ed6a3be97112808 Mon Sep 17 00:00:00 2001 From: wangweiye7840 Date: Fri, 15 Dec 2023 12:36:38 +0800 Subject: [PATCH] =?UTF-8?q?database=5Ftools=E4=B8=AD=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=96=B0=E7=9A=84=E8=AE=B2=E4=B9=89=E6=A8=A1?= =?UTF-8?q?=E6=9D=BFjson=E7=94=9F=E6=88=90=E8=AE=B2=E4=B9=89=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BDGenerateSingleLessonNote?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具v2/database_tools.py | 43 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/工具v2/database_tools.py b/工具v2/database_tools.py index 6526ce82..c4bbebc9 100644 --- a/工具v2/database_tools.py +++ b/工具v2/database_tools.py @@ -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__":