-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·135 lines (106 loc) · 3.7 KB
/
generate.py
File metadata and controls
executable file
·135 lines (106 loc) · 3.7 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/python3
import yaml
from os.path import expanduser
from shutil import copyfile
from bs4 import BeautifulSoup
# get home directory
home = expanduser("~")
# get config path
config_dir = home + '/.config/StartTree'
config_path = home + '/.config/StartTree/config.yaml'
# get cache path
cache_dir = home + '/.cache/StartTree'
def prettifyHTML(html):
soup = BeautifulSoup(html, 'html.parser')
prettyHTML = soup.prettify()
return prettyHTML
def parse_yaml():
with open(config_path, mode='r') as file:
file_dict = yaml.full_load(file)
return file_dict
def print_keys(dictionary):
for key in dictionary:
print(key)
if isinstance(dictionary[key], dict):
print_keys(dictionary[key])
def gen_list_indices(html_file, file_dict):
for key in file_dict:
value = file_dict[key]
print(key + "->" + str(value))
if not isinstance(value, list):
html_file.write("<li><a href=\"" + value + "\">" + key + "</a></li>")
else:
html_file.write("<li><a href=\"" + value[0] + "\">" + value[1] + "</a></li>")
def gen_col_headers(html_file, file_dict):
for key in file_dict:
html_file.write("<li>\n")
html_file.write("<h1>" + key + "</h1>\n")
html_file.write("<ul>\n")
# generate list indices
gen_list_indices(html_file, file_dict[key])
html_file.write("</ul>\n")
html_file.write("</li>\n")
def gen_columns(html_file, file_dict):
for key in file_dict:
if key.split("_")[0] == "tree":
html_file.write("<div class=\"column\">\n")
html_file.write("<div class=\"tree\">\n")
html_file.write("<h1>.</h1>\n")
html_file.write("<ul>\n")
# generate the column headers
gen_col_headers(html_file, file_dict[key])
html_file.write("</ul>\n")
html_file.write("</div>\n")
html_file.write("</div>\n")
def gen_html(file_dict):
print("Generating index.html...")
# open files
skeleton_html = open(cache_dir + '/skeletons/index.html', 'r')
cache_html = open(cache_dir + '/index.html', 'w+')
# copy skeleton_html to cache_html until Column Start comment
lines = skeleton_html.readlines()
for line in lines:
if line == "<!-- Columns start -->\n":
gen_columns(cache_html, file_dict)
else:
cache_html.write(line)
# prettify
cache_html.seek(0)
pretty_string = cache_html.read()
pretty_string = prettifyHTML(pretty_string)
cache_html.close()
cache_html = open(cache_dir + '/index.html', 'w')
cache_html.write(pretty_string)
# close files
skeleton_html.close()
cache_html.close()
print("Done!")
def gen_style(file_dict):
skeleton_style = open(cache_dir + '/skeletons/style.css', 'r')
cache_style = open(cache_dir + '/styles/style.css', 'w')
# find style attributes in file_dict
font_size = 20
theme = "void"
for key in file_dict:
if key == "font_size":
font_size = file_dict[key]
if key == "theme":
theme = file_dict[key]
if theme == "pywal":
theme = home + '/.cache/wal/colors.css'
else:
theme = cache_dir + '/themes/' + theme + '.css'
copyfile(theme, home + '/.cache/StartTree/styles/colors.css')
cache_style.write("@import url('./colors.css');\n")
lines = skeleton_style.readlines()
for line in lines:
if line == "/* font-size */\n":
cache_style.write("font-size: " + str(font_size) + "px;\n")
else:
cache_style.write(line)
def main():
file_dict = parse_yaml()
gen_style(file_dict)
gen_html(file_dict)
if __name__ == '__main__':
main()