-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstgen-cli.py
115 lines (87 loc) · 2.56 KB
/
stgen-cli.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
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
import yaml,sys,os
from bs4 import BeautifulSoup
HTML_TEMPLATE = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{0}
<title>{1}</title>
</head>
<body>
{2}
</body>
</html>
'''
LAYOUT_TEMPLATE = '{0}\n{1}\n{2}'
INCLUDES_DIR = "./includes"
# beautify html code function
def beautify_html(html_data):
return BeautifulSoup(html_data, 'html.parser').prettify()
def readhtmlfile(filename):
data = None
with open(filename, 'r') as f:
data = f.read()
return data
def readfile(filename):
data = None
with open(filename, 'r') as f:
data = yaml.safe_load(f)
return data
def writefile(filename, data):
with open(filename, 'w') as f:
beautify_html_data = beautify_html(data)
f.write(beautify_html_data)
return True
return False
def includes(includes_dir,layout,name):
str = ""
for i in layout[name]['include']:
str+=readhtmlfile(f"{includes_dir}\{i}") + "\n"
return str
def depstr(deps,name):
str = ""
for i in deps:
if name == "scripts":
str+= f'<script src="{i}" defer></script>'
else:
str+= f'<link rel="stylesheet" href="{i}">'
return str
def get_all_files_names(directory):
file_names = []
for root,directories,files in os.walk(directory):
for filename in files:
file_names.append(filename)
return file_names
def handleConfig(config):
deps = config["dependencies"]
scripts = depstr(deps["scripts"],"scripts")
styles = depstr(deps["styles"],"styles")
return f"{styles}\n{scripts}\n{deps['html']}"
def configure(layoutfile,configData,compilehtmlfile):
layout = readfile(layoutfile)
layout = layout["layout"]
template_dir = configData["template_dir"]
header = includes(INCLUDES_DIR,layout,"header")
main = get_main_content(template_dir,compilehtmlfile)
footer = includes(INCLUDES_DIR,layout,"footer")
layout_template = LAYOUT_TEMPLATE.format(header,main,footer)
html_template = HTML_TEMPLATE.format(handleConfig(configData),configData["title"],layout_template)
writefile(f'compiled\{compilehtmlfile}.html',html_template)
def get_main_content(template_dir,compilehtmlfile):
html_fragment_content = readhtmlfile(f"{template_dir}\{compilehtmlfile}.stg")
return """
<main>
{0}
</main>
""".format(html_fragment_content)
def main(file):
configData = readfile('./.gen/page.yml')
configure("./.gen/layout.yml",configData,file)
if __name__ == "__main__":
files = get_all_files_names("./templates")
for file in files:
file = file.split(".")[0]
main(file)