forked from gregorio-project/gregorio-project.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildSite.py
executable file
·66 lines (57 loc) · 1.92 KB
/
BuildSite.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
#! /usr/bin/env python2
"""
Very simple script to update headers and footers for the gregorio website.
./BuildSite.py replaces what is above <!-- header --> with the content
of header.html, and what is below <!-- footer --> with the content of
footer.html.
"""
import os
from os.path import join, getsize
HEADER_LIMIT = '<!-- header -->'
FOOTER_LIMIT = '<!-- footer -->'
def get_all_files():
"Get all html files in current directory, recursively"
res = []
for root, dirs, files in os.walk('.'):
for filename in files:
if (filename.endswith(".html") and filename != 'header.html' and filename != 'footer.html'):
res.append(join(root,filename))
if 'illus' in dirs:
dirs.remove('illus')
if '.git' in dirs:
dirs.remove('.git')
return res
def get_header():
"Get header.html content"
with open('header.html', 'r') as infile:
HEADER=infile.read()
def get_footer():
"Get footer.html content"
with open('footer.html', 'r') as infile:
FOOTER=infile.read()
def main():
"Get the job done"
files = get_all_files()
header = ''
footer = ''
with open('header.html', 'r') as infile:
header=infile.read()
with open('footer.html', 'r') as infile:
footer=infile.read()
for myfile in files:
result = []
state = 'header'
with open(myfile, 'r') as infile:
for line in infile:
if state == 'header' and HEADER_LIMIT in line:
result.append(line)
state = 'main'
elif state == 'main' and FOOTER_LIMIT in line:
result.append(line)
break
elif state == 'main':
result.append(line)
with open(myfile, 'w') as outfile:
outfile.write(header + ''.join(result) + footer)
if __name__ == "__main__":
main()