forked from codehs/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_html.py
57 lines (43 loc) · 1.64 KB
/
build_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
# Import the pyyaml library
# Command to run
# [~/code/book]$
# python build_html.py introcs/setup/config.yaml introcs/build/book.html
import yaml
import sys
import os
HEADER_HTML = "static/header.html"
def process_file(config_filename, output_filename):
"""This processe the filename to generate a html page."""
config_dir = os.path.dirname(config_filename)
with open(output_filename, 'w') as outfile:
# Write out the HTML header
with open(HEADER_HTML, 'r') as f:
outfile.write(f.read())
with open(config_filename, 'r') as f:
# Change directory to the config file
os.chdir(config_dir)
# Now change to the directory of files
# This way we can just access the files with the file names
os.chdir('../files')
book = yaml.load(f)
for entry in book:
chapter_file = entry['file']
print chapter_file
with open(chapter_file) as infile:
div = "<div id='%s' class='mkdown-text'>\n" % entry['name']
outfile.write(div)
outfile.write(infile.read())
outfile.write("</div>\n")
outfile.write("</html>")
def start():
if not len(sys.argv) == 3:
print "Expecting two arguments"
print "python build_html.py <configfile> <outputfile>"
print "Example:"
print "python build_html.py introcs/setup/config.yaml \
introcs/build/book.html"
sys.exit(1)
process_file(sys.argv[1], sys.argv[2])
print "Completed Generating Book"
if __name__ == "__main__":
start()