2020-08-24 06:31:09 +02:00
|
|
|
#!/bin/python3
|
|
|
|
|
|
|
|
import yaml
|
2020-08-24 07:08:07 +02:00
|
|
|
import os
|
|
|
|
from os.path import expanduser
|
|
|
|
from shutil import copyfile
|
2020-08-24 09:51:35 +02:00
|
|
|
from bs4 import BeautifulSoup
|
2020-08-24 07:08:07 +02:00
|
|
|
|
|
|
|
# get home directory
|
|
|
|
home = expanduser("~")
|
|
|
|
|
|
|
|
# get config path
|
|
|
|
config_dir = home + '/.config/StartTree'
|
|
|
|
config_path = home + '/.config/StartTree/config.yaml'
|
|
|
|
|
2020-08-24 08:38:15 +02:00
|
|
|
# get cache path
|
|
|
|
cache_dir = home + '/.cache/StartTree'
|
2020-08-24 07:08:07 +02:00
|
|
|
|
2020-08-24 10:12:34 +02:00
|
|
|
def prettifyHTML(html):
|
2020-08-24 09:51:35 +02:00
|
|
|
soup = BeautifulSoup(html, 'html.parser')
|
2020-08-24 10:12:34 +02:00
|
|
|
prettyHTML = soup.prettify()
|
2020-08-24 09:51:35 +02:00
|
|
|
return prettyHTML
|
|
|
|
|
2020-08-24 08:38:15 +02:00
|
|
|
def parse_yaml():
|
|
|
|
with open(config_path, mode='r') as file:
|
|
|
|
file_dict = yaml.full_load(file)
|
|
|
|
file.close()
|
|
|
|
return file_dict
|
|
|
|
|
|
|
|
def print_keys(dictionary):
|
|
|
|
for key in dictionary:
|
|
|
|
print(key)
|
|
|
|
if isinstance(dictionary[key], dict):
|
|
|
|
print_keys(dictionary[key])
|
|
|
|
|
2020-08-24 09:53:03 +02:00
|
|
|
def gen_list_indices(html_file, file_dict):
|
|
|
|
for key in file_dict:
|
2020-08-24 23:10:37 +02:00
|
|
|
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>")
|
2020-08-24 09:53:03 +02:00
|
|
|
|
2020-08-24 09:47:57 +02:00
|
|
|
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")
|
2020-08-24 08:38:15 +02:00
|
|
|
|
2020-08-24 09:47:57 +02:00
|
|
|
# generate list indices
|
2020-08-24 10:12:34 +02:00
|
|
|
gen_list_indices(html_file, file_dict[key])
|
2020-08-24 09:47:57 +02:00
|
|
|
|
|
|
|
html_file.write(" </ul>\n")
|
|
|
|
html_file.write("</li>\n")
|
|
|
|
|
|
|
|
def gen_columns(html_file, file_dict):
|
|
|
|
for key in file_dict:
|
2020-08-25 00:07:28 +02:00
|
|
|
if key == "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")
|
2020-08-24 09:47:57 +02:00
|
|
|
|
2020-08-25 00:07:28 +02:00
|
|
|
# generate the column headers
|
|
|
|
gen_col_headers(html_file, file_dict[key])
|
2020-08-24 09:47:57 +02:00
|
|
|
|
2020-08-25 00:07:28 +02:00
|
|
|
html_file.write(" </ul>\n")
|
|
|
|
html_file.write(" </div>\n")
|
|
|
|
html_file.write("</div>\n")
|
2020-08-24 09:47:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
def gen_html(file_dict):
|
2020-08-24 08:38:15 +02:00
|
|
|
print("Generating index.html...")
|
|
|
|
|
|
|
|
# open files
|
|
|
|
skeleton_html = open('./skeletons/index.html', 'r')
|
2020-08-24 10:12:34 +02:00
|
|
|
cache_html = open(cache_dir + '/index.html', 'w+')
|
2020-08-24 08:38:15 +02:00
|
|
|
|
|
|
|
# copy skeleton_html to cache_html until Column Start comment
|
|
|
|
lines = skeleton_html.readlines()
|
|
|
|
for line in lines:
|
2020-08-24 09:47:57 +02:00
|
|
|
if line == "<!-- Columns start -->\n":
|
|
|
|
gen_columns(cache_html, file_dict)
|
2020-08-24 08:38:15 +02:00
|
|
|
else:
|
|
|
|
cache_html.write(line)
|
|
|
|
|
2020-08-24 10:12:34 +02:00
|
|
|
# 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)
|
|
|
|
|
2020-08-24 08:38:15 +02:00
|
|
|
# close files
|
|
|
|
skeleton_html.close()
|
|
|
|
cache_html.close()
|
|
|
|
|
|
|
|
print("Done!")
|
|
|
|
|
2020-08-25 00:07:28 +02:00
|
|
|
def gen_style(file_dict):
|
|
|
|
skeleton_style = open('./skeletons/style.css', 'r')
|
|
|
|
cache_style = open(cache_dir + '/styles/style.css', 'w')
|
|
|
|
|
|
|
|
font_size = 20
|
|
|
|
for key in file_dict:
|
|
|
|
if key == "font_size":
|
|
|
|
font_size = file_dict[key]
|
|
|
|
break
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-08-24 09:47:57 +02:00
|
|
|
def main():
|
2020-08-24 08:38:15 +02:00
|
|
|
file_dict = parse_yaml()
|
2020-08-25 00:07:28 +02:00
|
|
|
gen_style(file_dict)
|
2020-08-24 09:47:57 +02:00
|
|
|
gen_html(file_dict)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|