-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpysen.py
302 lines (239 loc) · 9.29 KB
/
pysen.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
"""
pysen
Simple static site generator
"""
import argparse
import datetime
import os
from tzlocal import get_localzone
from jinja2 import Template, Environment, FileSystemLoader
import shutil
import markdown
import frontmatter
import http.server
import socketserver
from functools import partial
__license__ = "MIT"
site_name = "verse"
html_theme = "poetry" # directory name from under themes/
build_export_directory = f"public/{site_name}"
def get_local_time_with_offset():
# Get the system's local timezone
local_timezone = get_localzone()
# Get the current time in the system's local timezone
current_local_time = datetime.datetime.now(local_timezone)
# Extract the timezone offset in the format +HH:MM
offset_hours, offset_minutes = divmod(
current_local_time.utcoffset().seconds // 60, 60)
formatted_timezone_offset = f"{'+' if offset_hours >= 0 else '-'}{abs(offset_hours):02d}:{offset_minutes:02d}"
# Format the date and time with the manually included timezone offset
return current_local_time.strftime(
f'%Y-%m-%dT%H:%M:%S{formatted_timezone_offset}')
def check_file_status(file_path):
"""
Check the status of a file.
Returns:
0 if the file does not exist.
1 if the file exists.
-1 if an error occurs (e.g., cannot determine the file status).
"""
try:
if os.path.exists(file_path):
return 1 # File exists
else:
return 0 # File does not exist
except Exception as e:
print(f"Error checking file status: {e}")
return -1 # Error occurred
def create_post(file_path):
# extract filename sans extension
filename = os.path.splitext(os.path.basename(file_path))[0]
title = filename.replace('-', ' ').title()
content = f"""---
title: "{title}"
date: {get_local_time_with_offset()}
draft: true
---
"""
with open(file_path, "w") as file:
file.write(content)
print(f"File '{file_path}' created successfully.")
def remove_directory(directory_path):
try:
# Remove the directory and its contents
shutil.rmtree(directory_path)
print(
f"Directory '{directory_path}' and its contents removed successfully.")
except FileNotFoundError:
print(f"Directory '{directory_path}' not found.")
except PermissionError:
print(f"Permission error: Unable to remove directory '{directory_path}'.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def remove_everything_inside_directory(directory_path):
try:
# Iterate over all files and subdirectories inside the directory
for filename in os.listdir(directory_path):
file_path = os.path.join(directory_path, filename)
# Remove files
if os.path.isfile(file_path):
os.unlink(file_path)
# Remove directories (recursively)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
print(f"Successfully removed everything inside '{directory_path}'.")
except Exception as e:
print(f"Error: {e}")
def create_directory(directory_path):
try:
# Create a directory at the specified path
os.makedirs(directory_path)
print(f"Directory '{directory_path}' created successfully.")
except FileExistsError:
print(f"Directory '{directory_path}' already exists.")
except PermissionError:
print(f"Permission error: Unable to create directory '{directory_path}'.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def copy_directory(source_path, destination_path):
try:
# Copy the entire directory from source_path to destination_path
shutil.copytree(source_path, destination_path)
print(
f"Directory '{source_path}' copied to '{destination_path}' successfully.")
except shutil.Error as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def copy_directory_content(source_path, destination_path):
try:
# Ensure destination directory exists
if not os.path.exists(destination_path):
os.makedirs(destination_path)
# Iterate through items in the source directory
for item in os.listdir(source_path):
source_item = os.path.join(source_path, item)
destination_item = os.path.join(destination_path, item)
# Copy file or directory
if os.path.isdir(source_item):
shutil.copytree(source_item, destination_item)
else:
shutil.copy2(source_item, destination_item)
print(f"Contents of '{source_path}' copied to '{destination_path}' successfully.")
except shutil.Error as e:
print(f"Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def load_template(html_theme, template_name):
# Load Jinja template from the 'themes' directory
template_loader = FileSystemLoader(f"themes/{html_theme}/layouts")
template_env = Environment(loader=template_loader)
return template_env.get_template(template_name)
def build_list_page(html_theme, base_template, build_export_directory, ctx):
template = load_template(html_theme, base_template)
rendered_output = template.render(ctx=ctx)
# Save the rendered output to an HTML file
with open(f"{build_export_directory}/{base_template}", "w") as web_page:
web_page.write(rendered_output)
def build_detail_page(html_theme, base_template, build_export_directory, content_file_path, ctx):
template = load_template(html_theme, base_template)
rendered_output = template.render(ctx=ctx)
post_filename = os.path.basename(content_file_path)
post_save_directory = f"{build_export_directory}/{post_filename.split('.')[0]}"
create_directory(post_save_directory)
# Save the rendered output to an HTML file
with open(f"{post_save_directory}/index.html", "w") as web_page:
web_page.write(rendered_output)
def get_post_info(file_path):
document = frontmatter.load(file_path)
front_matter = document.metadata
markdown_content = document.content
html_content = markdown.markdown(markdown_content)
ctx = {
"title": front_matter.get("title", "Untitled"),
"date": front_matter.get("date", "Undated"),
"draft": front_matter.get("draft", True),
"content": html_content,
"filename": os.path.basename(file_path).split(".")[0],
}
return ctx
def build_site():
content_directory = "content"
post_directory = f"{content_directory}/posts"
# Recreate build_export_directory directory
create_directory(build_export_directory)
remove_everything_inside_directory(build_export_directory)
# Set site values
site_ctx = {
"name": site_name,
"display_name": "Verse",
"tagline": "",
"footer": f'Copyright © 2025 <a href="https://hasithsen.pages.dev" class="text-decoration-none">hsen</a>. Powered by <a href="https://github.com/hasithsen/pysen" class="text-decoration-none">Pysen</a>.',
}
# Get post filename list from content directory
post_file_paths = [os.path.join(post_directory, f) for f in os.listdir(
post_directory) if os.path.isfile(os.path.join(post_directory, f))]
post_ctx_list = []
# Get post info for each post
for file_path in post_file_paths:
# Get post info
post_ctx = get_post_info(file_path)
# Skip drafts
if post_ctx['draft']:
continue
post_ctx_list.append(post_ctx)
# We have post info
# if post has associated asset directory, copy it to build_export_directory
directory_path = file_path.split(".")[0] + "/"
if check_file_status(directory_path):
copy_directory_content(directory_path, build_export_directory + '/' + post_ctx["filename"])
# build post page
build_detail_page(html_theme, "post.html",
build_export_directory, file_path, (site_ctx, post_ctx))
# Get about page info
file_path = f"{content_directory}/about.md"
post_ctx = get_post_info(file_path)
# Build about page
build_detail_page(html_theme, "about.html",
build_export_directory, file_path, (site_ctx, post_ctx))
# Build index page
build_list_page(html_theme, "index.html",
build_export_directory, (site_ctx, post_ctx_list))
# Copy theme assets
source_directory = f"themes/{html_theme}/assets"
destination_directory = f"{build_export_directory}/assets"
copy_directory(source_directory, destination_directory)
def serve_site():
port = 8000
directory = "public"
Handler = partial(http.server.SimpleHTTPRequestHandler, directory=directory)
with socketserver.TCPServer(("", port), Handler) as httpd:
print(
f"Serving on port {port} (http://localhost:{port}/) from the directory: {directory}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
def main():
parser = argparse.ArgumentParser(
description="pysen - Simple static site generator.")
parser.add_argument(
"action", nargs="?", choices=["new", "serve", "server"], help="Specify the action to perform.")
parser.add_argument(
"file_path", nargs="?", help="Specify the file path for new post.")
args = parser.parse_args()
if args.action == "new":
file_status = check_file_status(args.file_path)
if file_status == 0:
create_post(args.file_path)
elif file_status == 1:
print(f"The file '{args.file_path}' already exists, didn't touch.")
else:
print(f"Cannot determine the status of the file '{args.file_path}'.")
elif args.action in ("serve", "server"):
serve_site()
else:
build_site()
if __name__ == "__main__":
main()