-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
217 lines (169 loc) · 6.34 KB
/
Copy pathapp.py
File metadata and controls
217 lines (169 loc) · 6.34 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
from flask import Flask, render_template, abort
from utils.markdown_parser import MarkdownParser
from utils.yaml_loader import YAMLLoader
import os
import glob
from datetime import datetime
app = Flask(__name__)
# Initialize parsers
markdown_parser = MarkdownParser()
yaml_loader = YAMLLoader()
def get_image_path(image_name, image_type="blog"):
"""
Get the correct path for an image, checking multiple formats including WebP
Args:
image_name: filename from frontmatter (e.g., "image.jpg" or "image.webp")
image_type: "blog" or "event"
Returns:
The actual filename that exists, or the original if not found
"""
if not image_name:
return None
base_path = f"static/images/{image_type}"
# Create directory if it doesn't exist
os.makedirs(base_path, exist_ok=True)
# If the image_name already has an extension, check if it exists
if "." in image_name:
full_path = os.path.join(base_path, image_name)
if os.path.exists(full_path):
return image_name
# Get base name without extension
base_name = os.path.splitext(image_name)[0]
# Supported image formats (prioritize WebP for better compression)
supported_formats = [".webp", ".jpg", ".jpeg", ".png", ".gif"]
# Check each format
for ext in supported_formats:
test_filename = f"{base_name}{ext}"
full_path = os.path.join(base_path, test_filename)
if os.path.exists(full_path):
return test_filename
# If no file found, return original name (will show as 404 but won't break the site)
return image_name
@app.route("/")
def index():
"""Homepage with modern tech infrastructure design"""
# Get latest blog posts and events for homepage
latest_blogs = markdown_parser.get_latest_posts("content/blog", limit=3)
upcoming_events = markdown_parser.get_upcoming_events("content/event", limit=3)
latest_events = markdown_parser.get_latest_posts("content/event", limit=3)
# Load sponsor data
sponsor_data = yaml_loader.load_yaml("content/sponsor.yaml")
sponsors = sponsor_data.get("sponsors", []) if sponsor_data else []
# Filter only active sponsors
active_sponsors = [sponsor for sponsor in sponsors if sponsor.get("active", True)]
gallery_api_url = "https://devops-jogja-calendar.vercel.app/gallery"
return render_template(
"index.html",
latest_blogs=latest_blogs,
upcoming_events=upcoming_events,
latest_events=latest_events,
sponsors=active_sponsors,
gallery_api_url=gallery_api_url,
current_page="index",
)
@app.route("/blog")
def blog_index():
"""Blog listing page"""
blog_posts = markdown_parser.get_all_posts("content/blog")
return render_template(
"blog/index.html", posts=blog_posts, current_page="blog_index"
)
@app.route("/blog/<slug>")
def blog_post(slug):
"""Individual blog post"""
post = markdown_parser.get_post_by_slug("content/blog", slug)
if not post:
abort(404)
return render_template("blog/post.html", post=post, current_page="blog_post")
@app.route("/event")
def event_index():
"""Event listing page"""
events = markdown_parser.get_all_posts("content/event")
return render_template(
"event/index.html", events=events, current_page="event_index"
)
@app.route("/event/<slug>")
def event_detail(slug):
"""Individual event detail"""
event = markdown_parser.get_post_by_slug("content/event", slug)
if not event:
abort(404)
return render_template("event/event.html", event=event, current_page="event_detail")
@app.route("/organizer")
def organizer():
"""Organizer profiles page"""
organizer_data = yaml_loader.load_yaml("content/organizer.yaml")
return render_template(
"organizer.html", data=organizer_data, current_page="organizer"
)
@app.route("/about")
def about():
"""About page"""
about_data = yaml_loader.load_yaml("content/about.yaml")
return render_template("about.html", data=about_data, current_page="about")
@app.route("/gallery")
def gallery():
"""Gallery page"""
gallery_api_url = "https://devops-jogja-calendar.vercel.app/gallery"
return render_template(
"gallery.html",
gallery_api_url=gallery_api_url,
current_page="gallery"
)
@app.route("/schedule")
def schedule():
"""Schedule page with client-side fetching"""
# Pass API URL to template for client-side fetching
# Use public URL if available, otherwise fallback to local default
api_url = "https://devops-jogja-calendar.vercel.app/events"
return render_template(
"schedule.html",
api_url=api_url,
current_page="schedule"
)
@app.errorhandler(404)
def not_found(error):
return render_template("404.html"), 404
@app.errorhandler(500)
def internal_error(error):
return render_template("500.html"), 500
# Template filters
@app.template_filter("date")
def date_filter(value, format="%Y"):
"""Custom date filter for Jinja2"""
if value == "now":
return datetime.now().strftime(format)
elif isinstance(value, str):
try:
date_obj = datetime.strptime(value, "%Y-%m-%d")
return date_obj.strftime(format)
except ValueError:
return value
return value
@app.template_filter("date_format")
def date_format(date_string, format="%B %d, %Y"):
"""Format date string"""
if isinstance(date_string, str):
try:
date_obj = datetime.strptime(date_string, "%Y-%m-%d")
return date_obj.strftime(format)
except ValueError:
return date_string
return date_string
@app.template_filter("excerpt")
def excerpt_filter(text, length=150):
"""Create excerpt from text"""
if len(text) <= length:
return text
return text[:length].rsplit(" ", 1)[0] + "..."
@app.template_filter("get_image")
def get_image_filter(image_name, image_type="blog"):
"""Template filter to get the correct image path with WebP support"""
return get_image_path(image_name, image_type)
if __name__ == "__main__":
# Create content directories if they don't exist
os.makedirs("content/blog", exist_ok=True)
os.makedirs("content/event", exist_ok=True)
os.makedirs("static/organizer", exist_ok=True)
os.makedirs("static/images", exist_ok=True)
app.run(debug=True, host="0.0.0.0", port=3008)