defconvert(path): print(f"converting {path} ...") withopen(path, "r", encoding="utf8") as file:
art = file.readlines() title = path.split("\\")[-1].split(".")[0] flag = 2 art1 = [ "%! TeX program = xelatex\n\\documentclass[UTF8]{cteXart}\n\\usepackage{CJKfntef}\n\\usepackage{amsmath}\n" "\\usepackage{amsfonts}\\usepackage{amssymb}\n\\usepackage{enumerate}\n\\usepackage{multirow}" "\\usepackage{geometry}\n\\geometry{top=0cm, bottom=0.5cm, left=2.5cm, right=2.5cm}\n\\usepackage{tabularx}", f"\\title{{{title}}}", "\\begin{document}\\maketitle\\tableofcontents" ] for line in art: if line.startswith("---"): flag -= 1 continue if flag: continue if line.startswith("###"): line1 = "".join(line.split()[1:]) art1.append(f"\\subsubsection{{{line1}}}") elif line.startswith("##"): line1 = "".join(line.split()[1:]) art1.append(f"\\subsection{{{line1}}}") elif line.startswith("#"): line1 = "".join(line.split()[1:]) art1.append(f"\\section{{{line1}}}") else: art1.append(line.strip()) art1.append("\\end{document}")
to = r"D:\VsCode\TeXProject\convert" withopen(to+"\\" + title + ".tex", "w", encoding="utf8") as file: file.write("\n".join(art1))
if __name__ == '__main__': dir1 = r"D:\VsCode\blogProj\MathNote\source\_posts" for file in os.listdir(dir1): if file.endswith(".md"): convert(path=dir1 + "\\" + file)