forked from nju-icpc/code-library-legacy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen.py
executable file
·92 lines (65 loc) · 2.61 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python2
import re, os, hashlib, sys
import oyaml as yaml
line_count = 1
scl = yaml.load(open("scl.yaml").read())
LANGS = {"c": "C", "cpp": "C++", "py": "Python", "pl": "Perl",
"sh": "sh", "java": "Java"}
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']
sys.stderr.write('Section ' + name + '\n')
sect = []
if name == 'Appendices' :
sect.append('\\clearpage')
dirname = sect_yaml['dir']
files = sect_yaml['files']
sect.append("\\section{%s}" % name)
subsects = []
for (idx, f) in enumerate(files):
title = f['title']
sys.stderr.write('Processing ' + title + '\n')
fname = f['fname']
desc = f.get('desc', None)
usage = f.get('usage', None)
tcmplx = f.get('time', None)
with open("src/%s/%s" % (dirname, fname), "r") as fp:
code = fp.read()
extension = fname.split('.')[-1]
sect.append("\\subsection{%s}" % title)
if extension == 'tex' :
sect.append(code)
continue
def digest_line(s):
return hashlib.md5(re.sub(r'\s|//.*', '', s).encode('utf8')).hexdigest()[-4:]
for line in code.split("\n"):
sect.append("\\createlinenumber{%d}{%s}" % (line_count, digest_line(line)))
line_count += 1
descriptions = []
if desc:
descriptions.append(desc.strip())
descriptions.append('\\par')
if usage:
descriptions.append('\\textbf{Usage:}\\par')
descriptions.append('\\begin{tabular}{p{3.5cm} p{8cm}}')
for (funct, descript) in usage.items() :
descriptions.append('\lstinline|%s| & %s \\\\' % (funct, descript))
descriptions.append('\\end{tabular}')
if tcmplx: # print time complexity
descriptions.append('\\textbf{Time Complexity:}')
descriptions.append(tcmplx.strip())
descriptions.append('\\par')
if descriptions :
# sect.append('\\begin{mdframed}[hidealllines=true,backgroundcolor=blue!5]')
sect.append('\n'.join(descriptions))
# sect.append('\\end{mdframed}\\vspace{-10pt}')
sect.append("\\begin{lstlisting}[language=%s]" % lang(extension))
sect.append(code.decode('utf-8'))
sect.append("\\end{lstlisting}")
return "\n".join(sect).encode('utf-8')
for section in scl:
print gen_section(section)