-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.py
executable file
·77 lines (55 loc) · 1.88 KB
/
gen.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
import re, os, hashlib, yaml
scl = yaml.safe_load(open("scl.yaml").read())
LANGS = {
"c": "C",
"cpp": "C++",
"h": "C++",
"py": "Python",
"pl": "Perl",
"sh": "sh",
"java": "Java",
"json": "C++",
}
def lang(ext):
if not ext:
return "{}"
ext = ext.lower()
return LANGS[ext] if ext in LANGS else "{}"
def gen_section(sect_yaml):
global line_count
name = sect_yaml["name"]
dirname = sect_yaml["dir"]
files = sect_yaml["files"]
sect = []
sect.append("\\section{%s}" % name)
subsects = []
for idx, f in enumerate(files):
title = f["title"]
fname = f["fname"]
desc = f.get("desc", None)
extension = fname.split(".")[-1]
sect.append("\\subsection{%s}" % title)
def digest_line(s):
return hashlib.md5(re.sub(r"\s|//.*", "", s).encode()).hexdigest()[-4:]
with open("src/%s/%s" % (dirname, fname), "r") as fp:
code = fp.read()
line_count = 1
for line in code.split("\n"):
sect.append("\\createlinenumber{%d}{%d}" % (line_count, line_count))
line_count += 1
if desc:
sect.append("\\begin{mdframed}[hidealllines=true,backgroundcolor=blue!5]")
sect.append(desc.strip())
sect.append("\\end{mdframed}\\vspace{-10pt}")
# "src/%s/%s"
sect.append("\\lstinputlisting[")
sect.append(" caption = {\\bf %s}," % fname)
sect.append(" label = {%s}," % fname)
sect.append(" language = %s" % lang(extension))
sect.append("]{../src/%s%s}" % (dirname, fname))
# sect.append("\\begin{lstlisting}[language=%s]" % lang(extension))
# sect.append(code.replace("$", "{dollar}"))
# sect.append("\\end{lstlisting}")
return "\n".join(sect)
for section in scl:
print(gen_section(section))