-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke_template_update.py
43 lines (32 loc) · 1.54 KB
/
invoke_template_update.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
#!/usr/bin/python3
import argparse
import os
parser = argparse.ArgumentParser(description='Updates html files to have same assets and navbar as in "template.html", \
\nThe updating is done according to "PAGE CONTENT" comments in the html files.')
parser.add_argument('inputs', type=str, nargs='+')
parser.add_argument('--mode', type=str,
help='set input mode, default: filenames, "dir": directories, "recursive": not implemented yet.')
args = parser.parse_args()
mode = args.mode if args.mode else "filename"
splitter = 'PAGE CONTENT'
with open('template.html', 'r') as template_file:
template_html = template_file.read()
template_parts = template_html.split(splitter)
template_begin = template_parts[0]
template_end = template_parts[-1]
def update_to_match_template(filename):
with open(filename, 'r') as html_file:
html = html_file.read()
html_content = html.split(splitter)[1]
updated_html_parts = (template_begin, html_content, template_end)
with open(filename, 'w') as html_file:
html_file.write(splitter.join(updated_html_parts))
print('updated file', filename)
if mode == "filename":
for filename in args.inputs:
update_to_match_template(filename)
if mode == "dir":
for directory in args.inputs:
print('updating files in directory {}...'.format(directory))
for filename in [fn for fn in os.listdir(directory) if len(fn) > 5 and fn[-5:] == '.html']:
update_to_match_template(os.path.join(directory, filename))