-
Notifications
You must be signed in to change notification settings - Fork 0
/
_generator.py
51 lines (47 loc) · 1.77 KB
/
_generator.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
#!/usr/bin/python
"""
------Notebook Generator------
"""
import subprocess
CONTENT_LATEX = "contentNotebook.tex"
TEMPLATE_LATEX = "templateNotebook.tex"
CODES_TREE = "content.txt"
LATEXC = ["latexmk", "-pdf", TEMPLATE_LATEX]
def getSections(secs=[], aux={}, sec_name=None):
with open(CODES_TREE, 'r') as f:
for line in f:
line = line.strip()
if len(line) == 0: continue
if (line.startswith("_")): continue
tmp = line.split('/')
if len(tmp) == 1: continue;
if tmp[0] not in aux :
aux[tmp[0]] = sec_name = tmp[0]
subsecs = []
if sec_name is not None: secs.append((sec_name, subsecs))
filename = tmp[-1]
subsec_name = ' '.join([x for x in tmp[1:-1]])+" "+ filename.split(".")[0]
dire = '/'.join([x for x in tmp[:-1]])
if subsec_name is None: raise ValueError('Subsec given without sec')
subsecs.append((filename, subsec_name, dire))
return secs
def getStyle(filename):
ext = filename.lower().split('.')[-1]
if ext in ['cc', 'cpp']: return 'cpp'
elif ext in ['java']: return 'java'
elif ext in ['py']: return 'py'
else: return 'txt'
def txt2Latex(secs, tex=''):
for (sec_name, subsecs) in secs:
tex += '\\section{%s}\n' % sec_name
for (filename, subsec_name, dire) in subsecs:
tex += '\\subsection{%s}\n' % subsec_name
tex += '\\raggedbottom\\lstinputlisting[style=%s]{%s/%s}\n' % (getStyle(filename), dire, filename)
tex += '\\hrulefill\n'
tex += '\n'
return tex
if __name__ == "__main__":
print (__doc__)
tex = txt2Latex(getSections())
with open(CONTENT_LATEX, 'w') as f: f.write(tex)
subprocess.call(LATEXC)