-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
68 lines (48 loc) · 1.67 KB
/
generate.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
67
68
import os
import sys
from configobj import ConfigObj
from jinja2 import Environment, FileSystemLoader
def get_row(values):
if isinstance(values, str):
l = values.split('$$')
else:
l = values
return l[0], [] if len(l) == 1 else l[1:]
def load_configuration(file):
lines = []
prev_line = []
with open(file) as fd:
for line in fd:
current_line = line.strip()
if current_line:
prev_line.append(current_line)
else:
lines.append(' '.join(prev_line))
prev_line = []
else:
lines.append(' '.join(prev_line))
return lines
def generate_cheatsheet(configuration_file, output_file, static=True):
try:
os.remove(output_file)
except OSError:
pass
kw = {}
items = ConfigObj(configuration_file)
kw['is_static'] = static # set static to False to generate a Jinja template
title = items.pop('title', os.path.basename(configuration_file))
kw['name'] = title
kw['columns'] = items.pop('columns', 2)
footer = items.pop('footer', None)
if footer is not None:
kw['footer'] = footer
kw['items'] = items
directory = os.path.dirname(os.path.abspath(__file__))
env = Environment(loader=FileSystemLoader(os.path.join(directory, 'templates')))
env.globals['get_row'] = get_row
with open(os.path.join(directory, 'style', 'cheatsheet.css')) as fd:
kw['ccs_content'] = fd.read()
with open(os.path.join(output_file), 'w') as fd:
fd.write(env.get_template('cheatsheet.html').render(**kw))
if __name__ == '__main__':
generate_cheatsheet(sys.argv[1], sys.argv[2])