-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
executable file
·275 lines (227 loc) · 8.69 KB
/
generator.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python3
import argparse
import os
import json
import css_html_js_minify
import mimetypes
import shutil
from pprint import pprint
import template
def load_config(config_path):
config = {}
if os.path.isfile(config_path):
with open(config_path) as fd:
config = json.load(fd)
if "files" not in config: config["files"] = {}
return config
def copy_file(src, dest):
with open(src, 'r') as fdr:
with open(dest, 'w') as fdw:
fdw.write(fdr.read())
# pray that it never comes to this :(
def delete_with_extreme_prejudice(path):
print("Removing:", path)
if os.path.isfile(path):
os.remove(path)
else:
for root, dirs, files in os.walk(path, topdown = False):
for file in files:
os.remove(os.path.join(root,file))
for dir in dirs:
os.rmdir(os.path.join(root,dir))
os.rmdir(path)
class Path_Reformatter:
SRC = 0
S = 0
MUTUAL = 1
M = 1
DEST = 2
D = 2
def __init__(self, src, dest):
self.dir_src = os.path.normpath(src)
self.dir_dest = os.path.normpath(dest)
self.dir_src_len = len(self.dir_src) + 1
self.dir_dest_len = len(self.dir_dest) + 1
def src(self, type, path):
if type == self.SRC:
return path
elif type == self.MUTUAL:
return os.path.join(self.dir_src, path)
elif type == self.DEST:
return os.path.join(self.dir_src, path[self.dir_dest_len:])
raise ValueError("Unrecognised type. Must be one of SRC, MUTUAL, DEST")
def mutual(self, type, path):
if type == self.SRC:
return path[self.dir_src_len:]
elif type == self.MUTUAL:
return path
elif type == self.DEST:
return path[self.dir_dest_len:]
raise ValueError("Unrecognised type. Must be one of SRC, MUTUAL, DEST")
def absolute(self, type, path):
return "/" + self.mutual(type, path)
def dest(self, type, path):
if type == self.SRC:
return os.path.join(self.dir_dest, path[self.dir_src_len:])
elif type == self.MUTUAL:
return os.path.join(self.dir_dest, path)
elif type == self.DEST:
return path
raise ValueError("Unrecognised type. Must be one of SRC, MUTUAL, DEST")
def copy_file(self, mutual):
shutil.copy(self.src(self.MUTUAL, mutual), self.dest(self.MUTUAL, mutual))
def parse_args():
parser = argparse.ArgumentParser(description='Renders a website from a template of static files.')
parser.add_argument('--skel', help='A directory containing the skeleton of the website.')
parser.add_argument('--webdir', help='Directory where the site will be rendered to.')
parser.add_argument('--url', help='The web facing address of --webdir')
parser.add_argument('--minify', action='store_true', help='Runs HTML, Javascript and CSS files through a minifier.')
parser.add_argument('--redirect', choices=['none', 'htaccess', 'symlink'], help='Choose how to redirect default_ajax_action.json to the right file for every directory. If "none" is chosen, you should make arrangements to perform the redirects some other way. eg, modifying the server config', default='htaccess')
return parser.parse_args()
def main():
global webpage_template
args = parse_args()
path = Path_Reformatter(src=args.skel, dest=args.webdir)
template.load(args.url)
master_index = dict()
master_anomaly = dict()
config = load_config(os.path.join(path.dir_src, "config.json"))
for path_src, dirs, files in os.walk(path.dir_src):
if not os.path.exists(path.dest(path.SRC, path_src)):
os.mkdir(path.dest(path.SRC, path_src))
# build an index of files to display
index = []
path_mutual = path.mutual(path.SRC, path_src)
if path_mutual in master_index:
index = master_index[path_mutual]
# get a list of files already in the destination, to delete anything unexpected.
listing = None
if path_mutual in master_anomaly:
listing = master_anomaly[path_mutual]
else:
listing = os.listdir( path.dest(path.MUTUAL, path_mutual) )
has_defaulthtml = False
print("/"+path.mutual(path.SRC, path_src))
for dir in dirs:
if dir in listing: listing.remove(dir)
destpath = os.path.join( path.dest(path.SRC, path_src) , dir)
if os.path.isfile(destpath):
os.remove(destpath)
index.append({"name":dir,"type":"dir", "mime":"Directory", "size":0})
for file in files:
ext = os.path.splitext(file)[1]
mutualpath = path.mutual(path.SRC, os.path.join(path_src, file) )
if mutualpath in config["files"]:
if "action" in config["files"][mutualpath]:
if config["files"][mutualpath]["action"] == "ignore":
continue
if config["files"][mutualpath]["action"] == "raw":
path.copy_file(mutualpath)
if not "listed" in config["files"][mutualpath] or config["files"][mutualpath]["listed"]:
index.append({
"name": file,
"type": "file",
"mime": mimetypes.guess_type(mutualpath)[0],
"size": os.path.getsize(path.dest(path.MUTUAL, mutualpath))
})
if file in listing: listing.remove(file)
continue
if "forcetype" in config["files"][mutualpath]:
ext = "." + config["files"][mutualpath]["forcetype"]
if file in listing: listing.remove(file)
if ext in [".html", ".htm", ".md"]:
newname = file[:-len(ext)] + ".html"
if newname in listing: listing.remove(newname)
if newname+".json" in listing: listing.remove(newname+".json")
path_dest = path.dest(path.MUTUAL, mutualpath)[:-len(ext)] + ".html"
page = None
if ext == ".md":
page = template.Markdown(name=newname, pwd=path.absolute(path.SRC, path_src) )
else:
page = template.Html(name=newname, pwd=path.absolute(path.SRC, path_src) )
with open(path.src(path.MUTUAL, mutualpath), 'r') as fd:
page.parse(fd.read())
with open(path_dest, 'w') as fd:
fd.write(page.render(minify=args.minify))
with open(path_dest + ".json", 'w') as fd:
fd.write(json.dumps(page.as_dict(minify=args.minify)))
index.append({
"name": newname,
"type": "html",
"mime": "text/html",
"size": os.path.getsize(path_dest)
})
elif ext in [".jpeg", ".jpg", ".png", ".svg"]:
path.copy_file(mutualpath)
index.append({
"name": file,
"type": "file",
"mime": mimetypes.guess_type(mutualpath)[0],
"size": os.path.getsize(path.dest(path.MUTUAL, mutualpath))
})
elif ext in [".txt", ".conf"]:
path.copy_file(mutualpath)
index.append({
"name": file,
"type": "file",
"mime": "text/plain",
"size": os.path.getsize(path.dest(path.MUTUAL, mutualpath))
})
else:
print("Found unknown file {}, type: {}".format(mutualpath, mimetypes.guess_type(mutualpath)))
master_index[path_mutual] = index
master_anomaly[path_mutual] = listing
# generate index pages
pathobj = Path_Reformatter(src="", dest=args.webdir)
for key in master_index:
added_files = create_index_page(pathobj, key, master_index[key], args.redirect)
for file in added_files:
if file in master_anomaly[key]: master_anomaly[key].remove(file)
# delete anomilies
for dir in master_anomaly:
for anomaly in master_anomaly[dir]:
delete_with_extreme_prejudice(os.path.join(path.dest(path.MUTUAL, dir), anomaly))
def create_index_page(path, path_mut, index, redirect_mode):
# create index pages
added_files = []
index = sorted(index, key=lambda row: (row["type"] != "dir", row["name"]))
with open(os.path.join(path.dest(path.MUTUAL, path_mut), "index.html"), 'w') as fd:
fd.write( template.Html_Index(path.absolute(path.MUTUAL, path_mut), index).render(minify=False) )
added_files.append("index.html")
with open(os.path.join(path.dest(path.MUTUAL, path_mut), "index.html.json"), 'w') as fd:
json_index = {}
for item in index:
json_index[item['name']] = item
fd.write(json.dumps({
"type":"dir",
"complete":True,
"children":json_index
}))
added_files.append("index.html.json")
if redirect_mode == "symlink":
# create default_ajax_action.json symlink
symlink = os.path.join(path.dest(path.MUTUAL, path_mut), "default_ajax_action.json")
symlink_dest = "./index.html.json"
if has_defaulthtml:
symlink_dest = "./default.html.json"
if os.path.exists(symlink):
if not os.path.islink(symlink) or (os.readlink(symlink) != symlink_dest):
if os.path.isdir(symlink):
delete_with_extreme_prejudice(symlink)
else:
os.remove(symlink)
os.symlink(symlink_dest, symlink)
else:
os.symlink(symlink_dest, symlink)
added_files.append("default_ajex_action.json")
elif redirect_mode == "htaccess" and path.mutual(path.MUTUAL, path_mut) == "":
with open(path.dest(path.MUTUAL, ".htaccess"), 'w') as fd:
fd.write("""RewriteEngine On
RewriteCond %{{DOCUMENT_ROOT}}{0}/$1default.html.json -f
RewriteRule ^(.*/)?default_ajax_action.json$ $1default.html.json [L]
RewriteRule ^(.*/)?default_ajax_action.json$ $1index.html.json [L]
DirectoryIndex default.html index.html
""".format(template.uri()))
added_files.append(".htaccess")
return added_files
main()