From e098cf574b32d4c192e8551434646affda48b29c Mon Sep 17 00:00:00 2001 From: holysoles Date: Tue, 23 Apr 2024 03:12:38 +0000 Subject: [PATCH 1/5] generate date data, init sidebar --- app.py | 24 ++++++++-- templates/blog.html | 110 ++++++++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 50 deletions(-) diff --git a/app.py b/app.py index 2ac8bee..7da4557 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ from werkzeug.middleware.proxy_fix import ProxyFix from flask_minify import minify import yaml +import re from os import listdir from os.path import isfile, join, splitext @@ -16,14 +17,29 @@ @app.route("/") def home(): - yaml_path = './blog/posts/' + posts_dir = './blog/posts/' code_snippet_path = './blog/code_snippets/' - dir_files = [join(yaml_path, file) for file in listdir(yaml_path) if isfile(join(yaml_path, file))] + dir_files = listdir(posts_dir) yaml_files = [file for file in dir_files if splitext(file)[1] == '.yaml'] yaml_files.sort(reverse=True) post_array = [] + timeline = {} for yaml_file in yaml_files: - with open(yaml_file, 'r') as file: + # collect datetime info + datetime_list = re.split('_|\.', yaml_file) + file_year = datetime_list[0] + file_month = datetime_list[1] + file_day = datetime_list[2] + if file_year in timeline: + if file_month in timeline[file_year]: + timeline[file_year][file_month].append(file_day) + else: + timeline[file_year][file_month] = {file_day: } + else: + timeline[file_year] = {file_month: [file_day]} + + # load in post text + with open(join(posts_dir, yaml_file), 'r') as file: post_data = yaml.safe_load(file) # parse and preload code snippet files @@ -35,7 +51,7 @@ def home(): body['code'] = code_snippet post_array.append(post_data) - return render_template('blog.html', post_array=post_array) + return render_template('blog.html', post_array=post_array, date_dict=timeline) @app.route("/contact") def contact(): diff --git a/templates/blog.html b/templates/blog.html index 8508bc7..0a9af75 100644 --- a/templates/blog.html +++ b/templates/blog.html @@ -1,53 +1,71 @@ {% extends 'layout.html' %} {% block content %} -
- - {% for post in post_array %} - - + + {% endfor %} +
-
- {% if post['title'] %} -

- {{ post['title'] }} - {{ post['date'] }} -

- {% endif %} - {% for paragraph in post['body'] %} -
- {% if paragraph['section_title'] %} -

- {{ paragraph['section_title'] }} -

- {% endif %} - {% if paragraph['text'] %} -

- {{ paragraph['text'] }} -

- {% endif %} - {% if paragraph['code'] %} -
-
- {% for line in paragraph['code'].splitlines() %} - {{ loop.index }} +
+ +
+ + {% for post in post_array %} + + - - {% endfor %} -
+
+ {% if post['title'] %} +

+ {{ post['title'] }} - {{ post['date'] }} +

+ {% endif %} + {% for paragraph in post['body'] %} +
+ {% if paragraph['section_title'] %} +

+ {{ paragraph['section_title'] }} +

+ {% endif %} + {% if paragraph['text'] %} +

+ {{ paragraph['text'] }} +

+ {% endif %} + {% if paragraph['code'] %} +
+
+ {% for line in paragraph['code'].splitlines() %} + {{ loop.index }} + {% endfor %} +
+ + {% for line in paragraph['code'].splitlines() %} + +
{{line}}
+
+ {% endfor %} +
- - {% for line in paragraph['code'].splitlines() %} - -
{{line}}
-
- {% endfor %} -
+ {% endif %} + {% if paragraph['image'] %} + + {% endif %}
- {% endif %} - {% if paragraph['image'] %} - - {% endif %} - - {% endfor %} -
-
+ {% endfor %} +
+
+
{% endblock %} \ No newline at end of file From 978242fa5ca2168d8422761262a4090fae9eec84 Mon Sep 17 00:00:00 2001 From: holysoles Date: Sun, 28 Apr 2024 20:03:10 +0000 Subject: [PATCH 2/5] implement sidebar nav --- app.py | 46 ++++++++++++++++++++++++++++++++------------- templates/blog.html | 9 ++++++++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index 7da4557..b3a5abd 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template +from flask import Flask, request, render_template from werkzeug.middleware.proxy_fix import ProxyFix from flask_minify import minify import yaml @@ -15,16 +15,16 @@ app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1 ) -@app.route("/") -def home(): - posts_dir = './blog/posts/' - code_snippet_path = './blog/code_snippets/' +posts_dir = './blog/posts/' +code_snippet_path = './blog/code_snippets/' + +def get_posts(): dir_files = listdir(posts_dir) yaml_files = [file for file in dir_files if splitext(file)[1] == '.yaml'] yaml_files.sort(reverse=True) - post_array = [] timeline = {} for yaml_file in yaml_files: + link_to_post = "/post?date" + yaml_file # collect datetime info datetime_list = re.split('_|\.', yaml_file) file_year = datetime_list[0] @@ -32,16 +32,20 @@ def home(): file_day = datetime_list[2] if file_year in timeline: if file_month in timeline[file_year]: - timeline[file_year][file_month].append(file_day) + timeline[file_year][file_month][file_day] = yaml_file else: - timeline[file_year][file_month] = {file_day: } + timeline[file_year][file_month] = {file_day: yaml_file} else: - timeline[file_year] = {file_month: [file_day]} + timeline[file_year] = {file_month: {file_day: yaml_file}} + return yaml_files, timeline +def load_post_data(yaml_files): + post_array = [] + for yaml_file in yaml_files: # load in post text with open(join(posts_dir, yaml_file), 'r') as file: post_data = yaml.safe_load(file) - + # parse and preload code snippet files if post_data.get('body'): for body in post_data['body']: @@ -49,10 +53,26 @@ def home(): with open(join(code_snippet_path, body['code']), 'r') as file: code_snippet = file.read() body['code'] = code_snippet - - post_array.append(post_data) + + post_array.append(post_data) + return post_array + +@app.route("/", methods=['GET']) +def home(): + yaml_files, timeline = get_posts() + post_array = load_post_data(yaml_files) return render_template('blog.html', post_array=post_array, date_dict=timeline) -@app.route("/contact") +@app.route("/post", methods=['GET']) +def post(): + req_post = request.args.get('date') + if req_post: + yaml_files, timeline = get_posts() + if req_post in yaml_files: + post = load_post_data([req_post]) + return render_template('blog.html', post_array=post, date_dict=timeline) + return "Post not found", 404 + +@app.route("/contact", methods=['GET']) def contact(): return render_template('contact.html') \ No newline at end of file diff --git a/templates/blog.html b/templates/blog.html index 0a9af75..2c4b3ea 100644 --- a/templates/blog.html +++ b/templates/blog.html @@ -3,13 +3,20 @@