-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (232 loc) · 7.68 KB
/
main.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
import errno
import math
import os
import shutil
import threading
import time
import zipfile
import jinja2
import markdown
import requests
import tqdm
from flask import Flask, send_from_directory
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from typing import List
mark = markdown.Markdown(extensions=['mdx_math', 'toc', 'attr_list'])
scripts = []
styles = []
def recursive_copy(src, dst, rm=True):
"""recursive_copy
Recursive copy files in directory.
:param src: source location
:param dst: destanation.
:param rm: will delete Resource before copying if exists.
"""
try:
if os.path.exists(dst) and rm:
shutil.rmtree(dst)
shutil.copytree(src, dst)
print(f"[ OK ] Copying {src}")
except FileExistsError:
if rm:
raise
except OSError as exc:
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else:
raise
def create_folder(path):
"""create_folder
Create folder if not exists
:param path:
"""
if not os.path.exists(path):
os.makedirs(path)
def create_structure():
"""create_structure
Create file structure for generated pages
and resources.
"""
create_folder("generated")
create_folder("generated/pages")
create_folder("generated/js")
create_folder("generated/loaded")
recursive_copy("templates/js", "generated/js/static", False)
def download_repo(repo):
request = requests.get(
f"https://github.com/{repo}/archive/master.zip", stream=True)
total_size = int(request.headers.get('content-length', 0))
block_size = 1024
wrote = 0
with open("tmp/plugin.zip", 'wb') as out:
for data in tqdm.tqdm(
request.iter_content(block_size),
total=math.ceil(total_size // block_size),
unit="KB",
desc=f"Loading {repo}"):
wrote += len(data)
out.write(data)
if total_size != 0 and wrote != total_size:
raise Exception("Can't load plugin. Aborting")
def load_plugin_from_github(plugin_link):
url_path = plugin_link.split(":")
local_path = ""
if len(url_path) > 1:
local_path = url_path[1]
url = url_path[0]
if os.path.exists(f"plugins/loaded/{url}/{local_path}"):
print(f"Found cached plugin {url}")
return f"plugins/loaded/{url}/{local_path}"
create_folder("tmp")
try:
download_repo(url)
except Exception:
shutil.rmtree("tmp")
raise
plugin_zip = zipfile.ZipFile("tmp/plugin.zip")
print("[ OK ] Download Plugin.")
if local_path == "":
plugin_zip.extractall(f"plugins/loaded/{url}")
shutil.rmtree("tmp")
return f"plugins/loaded/{url}"
plugin_zip.extractall("tmp")
load_location = f"plugins/loaded/{url}/{local_path}"
recursive_copy(f"tmp/{url.split('/')[-1]}-master/{local_path}",
load_location, True)
shutil.rmtree("tmp")
return load_location
def copy_and_link(src: str, pluginname: str, dst: str, links: List[str]):
if os.path.exists(src):
recursive_copy(src, f"generated/{dst}/{pluginname}")
for element in os.listdir(src):
if element not in links:
links.append(f"/{dst}/{pluginname}/{element}")
print(f"[ OK ] Linking {pluginname}/{element}")
def register_plugin(directory: str, pluginname: str):
if not os.path.exists(f"{directory}/{pluginname}/main.py"):
raise Exception("No main.py entry was found in plugin.")
import_path = directory.replace("/", ".")
mark.registerExtensions([f"{import_path}.{pluginname}.main:Plugin"], {})
global scripts
script_dir = f"{directory}/{pluginname}/scripts"
copy_and_link(script_dir, pluginname, "js", scripts)
print("[ OK ] Linking scripts")
global styles
style_dir = f"{directory}/{pluginname}/styles"
copy_and_link(style_dir, pluginname, "css", styles)
print("[ OK ] Linking styles")
print(f"[ OK ] Registring {pluginname}")
def load_plugins(md: str):
"""load_plugins
Function to load all imported plugins from page.
And return page text without "@import".
:param md:
:type md: str
"""
global scripts
scripts = []
global styles
styles = []
new_lines = []
for line in md.splitlines():
if line.strip().startswith("@import"):
plugin = line.replace("@import", "").split()
if not plugin:
raise Exception("Empty plugin nothing to load.")
if plugin[0].lower() == "from":
if not plugin[1:]:
raise Exception("Empty plugin. Nothing to load.")
url = plugin[1]
plugin_path = load_plugin_from_github(url)
plugin_dir = "/".join(plugin_path.split("/")[:-1])
register_plugin(plugin_dir, plugin_path.split("/")[-1])
else:
extension = plugin[0]
register_plugin("plugins", extension)
else:
new_lines.append(line)
return "\n".join(new_lines)
def generate_page(filename: str):
"""generate_page
Generate page from markdown to html
and place it in "generated/pages/".
:param filename:
:type filename: str
"""
global scripts
global styles
print('{:-^100}'.format(f'Generating {filename}'))
with open(filename, 'r') as page:
text = page.read()
text = load_plugins(text)
content = mark.convert(text)
print(f"[ OK ] Converting {filename}")
mark.reset()
name = filename.replace(".md", "").split("/")[-1]
with open(f"generated/pages/{name}.html", 'w') as out:
template = jinja2.Template(open("templates/page.html").read())
rendered = template.render(
page_title=name,
content=content,
scripts=scripts,
styles=styles)
out.write(rendered)
print(f"[ OK ] Generating page for {name}")
def generate_book():
print("Parsing pages")
for file in os.listdir("book"):
if os.path.isfile(f"book/{file}"):
generate_page(f"book/{file}")
print('{:-^100}'.format(f'Starting server'))
def init_app() -> Flask:
"""init_app
Initialize scribes app.
Create file structure,
load and generate all pages,
return Flask object.
:rtype: Flask
"""
app = Flask(__name__)
create_structure()
generate_book()
return app
app = init_app()
@app.route('/js/<path:path>')
def find_js(path):
return send_from_directory(f"generated/js", path)
@app.route('/css/<path:path>')
def find_css(path):
return send_from_directory(f"generated/css", path)
@app.route('/page/<path:path>')
def find_page(path):
return send_from_directory(f"generated/pages", path)
@app.route("/")
def hello():
return "Table of contents."
class FileWatcher(FileSystemEventHandler):
def on_modified(self, event):
if event.src_path.endswith(".md"):
try:
generate_page(event.src_path)
except Exception as ex:
print(
f"Cannot render page {event.src_path}\n\nReason: {str(ex)}"
)
def start_listen(directory: str):
event_handler = FileWatcher()
observer = Observer()
observer.schedule(event_handler, path=directory, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
t = threading.Thread(
target=start_listen, name="File watcher", args=("book/", ))
t.daemon = True
t.start()
app.run()