Begin porting code from NJR

This commit is contained in:
2025-01-02 20:34:22 +00:00
parent ccdc106ef8
commit 8cb13a4bca
27 changed files with 623 additions and 1 deletions

3
src/.gitattributes vendored
View File

@@ -1,3 +0,0 @@
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text

11
src/common.py Normal file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/python3
import frontmatter
from glob import glob
from os import path
from main import app
from datetime import datetime
from bs4 import BeautifulSoup
from markdown import markdown

12
src/main.py Normal file
View File

@@ -0,0 +1,12 @@
#!/usr/bin/python3
from flask import Flask, render_template
application = Flask(__name__)
app = application
import projects
import common
# Load the homepage.

109
src/projects.py Normal file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/python3
from main import application as app
import common
from os import path
import json
from flask import render_template, Response, send_from_directory
from markdown import markdown
md_directory = path.join(path.realpath(path.dirname(__file__)), path.normpath('projects/'))
@app.context_processor
def processor():
def get_excerpt(post):
html = markdown(post.content)
post_soup = BeautifulSoup(html, 'html.parser')
all_text = ' '.join([x.get_text() for x in post_soup.findAll('p')])
return ' '.join(all_text.split()[:200])
return dict(get_excerpt=get_excerpt)
@app.template_filter('human_date')
def human_date(iso_date: str) -> str:
try:
return datetime.fromisoformat(str(iso_date)).strftime('%A %d %B %Y')
except ValueError:
return iso_date
@app.template_filter('to_html')
def to_html(content):
return markdown(content)
def get_all_posts(directory: str) -> list:
abs_paths = [path.join(directory, x) for x in glob(f'{directory}/*.md')]
return [frontmatter.load(x) for x in abs_paths]
def get_by_meta_key(directory: str, key: str, value: str):
return [x for x in get_all_posts(directory) if x.get(key) == value]
@app.route('/')
def index():
return "Hello, World!"
@app.route('/error/<code>')
def error(code):
error_definitions = {
400: 'Bad Request',
403: 'Forbidden',
404: 'Page Not Found',
418: 'I\'m a Teapot',
500: 'Internal Server Error',
503: 'Service Temporarily Unavailable',
505: 'HTTP Version Not Supported'
}
error_desc = {
400: 'Sorry, we didn\'t understand your request.',
403: 'Sorry, you aren\'t allowed to view this page.',
404: 'Sorry, that page doesn\'t exist.',
418: 'I can\'t brew coffee as I am, in fact, a teapot.',
500: 'Something went wrong on our end.',
503: 'Our website is experiencing some issues and will be back shortly.',
505: 'Your browser tried to use a HTTP version we don\'t support. Check it is up to date.'
}
errorText = f'''
<div id='error'>
<h2>{code}: {error_definitions.get(int(code))}</h2>
<p>{error_desc.get(int(code))}</p>
<a href='/'>Click here to return to our homepage</a>
</div>
'''
return render_template('error.html', post=errorText)
@app.route('/<category>/')
def category(category):
with open(path.join(md_directory, 'categories.json')) as categories_file:
categories = json.load(categories_file)
the_category = next((x for x in categories if x.get('id') == category), None)
if the_category is None:
return Response(status=404)
articles_to_return = sorted(
common.get_by_meta_key(
md_directory, 'category', category),
key=lambda d: d.metadata.get('date'),
reverse=True
)
return render_template('projects.html', articles=articles_to_return,
title=the_category['title'],
description=the_category['long_description'],
pageName=f'{the_category["title"]} -')
@app.route('/<category>/<article>')
def article(category, article):
articles = [x for x in common.get_by_meta_key(md_directory, 'id', article) if x.metadata.get('category') == category]
if len(articles) == 0:
return Response(status=404)
if len(articles) > 1:
return Response(status=500)
the_article = articles[0]
return render_template('article.html', post=markdown(the_article.content), metadata=the_article.metadata,
pageName=f'{the_article.metadata["title"]} - {the_article.metadata["author"]} -')
@app.route('/image/<image>')
def image(image):
return send_from_directory(path.join(md_directory, 'images'), image)

6
src/projects.wsgi Normal file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/python3
import sys
sys.path.append('/var/www/jc')
from main import application

5
src/requirements.txt Normal file
View File

@@ -0,0 +1,5 @@
flask>=2.2.3
flask-markdown>=0.3
markdown>=3.4.1
beautifulsoup4>=4.11.1
python-frontmatter>=1.1.0

View File

@@ -0,0 +1,2 @@
{% include 'header.html' %}
{% include 'footer.html' %}

5
src/templates/error.html Normal file
View File

@@ -0,0 +1,5 @@
{% include 'header.html' %}
<section id="post">
{{ post | safe }}
</section>
{% include 'footer.html' %}

View File

@@ -0,0 +1,5 @@
<footer>
<p>&copy 2024 Jake Charman. This site uses cookies.</p>
</footer>
</body>
</html>

19
src/templates/header.html Normal file
View File

@@ -0,0 +1,19 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jake Charman</title>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400..900&family=Tourney:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital,wght@0,100..900;1,100..900&family=Orbitron:wght@400..900&family=Tourney:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style/mobile.css" />
<link rel="stylesheet" href="style/desktop.css" />
<link rel="stylesheet" href="fonts/fontawesome/css/all.min.css" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
</head>
<body>
<header>
<div id="logo">
<h1>Jake Charman</h1>
</div>
</header>

View File

@@ -0,0 +1,2 @@
{% include 'header.html' %}
{% include 'footer.html' %}