Skip to content

Commit

Permalink
Updated Nov 23 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiangyuan Li authored and Jiangyuan Li committed Nov 23, 2024
1 parent 67dcff3 commit 3ecc4c2
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
# Step 4: Run flask freeze
- name: Freeze Flask App
run: |
python freeze.py # Generate static files in the './build' directory
python app.py # Generate static files in the './build' directory
# Step 5: Deploy GitHub Pages
- name: Upload artifact for Pages
Expand Down
61 changes: 25 additions & 36 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
from flask import Flask, render_template, abort
import os
from flask import Flask, render_template
from flask_frozen import Freezer
from markupsafe import Markup

app = Flask(__name__)
freezer = Freezer(app)

# Base route for the homepage


@app.route('/')
def index():
return render_template('index.html')


@app.route("/blog")
def blog():
posts = []
for filename in os.listdir("posts"):
if filename.endswith(".html"):
with open(os.path.join("posts", filename)) as f:
content = f.read()
title = os.path.splitext(filename)[0].replace("-", " ").title()
posts.append({"title": title, "content": Markup(content)})
return render_template("blog.html", posts=posts)


@app.route("/blog/<post_slug>.html")
def post(post_slug):
post_file = f"posts/{post_slug}.html"
if os.path.exists(post_file):
with open(post_file) as f:
content = Markup(f.read())
title = post_slug.replace("-", " ").title()
return render_template("post.html", title=title, content=content)
else:
return "Post not found", 404


if __name__ == '__main__':
freezer.freeze()

if __name__ == '__main__':
app.run(debug=True)
post_files = os.listdir('posts/')
posts = [post.replace('.html', '')
for post in post_files if post.endswith('.html')]
print(f"Posts: {posts}") # Debugging line
return render_template('index.html', posts=posts)

# Route for individual posts


@app.route('/posts/<post_name>')
def post(post_name):
post_path = f'posts/{post_name}.html'
try:
with open(post_path, 'r') as file:
content = file.read()
return render_template('post.html', title=post_name.capitalize(), content=content)
except FileNotFoundError:
abort(404)


if __name__ == "__main__":
app.run(debug=True, port=5000)
8 changes: 1 addition & 7 deletions freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@
freezer = Freezer(app)

if __name__ == "__main__":
freezer.freeze()

@freezer.register_generator
def post():
posts = ["my-first-post", "learning-flask"]
for post_slug in posts:
yield {"post_slug": post_slug + ".html"}
freezer.freeze()
1 change: 1 addition & 0 deletions posts/first-post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p id="dev-cal-2">大家在高中一定趁大好时光多开心呀</p>
2 changes: 0 additions & 2 deletions posts/my-first-post.html

This file was deleted.

16 changes: 0 additions & 16 deletions templates/blog.html

This file was deleted.

6 changes: 5 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{% block content %}
<h2>Jerry's blog</h2>
<p id="dev-cal-2">This is the content specific to the homepage.</p>
<p id="dev-cal-2">This is the content specific to the homepage.</p>
<ul>
{% for post in posts %}
<li><a href="{{ url_for('post', post_name=post) }}">{{ post | capitalize }}</a></li>
{% endfor %}
</ul>
<p id="dev-cal-2">This is the content specific to the homepage.</p>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<div id="attic-items">
<div class="b-item-2">publications</div>
<div class="b-item">
<a href="{{ url_for('blog') }}">Blog</a>
<a href="javascript:;">None</a>
</div>
</div>
</header>
Expand Down
2 changes: 1 addition & 1 deletion templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ <h1>{{ title }}</h1>
{{ content | safe }}
</div>

<a href="{{ url_for('blog') }}">Back to Blog</a>
<a id="dev-cal-2" href="{{ url_for('index') }}"><i class="fas fa-arrow-left"></i>&emsp;Back to Blog</a>
{% endblock %}

0 comments on commit 3ecc4c2

Please sign in to comment.