系列讲义生成功能 改为 先生成字典后生成讲义

This commit is contained in:
weiye.wang 2024-05-02 00:40:05 +08:00
parent 22d8c59579
commit 61b32c7200
2 changed files with 81 additions and 8 deletions

View File

@ -1860,7 +1860,22 @@ def generateLaTeXBodyContentfromMariaDB(cursor,id,misc): #根据id,读取的json
output = f"\\item ({id}) 题号有误!!!" output = f"\\item ({id}) 题号有误!!!"
return output return output
def generateLaTeXobjsfromDict(objid,objdict):
objid = str(objid).upper().strip()
if objid == "KNONE":
return ""
else:
output = f"\\item {objid} {objdict[objid]['objcontent']}\n\n"
return output
def generateLaTeXbnsfromDict(bnid,bndict):
bnid = str(bnid).upper().strip()
if not bnid in bndict:
bn_content = f"{bnid} 基础知识目标编号有误"
else:
bn_content = f"{bndict[bnid]['bncontent']}"
output = f"\\item {bn_content}\n\n"
return output
def generateLaTeXobjsfromMariaDB(cursor,objid): def generateLaTeXobjsfromMariaDB(cursor,objid):
objid = str(objid).upper().strip() objid = str(objid).upper().strip()
@ -2395,6 +2410,48 @@ 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 GenerateSectionBodyStringfromDict(problems,sectiontitles,prodict,objdict,bndict,misc,consecutivenumbering):
bodystring = ""
count = 0
for i in range(len(problems)):
if problems[i][0] in "0123456789":
idlist = generate_number_set(problems[i])
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 += generateLaTeXBodyContentFromDict(id,prodict,objdict,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 += generateLaTeXobjsfromDict(objid,objdict)
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 bnid in idlist:
sectionstring += generateLaTeXbnsfromDict(bnid,bndict)
sectionstring += "\\end{enumerate}\n\n"
bodystring += sectionstring
return bodystring #返回主题内容字符串
def GenerateSingleLessonTeXfromDict(id,notesdict,pro_dict,obj_dict,bn_dict,misc,consecutivenumbering=False):
structure = notesdict["structures"][id[0].upper()]["structure"]
note_contents = notesdict["notes"][id]
sections_list = []
problems_list = []
for key in structure:
if not len(note_contents[key]) == 0:
sections_list.append(structure[key]["name"])
problems_list.append(",".join(note_contents[key]))
rawoutput = GenerateSectionBodyStringfromDict(problems=problems_list,sectiontitles=sections_list,prodict=pro_dict,objdict=obj_dict,bndict=bn_dict,misc=misc,consecutivenumbering= consecutivenumbering)
if len(sections_list) == 1:
rawoutput = re.sub(r"\\section\{[^\n]*","",rawoutput)
return rawoutput
def GenerateSingleLessonNotefromMariaDB(cursor,id,notesdict,templatepath,outputfilepath,misc,consecutivenumbering = False): #20240415版讲义生成 def GenerateSingleLessonNotefromMariaDB(cursor,id,notesdict,templatepath,outputfilepath,misc,consecutivenumbering = False): #20240415版讲义生成
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

@ -87,14 +87,12 @@ class MyWindow_jysc(QWidget,Ui_Form):
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name) mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
mycursor = mydb.cursor() mycursor = mydb.cursor()
# raw_pro_dict = load_dict("../题库0.3/Problems.json") raw_pro_dict,obj_dict,bn_dict,unit_obj_dict = generateDictsfromMariaDB(mycursor)
# obj_dict = load_dict("../题库0.3/LessonObj.json")
# basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json")
if self.radioButton_teacher.isChecked() and self.checkBox_usages.isChecked() and not self.lineEdit_grades.text().strip() == "": if self.radioButton_teacher.isChecked() and self.checkBox_usages.isChecked() and not self.lineEdit_grades.text().strip() == "":
grades = self.lineEdit_grades.text().strip().split(",") grades = self.lineEdit_grades.text().strip().split(",")
else: else:
grades = [] grades = []
# pro_dict = select_grade_from_pro_dict(raw_pro_dict,grades) pro_dict = select_grade_from_pro_dict(raw_pro_dict,grades)
# dictionaries = {} #合并字典 # dictionaries = {} #合并字典
# for t in (obj_dict,basicknowledge_dict,pro_dict): # for t in (obj_dict,basicknowledge_dict,pro_dict):
# dictionaries.update(t) # dictionaries.update(t)
@ -128,11 +126,29 @@ class MyWindow_jysc(QWidget,Ui_Form):
filename = notes_dict["notes"][lessonid]["id"]+notes_dict["notes"][lessonid]["filename"]+".tex" filename = notes_dict["notes"][lessonid]["id"]+notes_dict["notes"][lessonid]["filename"]+".tex"
papertype = lessonid[0] papertype = lessonid[0]
consecutivenumbering = notes_dict["structures"][papertype]["consecutivenumbering"] consecutivenumbering = notes_dict["structures"][papertype]["consecutivenumbering"]
texdata = GenerateSingleLessonNotefromMariaDB(cursor = mycursor, id = lessonid,notesdict=notes_dict,templatepath="./模板文件/讲义模板.txt",outputfilepath = os.path.join(self.outputpath,filename),misc=configjson,consecutivenumbering = consecutivenumbering) texdata = GenerateSingleLessonTeXfromDict(id=lessonid, notesdict=notes_dict, pro_dict=pro_dict, obj_dict=obj_dict,bn_dict=bn_dict,misc = configjson, consecutivenumbering= consecutivenumbering)
papernames.append(notes_dict["notes"][lessonid]["id"]+" \\ "+notes_dict["notes"][lessonid]["name"]) papernames.append(notes_dict["notes"][lessonid]["id"]+" \\ "+notes_dict["notes"][lessonid]["name"])
print(f"已生成 {papernames[-1]} 文件") print(f"已生成 {papernames[-1]} 文件")
multitexdata.append(re.findall(r"\\begin{center}\n{\\bf\\large \\papername}\n\\end{center}([\s\S]*\\end\{enumerate\})",texdata)[0]) multitexdata.append(texdata)
# print(lessonid) latex_raw = ReadTextFile("./模板文件/讲义模板.txt")
latex_raw = latex_raw.replace(r"学号\blank{50} \ 姓名\blank{80}","上海市控江中学") #替换掉模板中的姓名学号
if sys.platform == "darwin": #非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,(papernames[-1],texdata)) #替换标题和bodystring
outputfilepath = os.path.join(self.outputpath,filename)
SaveTextFile(latex_data,outputfilepath) #保存.tex文件
if configjson["编译单个文件"] == True:
outputdir,filename = os.path.split(outputfilepath)
print(f"{filename}编译中...")
if XeLaTeXCompile(outputdir,filename):
print("编译成功")
else:
print("编译失败")
# print(lessonid)
# print(configjson) # print(configjson)
# print("\n".join(patterns)) # print("\n".join(patterns))
mydb.close() mydb.close()
@ -166,7 +182,7 @@ class MyWindow_jysc(QWidget,Ui_Form):
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication([]) app = QApplication([])
windows = MyWindow() windows = MyWindow_jysc("tikutest")
windows.show() windows.show()
app.exec() app.exec()