made flask server able to read markdown files for posts

This commit is contained in:
0x01fe 2024-03-11 14:27:53 -05:00
parent ad3a3f17ff
commit 5c4d0dfa6f
11 changed files with 155 additions and 44 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
*.woff __pycache__
*.md
!POST_TEMPLATE.md

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
# syntax=docker/dockerfile:1
FROM python:3.12.2-slim-bookworm
RUN apt-get update && apt-get upgrade -y
RUN useradd -m app
USER app
COPY . .
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install -r requirements.txt
WORKDIR ./app
CMD ["python3", "-u", "./app.py"]

25
app/post.py Normal file
View File

@ -0,0 +1,25 @@
import markdown
import datetime
class Post:
category : str
author : str
date : datetime.datetime
body : str
file : str
def __init__(self, file_path):
self.file = file_path
with open(file_path, 'r') as file:
lines = file.readlines()
self.category = lines[1].split(":")[1].strip()
self.author = lines[2].split(":")[1].strip()
date = lines[3].split(":")[1].strip()
self.date = datetime.datetime.strptime(date, "%d-%m-%Y")
self.body = markdown.markdown(''.join(lines[6:]))

View File

@ -0,0 +1,9 @@
# Metadata
category: category
author: author
date: date
# POST
## TITLE
### DATE OR SUBTITLE
POST TEXT

Binary file not shown.

View File

@ -12,7 +12,6 @@ body {
a { a {
color: black; color: black;
text-decoration: none;
} }
a:hover { a:hover {
@ -70,11 +69,15 @@ a:hover {
line-height: 30pt; line-height: 30pt;
} }
.sidebar a {
text-decoration: none;
}
.dlog { .dlog {
order: 2; order: 2;
flex: 3; flex: 3;
text-align: center; /* text-align: center; */
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -104,3 +107,7 @@ a:hover {
background-color: #C9D6DF; background-color: #C9D6DF;
} }
.post p {
text-indent: 3em;
}

31
app/templates/index.html Normal file
View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<header>
<link rel="shortcut icon" href="index_favicon.ico">
<link rel="stylesheet" href="index_style.css">
<title>0x01fe.net</title>
</header>
<body>
<div class="header">
<h1>0x01fe.net</h1>
<h4>Catchy Right‽</h4>
</div>
<div class="container">
<!-- Sidebar -->
<div class="sidebar">
<a href="./">Home</a><br>
<a href="">Media</a><br>
<a href="">Programming</a><br>
<a href="">About</a>
</div>
<!-- Main Page -->
<!-- Get it? D-Log? Like digital log? -->
<div class="dlog">
{% for post in posts %}
<div class="post">{{ post|safe }}</div>
{% endfor %}
</div>
</div>
</body>
</html>

53
app/website.py Normal file
View File

@ -0,0 +1,53 @@
import glob
import configparser
import flask
from post import Post
app = flask.Flask(__name__, static_url_path='', static_folder='static')
config = configparser.ConfigParser()
config.read("config.ini")
POSTS_FOLDER = config['POSTS']['POSTS_FOLDER']
def get_posts() -> list[Post]:
post_files= glob.glob(f'{POSTS_FOLDER}/*')
post_files.remove(f'{POSTS_FOLDER}\\POST_TEMPLATE.md')
posts: list[Post] = []
for post_file in post_files:
post = Post(post_file)
posts.append(post)
# Order Posts by Date
ordered_posts = []
for i in range(len(posts)):
most_recent = posts[0]
for p in posts:
if p.date < most_recent.date:
most_recent = p
ordered_posts.append(most_recent)
posts.remove(most_recent)
return reversed(ordered_posts)
@app.route('/')
def index():
# Get posts
posts = get_posts()
post_bodies = []
for post in posts:
post_bodies.append(post.body)
return flask.render_template('index.html', posts=post_bodies)
if __name__ == "__main__":
app.run()

5
config.ini Normal file
View File

@ -0,0 +1,5 @@
[POSTS]
posts_folder=./posts
[NETWORK]
PORT=1111

View File

@ -1,41 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<header>
<link rel="shortcut icon" href="index_favicon.ico">
<link rel="stylesheet" href="index_style.css">
<title>0x01fe.net</title>
</header>
<body>
<div class="header">
<h1>0x01fe.net</h1>
<h4>Catchy Right‽</h4>
</div>
<div class="container">
<!-- Sidebar -->
<div class="sidebar">
<a href="./index.html">Home</a><br>
<a href="">Media</a><br>
<a href="">Programming</a><br>
<a href="">About</a>
</div>
<!-- Main Page -->
<!-- Get it? D-Log? Like digital log? -->
<div class="dlog">
<div class="post">
<h2>It's Peaks, <i>Peaks of Yore</i></h1>
<h3>March 8th, 2024</h3>
<p>Excuse the awful title please, but it truly is peak. <a href="https://store.steampowered.com/app/2236070/Peaks_of_Yore/"><i>Peaks of Yore</i></a> has been a more enjoyable title than I could've ever expected. At first glance it seems like a "rage bait" game akin to the likes of <i>Getting Over It With Bennett Foddy</i> or <i>Super Meatboy</i>, but after the first few fundamental courses you realize the controls aren't fighting you, you just haven't mastered them yet.</p>
</div>
<div class="post">
<h2>Post Title</h1>
<p>foobar</p>
</div>
<div class="post">
<h2>Post Title</h1>
<p>foobar</p>
</div>
</div>
</div>
</body>
</html>

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Markdown==3.5.2
Flask==2.2.3