-
Notifications
You must be signed in to change notification settings - Fork 497
/
generate_html.py
executable file
·64 lines (59 loc) · 2.33 KB
/
generate_html.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
#!/usr/bin/python
import subprocess
code_dir = "code"
title = "Stanford ACM-ICPC Team Notebook"
def add_toc(html, sections):
html2 = ''
for l in html.splitlines():
html2 += l + '\n'
if l == "<A NAME=\"top\">":
html2 += "<CENTER><H1><U>" + title + "</U></H1></CENTER>\n"
html2 += "<H1>Table of Contents</H1>\n"
cnt = 1
for (section_name, subsections) in sections:
html2 += "<H2>" + section_name + "</H2>\n\n"
html2 += "<OL START=" + str(cnt) + ">\n"
for (_, subsection_name) in subsections:
html2 += "<LI><A HREF=\"#file" + str(cnt) + "\">" + subsection_name + "</A></LI>\n"
cnt += 1
html2 += "</OL>\n"
html2 += "<HR>\n"
return html2
def get_sections():
sections = []
section_name = None
with open('contents.txt', 'r') as f:
for line in f:
if '#' in line: line = line[:line.find('#')]
line = line.strip()
if len(line) == 0: continue
if line[0] == '[':
if section_name is not None:
sections.append((section_name, subsections))
section_name = line[1:-1]
subsections = []
else:
tmp = line.split('\t', 1)
if len(tmp) == 1:
raise ValueError('Subsection parse error: %s' % line)
filename = tmp[0]
subsection_name = tmp[1]
if subsection_name is None:
raise ValueError('Subsection given without section')
subsections.append((filename, subsection_name))
if section_name is not None:
sections.append((section_name, subsections))
return sections
def get_html_enscript(sections):
enscript_options = ["enscript", "-E", "--color", "--language=html", "-o", "-", "-t", title]
filenames = []
for (_, subsections) in sections:
filenames += [code_dir + '/' + filename for (filename, _) in subsections]
bstr = subprocess.check_output(enscript_options + filenames)
return bstr.decode('utf-8')
if __name__ == "__main__":
sections = get_sections()
html = get_html_enscript(sections)
html = add_toc(html, sections)
with open('notebook.html', 'w') as f:
f.write(html)