-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (143 loc) · 4.27 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import subprocess
import shutil
import sys
import os
# configuration
title = "Code Templates"
author = "Seaside Guys"
# 各级文件夹对应的目录等级
level_map = {1: "section",
2: "subsection",
3: "subsubsection",
4: "paragraph",
5: "subparagraph"}
source_dir = "./sources"
stdout0 = sys.stdout # 备份stdout
def open_file_and_redirect_stdout():
global stdout0
stdout0 = sys.stdout
if not os.path.isdir("./.temp"):
os.mkdir("./.temp")
sys.stdout = open("./.temp/code.tex", "w", encoding='utf-8')
def close_file_and_recover_stdout():
global stdout0
sys.stdout.close()
sys.stdout = stdout0
def add_preset():
print(r"% 声明文章类型")
print(r"\documentclass[UTF8]{article}")
print()
print(r"% 声明使用包")
print(r"\usepackage{ctex}") # 中文支持
print(r"\usepackage{listings}") # 代码环境支持
print(r"\usepackage{xcolor}") # 修改颜色使用
print(r"\usepackage{geometry}") # 修改文档板式
print()
print(r"% 设置上下左右边距")
print(r"\geometry{a4paper, left=3cm, right=3cm, top=4cm, bottom=4cm}")
print()
print(r"% 声明颜色")
print(r"\definecolor{dkgreen}{rgb}{0,0.6,0}")
print(r"\definecolor{gray}{rgb}{0.5,0.5,0.5}")
print(r"\definecolor{mauve}{rgb}{0.58,0,0.82}")
print()
print(r"% 设置目录显示章节深度")
print(r"\setcounter{tocdepth}{4}")
print(r"% 设置章节编号显示深度")
print(r"\setcounter{secnumdepth}{3}")
print(r"% 设置代码高亮颜色", end="")
print(r"""
\lstset{
frame=leftline,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
framerule=1pt,
rulecolor=\color{gray!35},
backgroundcolor=\color{white},
basicstyle={\small\ttfamily},
numbers=left,
numberstyle=\tiny\color{gray}\tt,
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=4,
}
""")
print(r"% 论文信息")
print(r"\title{" + title + "}")
print(r"\author{" + author + "}")
print()
def add_beginning():
print(r"\pagestyle{empty}")
print(r"% 生成首页")
print(r"\maketitle")
print(r"\thispagestyle{empty}")
print(r"\newpage")
print(r"% 生成目录")
print(r"\tableofcontents")
print(r"\newpage")
print("")
print(r"%正文开始")
print(r"\pagestyle{plain}")
print(r"\setcounter{page}{1}")
def code2latex(path):
file_ext = os.path.splitext(os.path.basename(path))[1]
if file_ext == ".cpp":
lang = "c++"
elif file_ext == ".c":
lang = "c"
elif file_ext == ".py":
lang = "python"
elif file_ext == ".m":
lang = "matlab"
elif file_ext == ".html":
lang = "html"
elif file_ext in [".java", ".jsp"]:
lang = "java"
else:
raise Exception("no fucking language.")
print(r"\begin{lstlisting}[language=" + lang + "]")
with open(path, "r", encoding="utf-8") as file:
tot = file.readlines()
for i in tot:
print(i, end="")
print(r"\end{lstlisting}")
def add_main_body(root=source_dir, level=1):
dirs = []
files = []
for path in os.listdir(root):
path = os.path.join(root, path)
if os.path.isdir(path):
dirs.append(path)
elif os.path.isfile(path):
files.append(path)
dirs = sorted(dirs)
files = sorted(files)
for path in dirs:
dir_name = os.path.basename(path)
print("\\" + level_map[level] + "{" + dir_name + "}")
add_main_body(path, level + 1)
for path in files:
file_name = os.path.splitext(os.path.basename(path))[0]
print("\\" + level_map[level] + "{" + file_name + "}")
code2latex(path)
def make_pdf():
os.chdir(".temp")
cmd = r"xelatex.exe -synctex=1 -interaction=nonstopmode code.tex"
subprocess.run(cmd)
subprocess.run(cmd)
os.chdir("..")
shutil.copy(".temp/code.pdf", "code.pdf")
if __name__ == '__main__':
open_file_and_redirect_stdout()
add_preset()
print(r"\begin{document}")
add_beginning()
add_main_body()
print(r"\end{document}")
close_file_and_recover_stdout()
make_pdf()