-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
243 lines (197 loc) · 7.76 KB
/
Copy pathbuild.py
File metadata and controls
243 lines (197 loc) · 7.76 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
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
import argparse
import http.server
import os
import shutil
import socketserver
import sqlite3
import markdown
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(ROOT_DIR, "data.db")
BUILD_DIR = os.path.join(ROOT_DIR, "build")
SOURCE_DIR = os.path.join(ROOT_DIR, "src")
STATIC_DIRS = ["css", "img"]
def load_layout(dev_mode=False):
with open(os.path.join(SOURCE_DIR, "index.html"), "r") as f:
layout = f.read()
return layout
def execute_query(query, params=()):
"""Helper function to execute a query and fetch results from the SQLite database."""
try:
conn = sqlite3.connect(DB_PATH)
with conn:
cur = conn.cursor()
cur.execute(query, params)
results = cur.fetchall()
return results
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return []
finally:
if conn:
conn.close()
def get_pages():
return execute_query("SELECT slug, content FROM pages")
def get_projects():
return execute_query(
"SELECT id, title, slug, description, image FROM projects ORDER BY id DESC"
)
def get_updates(project_id):
return execute_query(
"""SELECT created_at, content, GROUP_CONCAT(attachments.url) as attachment_urls
FROM updates
LEFT JOIN attachments ON updates.id = attachments.update_id
WHERE project_id = ?
GROUP BY updates.id
ORDER BY created_at ASC""",
(project_id,),
)
def get_latest_updates(count):
return execute_query(
"""SELECT updates.created_at, updates.content, GROUP_CONCAT(attachments.url) as attachment_urls
FROM updates
LEFT JOIN attachments ON updates.id = attachments.update_id
GROUP BY updates.id
ORDER BY updates.created_at DESC
LIMIT ?""",
(count,),
)
def render_markdown(content, layout):
html = markdown.markdown(content, extensions=["fenced_code", "codehilite"])
return layout.format(page=html)
def save_html(slug, html, subdirectory=""):
if subdirectory:
os.makedirs(os.path.join(BUILD_DIR, subdirectory), exist_ok=True)
html_path = os.path.join(BUILD_DIR, subdirectory, f"{slug}.html")
with open(html_path, "w") as f:
f.write(html)
def render_update(created_at, content, attachment_urls, use_thumbnails=False):
"""Render the markdown for a single update with attachments."""
md = f"## {created_at}\n\n{content}\n\n"
if attachment_urls:
for url in attachment_urls.split(","):
if use_thumbnails:
md += f'<a href="{url}" target="_blank"><img class="attachment-thumb" src="{url}" /></a>\n\n'
else:
md += f'<a href="{url}" target="_blank"><img src="{url}" /></a>\n\n'
return md
def build_website(layout):
shutil.rmtree(BUILD_DIR, ignore_errors=True)
# copy static files
for static_dir in STATIC_DIRS:
shutil.copytree(
os.path.join(SOURCE_DIR, static_dir), os.path.join(BUILD_DIR, static_dir)
)
# build "static" pages
pages = get_pages()
for slug, content in pages:
print(f"rendering {slug}")
html = render_markdown(content, layout)
save_html(slug, html)
# build project pages
projects = get_projects()
project_links = []
for project_id, title, slug, description, image in projects:
print(f"rendering {slug}")
md = f"# {title}\n\n{description}\n\n" + "".join(
render_update(created_at, content, attachment_urls, use_thumbnails=False)
for created_at, content, attachment_urls in get_updates(project_id)
)
html = render_markdown(md, layout)
save_html(slug, html, subdirectory="projects")
# project index page
for project_id, title, slug, description, image in projects:
project_links.append(
f"""## [{title}]({slug}.html)\n\n<a href="{slug}.html"><img class="project-cover" src="{image}"/></a>\n"""
)
project_list = "".join(project_links)
project_index_md = f"# Projects\n\n{project_list}"
project_index_html = render_markdown(project_index_md, layout)
save_html("index", project_index_html, subdirectory="projects")
# build log page
updates = get_latest_updates(20)
log_md = "# Build log\n\n"
for created_at, content, attachment_urls in updates:
log_md += render_update(created_at, content, attachment_urls, use_thumbnails=True)
log_html = render_markdown(log_md, layout)
save_html("index", log_html, subdirectory="build-log")
shutil.rmtree(BUILD_DIR, ignore_errors=True)
# copy static files
for static_dir in STATIC_DIRS:
shutil.copytree(
os.path.join(SOURCE_DIR, static_dir), os.path.join(BUILD_DIR, static_dir)
)
# build "static" pages
pages = get_pages()
for slug, content in pages:
print(f"rendering {slug}")
html = render_markdown(content, layout)
save_html(slug, html)
# build project pages
projects = get_projects()
project_links = []
for project_id, title, slug, description, image in projects:
print(f"rendering {slug}")
md = f"# {title}\n\n{description}\n\n" + "".join(
render_update(created_at, content, attachment_urls)
for created_at, content, attachment_urls in get_updates(project_id)
)
html = render_markdown(md, layout)
save_html(slug, html, subdirectory="projects")
# project index page
for project_id, title, slug, description, image in projects:
project_links.append(
f"""## [{title}]({slug}.html)\n\n<a href="{slug}.html"><img class="project-cover" src="{image}"/></a>\n"""
)
project_list = "".join(project_links)
project_index_md = f"# Projects\n\n{project_list}"
project_index_html = render_markdown(project_index_md, layout)
save_html("index", project_index_html, subdirectory="projects")
# build log page
updates = get_latest_updates(20)
log_md = "# Build log\n\n"
for created_at, content, attachment_urls in updates:
log_md += render_update(created_at, content, attachment_urls)
log_html = render_markdown(log_md, layout)
save_html("index", log_html, subdirectory="build-log")
class RewriteUrlsHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Rewrite the URL for .html files.
if self.path.endswith("/"):
self.path += "index.html"
elif not any(
self.path.endswith(x)
for x in [".html", ".js", ".css", ".png", ".jpg", ".jpeg", ".gif"]
):
self.path += ".html"
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def serve_website(port=8000):
os.chdir(BUILD_DIR)
handler = RewriteUrlsHTTPRequestHandler
with socketserver.TCPServer(("", port), handler, bind_and_activate=False) as httpd:
httpd.allow_reuse_address = True
httpd.server_bind()
httpd.server_activate()
print(f"Serving on http://localhost:{port}/ from {BUILD_DIR}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down the server...")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Build and serve a static website with live reloading."
)
parser.add_argument("--dev", action="store_true", help="Enable development mode.")
parser.add_argument(
"--serve", action="store_true", help="Serve the website after building."
)
parser.add_argument(
"--port",
type=int,
default=9000,
help="Port to serve the website on (default: 9000).",
)
args = parser.parse_args()
layout = load_layout(args.dev)
build_website(layout)
if args.serve:
serve_website(args.port)