-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
210 lines (161 loc) · 6.33 KB
/
main.py
File metadata and controls
210 lines (161 loc) · 6.33 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import posixpath
import re
import os
import yaml
import argparse
def escape_latex_special_chars(text):
"""
转义 LaTeX 特殊字符,包括: _ % $ # & { } ~ ^
"""
special_chars = {
"\\": r"\textbackslash{}",
"&": r"\&",
"%": r"\%",
"$": r"\$",
"#": r"\#",
"_": r"\_",
"{": r"\{",
"}": r"\}",
"~": r"\textasciitilde{}",
"^": r"\textasciicircum{}",
}
return re.sub(
"|".join(re.escape(key) for key in special_chars.keys()),
lambda match: special_chars[match.group()],
text,
)
def read_file(file_path):
if not os.path.isfile(file_path):
raise FileNotFoundError(f"文件不存在:{file_path}")
with open(file_path, "r", encoding="UTF-8") as f:
return f.read()
def get_config(directory, filename=None):
config_paths = []
if filename:
config_paths.append(posixpath.join(directory, filename))
config_paths += [
posixpath.join(directory, "config.yml"),
posixpath.join(directory, "config.yaml"),
]
for config_path in config_paths:
if not os.path.isfile(config_path):
continue
with open(config_path, "r", encoding="UTF-8") as f:
config = yaml.safe_load(f)
if config is None:
continue
return config
raise FileNotFoundError(f"目录 {directory} 未找到配置文件")
def detect_style_by_extension(filename: str) -> str:
"""根据文件扩展名返回 LaTeX lstlisting style"""
if (
filename.endswith(".cpp")
or filename.endswith(".hpp")
or filename.endswith(".h")
):
return "cppstyle"
elif filename.endswith(".java"):
return "javastyle"
elif filename.endswith(".py"):
return "pythonstyle"
else:
return "" # 默认无 style
# 生成代码块 LaTeX 内容
def generate_latex_for_item(directory, item, depth):
latex_parts = []
if "name" not in item:
raise ValueError(f"目录 {directory} 配置文件有误(未配置 name)")
name = escape_latex_special_chars(item.get("name"))
if depth == 0:
latex_parts.append(f"\\section{{{name}}}\n")
elif depth == 1:
latex_parts.append(f"\\subsection{{{name}}}\n")
elif depth == 2:
latex_parts.append(f"\\subsubsection{{{name}}}\n")
else:
raise ValueError(f"目录 {directory} 配置过深")
# 处理 code-pre
if "code-pre" in item:
file_path = posixpath.join(directory, item["code-pre"])
contents = read_file(file_path)
latex_parts.append(contents + "\n")
# 处理 codes (支持字符串或数组)
if "codes" in item:
codes_value = item["codes"]
if isinstance(codes_value, str): # 单个文件
codes_list = [codes_value]
elif isinstance(codes_value, list): # 多个文件
codes_list = codes_value
elif codes_value is None: # 空值
codes_list = []
else:
raise ValueError(f"codes 字段格式错误: {codes_value}")
for code_file in codes_list:
file_path = posixpath.join(directory, code_file)
if not os.path.isfile(file_path):
raise FileNotFoundError(f"代码文件不存在:{file_path}")
style = detect_style_by_extension(code_file)
if style:
latex_parts.append(f"\\lstinputlisting[style={style}]{{{file_path}}}\n")
else:
latex_parts.append(f"\\lstinputlisting{{{file_path}}}\n")
# 处理 code-post
if "code-post" in item:
file_path = posixpath.join(directory, item["code-post"])
contents = read_file(file_path)
latex_parts.append(contents + "\n")
return "\n".join(latex_parts)
def generate_latex_from_config(directory, depth=0):
config = get_config(directory)
latex_sections = []
for item in config.get("contents") or []:
if "directory" in item:
subdir_path = posixpath.join(directory, item["directory"])
if not os.path.isdir(subdir_path):
raise NotADirectoryError(f"子目录不存在或不是目录:{subdir_path}")
latex_sections.append(generate_latex_for_item(directory, item, depth))
latex_sections.append(generate_latex_from_config(subdir_path, depth + 1))
else:
latex_sections.append(generate_latex_for_item(directory, item, depth))
return "\n".join(latex_sections)
def generate_latex(root_dir, root_config_filename=None):
config = get_config(root_dir, root_config_filename)
latex_pre = ""
latex_post = ""
if "latex-pre" in config:
latex_pre_path = config["latex-pre"]
latex_pre = read_file(latex_pre_path)
title = escape_latex_special_chars(config.get("title", "UESTC Nanana Templates"))
author = escape_latex_special_chars(config.get("author", "UESTC_Nanana"))
latex_pre = latex_pre.replace("{PLACEHOLDER:TITLE}", title).replace(
"{PLACEHOLDER:AUTHOR}", author
)
if "latex-post" in config:
latex_post_path = config["latex-post"]
latex_post = read_file(latex_post_path)
code_root = config.get("root-directory", "./templates")
if not os.path.isdir(code_root):
raise NotADirectoryError(f"模板根目录不存在或不是一个目录:{code_root}")
latex_content = generate_latex_from_config(code_root)
return latex_pre + latex_content + latex_post
def write_latex_file(latex_content, output_file):
try:
with open(output_file, "w", encoding="UTF-8") as f:
f.write(latex_content)
except IOError as e:
raise IOError(f"无法写入输出文件 {output_file}: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate LaTeX from templates.")
parser.add_argument("--root-dir", default="./", help="Specify the root directory.")
parser.add_argument(
"--config-file",
default=None,
help="Specify the config file name in the root directory.",
)
parser.add_argument(
"--output-file", default="output.tex", help="Specify the output file name."
)
args = parser.parse_args()
latex_content = generate_latex(args.root_dir, args.config_file)
write_latex_file(latex_content, args.output_file)
print(f"LaTeX 文件已生成:{args.output_file}")