feat: Cleanup duplicate script code
All checks were successful
Build Docker / BuildImage (push) Successful in 59s

This commit is contained in:
2025-10-16 17:37:48 +11:00
parent d7d4dbed8b
commit b69c7f381b
7 changed files with 97 additions and 136 deletions

View File

@@ -3,7 +3,7 @@ from flask import Blueprint, render_template, request, jsonify
import markdown
from bs4 import BeautifulSoup
import re
from tools import isCurl, getClientIP
from tools import isCurl, getClientIP, getHandshakeScript
blog_bp = Blueprint('blog', __name__)
@@ -83,7 +83,7 @@ def fix_numbered_lists(html):
return str(soup)
def render_home(handshake_scripts=None):
def render_home(handshake_scripts: str | None = None):
# Get a list of pages
blog_pages = list_page_files()
# Create a html list of pages
@@ -107,26 +107,15 @@ def render_home(handshake_scripts=None):
@blog_bp.route("/")
def index():
if not isCurl(request):
global handshake_scripts
return render_home(handshake_scripts=getHandshakeScript(request.host))
# If localhost, don't load handshake
if (
request.host == "localhost:5000"
or request.host == "127.0.0.1:5000"
or os.getenv("dev") == "true"
or request.host == "test.nathan.woodburn.au"
):
handshake_scripts = ""
return render_home(handshake_scripts)
# Get a list of pages
blog_pages = list_page_files()
# Create a html list of pages
blog_pages = [
{"name":page.replace("_", " "),"url":f"/blog/{page}", "download": f"/blog/{page}.md"} for page in blog_pages
{"name": page.replace("_", " "), "url": f"/blog/{page}", "download": f"/blog/{page}.md"} for page in blog_pages
]
# Render the template
return jsonify({
"status": 200,
@@ -136,22 +125,11 @@ def index():
}), 200
@blog_bp.route("/<path:path>")
def path(path):
if not isCurl(request):
global handshake_scripts
# If localhost, don't load handshake
if (
request.host == "localhost:5000"
or request.host == "127.0.0.1:5000"
or os.getenv("dev") == "true"
or request.host == "test.nathan.woodburn.au"
):
handshake_scripts = ""
return render_page(path, handshake_scripts=getHandshakeScript(request.host))
return render_page(path, handshake_scripts)
# Convert md to html
if not os.path.exists(f"data/blog/{path}.md"):
return render_template("404.html"), 404
@@ -169,6 +147,7 @@ def path(path):
"download": f"/blog/{path}.md"
}), 200
@blog_bp.route("/<path:path>.md")
def path_md(path):
if not os.path.exists(f"data/blog/{path}.md"):
@@ -176,6 +155,6 @@ def path_md(path):
with open(f"data/blog/{path}.md", "r") as f:
content = f.read()
# Return the raw markdown file
return content, 200, {'Content-Type': 'text/plain; charset=utf-8'}

View File

@@ -1,10 +1,12 @@
from flask import Blueprint, render_template, make_response, request, jsonify
import datetime
import os
from tools import getHandshakeScript
# Create blueprint
now_bp = Blueprint('now', __name__)
def list_page_files():
now_pages = os.listdir("templates/now")
now_pages = [
@@ -13,24 +15,28 @@ def list_page_files():
now_pages.sort(reverse=True)
return now_pages
def list_dates():
now_pages = list_page_files()
now_dates = [page.split(".")[0] for page in now_pages]
return now_dates
def get_latest_date(formatted=False):
if formatted:
date=list_dates()[0]
date = list_dates()[0]
date = datetime.datetime.strptime(date, "%y_%m_%d")
date = date.strftime("%A, %B %d, %Y")
return date
return list_dates()[0]
def render_latest(handshake_scripts=None):
now_page = list_dates()[0]
return render(now_page,handshake_scripts=handshake_scripts)
return render(now_page, handshake_scripts=handshake_scripts)
def render(date,handshake_scripts=None):
def render(date, handshake_scripts=None):
# If the date is not available, render the latest page
if date is None:
return render_latest(handshake_scripts=handshake_scripts)
@@ -40,60 +46,24 @@ def render(date,handshake_scripts=None):
if date not in list_dates():
return render_template("404.html"), 404
date_formatted = datetime.datetime.strptime(date, "%y_%m_%d")
date_formatted = date_formatted.strftime("%A, %B %d, %Y")
return render_template(f"now/{date}.html",DATE=date_formatted,handshake_scripts=handshake_scripts)
return render_template(f"now/{date}.html", DATE=date_formatted, handshake_scripts=handshake_scripts)
@now_bp.route("/")
def index():
handshake_scripts = ''
# If localhost, don't load handshake
if (
request.host == "localhost:5000"
or request.host == "127.0.0.1:5000"
or os.getenv("dev") == "true"
or request.host == "test.nathan.woodburn.au"
):
handshake_scripts = ""
else:
handshake_scripts = '<script src="https://nathan.woodburn/handshake.js" domain="nathan.woodburn" async></script><script src="https://nathan.woodburn/https.js" async></script>'
return render_latest(handshake_scripts)
return render_latest(handshake_scripts=getHandshakeScript(request.host))
@now_bp.route("/<path:path>")
def path(path):
handshake_scripts = ''
# If localhost, don't load handshake
if (
request.host == "localhost:5000"
or request.host == "127.0.0.1:5000"
or os.getenv("dev") == "true"
or request.host == "test.nathan.woodburn.au"
):
handshake_scripts = ""
else:
handshake_scripts = '<script src="https://nathan.woodburn/handshake.js" domain="nathan.woodburn" async></script><script src="https://nathan.woodburn/https.js" async></script>'
return render(path, handshake_scripts)
return render(path, handshake_scripts=getHandshakeScript(request.host))
@now_bp.route("/old")
@now_bp.route("/old/")
def old():
handshake_scripts = ''
# If localhost, don't load handshake
if (
request.host == "localhost:5000"
or request.host == "127.0.0.1:5000"
or os.getenv("dev") == "true"
or request.host == "test.nathan.woodburn.au"
):
handshake_scripts = ""
else:
handshake_scripts = '<script src="https://nathan.woodburn/handshake.js" domain="nathan.woodburn" async></script><script src="https://nathan.woodburn/https.js" async></script>'
now_dates = list_dates()[1:]
html = '<ul class="list-group">'
html += f'<a style="text-decoration:none;" href="/now"><li class="list-group-item" style="background-color:#000000;color:#ffffff;">{get_latest_date(True)}</li></a>'
@@ -106,7 +76,7 @@ def old():
html += "</ul>"
return render_template(
"now/old.html", handshake_scripts=handshake_scripts, now_pages=html
"now/old.html", handshake_scripts=getHandshakeScript(request.host), now_pages=html
)