-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (48 loc) · 1.68 KB
/
Copy pathapp.py
File metadata and controls
63 lines (48 loc) · 1.68 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
"""
Author: Jerry Li, Claude
Date: 2025-03-22
Version: 0.1
"""
from flask import Flask, render_template, abort
from flask_frozen import Freezer
from jinja2 import ChoiceLoader, FileSystemLoader
import markdown
import os
app = Flask(__name__, template_folder="assets/templates")
app.jinja_loader = ChoiceLoader([
app.jinja_loader,
FileSystemLoader("common/templates")
])
# Serve static files from the 'assets' folder
app.static_folder = "assets"
@app.route("/")
def index():
return render_template("index.html")
@app.route("/posts/<post_name>.html")
def serve_post(post_name):
md_path = os.path.join("posts", f"{post_name}.md")
html_path = os.path.join("posts", f"{post_name}.html")
if os.path.exists(md_path):
with open(md_path, encoding="utf8") as file:
content = markdown.markdown(file.read(), extensions=[
"codehilite", "fenced_code"])
return render_template("post.html", content=content)
elif os.path.exists(html_path):
with open(html_path, encoding="utf8") as file:
content = file.read()
return render_template("post.html", content=content)
else:
abort(404) # Return a 404 error if the post doesn't exist
freezer = Freezer(app)
# Register generator for dynamic posts
@freezer.register_generator
def serve_post():
# List all files in the 'posts' directory
posts_dir = "posts"
for file_name in os.listdir(posts_dir):
if file_name.endswith(".html"):
yield {"post_name": file_name[:-5]}
elif file_name.endswith(".md"):
yield {"post_name": file_name[:-3]}
if __name__ == "__main__":
freezer.freeze()