You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.0 KiB
Python
81 lines
2.0 KiB
Python
2 years ago
|
import os
|
||
|
from flask import Flask, render_template, url_for, send_from_directory
|
||
|
import frontmatter
|
||
|
import markdown
|
||
|
from prefix import PrefixMiddleware
|
||
|
from dotenv import load_dotenv
|
||
|
from time import strftime, localtime
|
||
|
import random
|
||
|
import glob
|
||
|
|
||
|
|
||
|
|
||
|
load_dotenv()
|
||
|
prefix = os.environ.get('URL_PREFIX', '')
|
||
|
port = os.environ.get('PORT', 3000)
|
||
|
debug = os.environ.get('DEBUG', False)
|
||
|
|
||
|
# ---
|
||
|
# Functions
|
||
|
# ---
|
||
|
|
||
|
def list_files(folder, remove_ext=False):
|
||
|
''' Read all the functions in a folder '''
|
||
|
names = []
|
||
|
for entry in os.scandir(folder):
|
||
|
# add to the list only proper files
|
||
|
if entry.is_file(follow_symlinks=False):
|
||
|
# remove the extension from the filename
|
||
|
n = entry.name
|
||
|
if remove_ext:
|
||
|
n = os.path.splitext(entry.name)[0]
|
||
|
names.append(n)
|
||
|
return names
|
||
|
|
||
|
def txt_list():
|
||
|
''' Generate list of writings '''
|
||
|
files = sorted(filter(os.path.isfile, glob.glob('txt/[!.]*.md')), key=lambda file: os.path.getmtime(file), reverse=True)
|
||
|
writings = []
|
||
|
for file in files:
|
||
|
with open(file) as f:
|
||
|
meta, content = frontmatter.parse(f.read())
|
||
|
meta['slug'] = os.path.splitext(os.path.basename(file))[0]
|
||
|
meta['last_edit'] = strftime('%d.%m.%Y', localtime(os.path.getmtime(file)))
|
||
|
writings.append(meta)
|
||
|
|
||
|
return writings
|
||
|
|
||
|
|
||
|
# ---
|
||
|
# Create Flask App
|
||
|
# ---
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=prefix)
|
||
|
|
||
|
|
||
|
# ---
|
||
|
# Routes
|
||
|
# ---
|
||
|
|
||
|
@app.route('/')
|
||
|
def home():
|
||
|
return render_template('home.html', writings=txt_list())
|
||
|
|
||
|
@app.route('/txt/<slug>')
|
||
|
def txt(slug):
|
||
|
try:
|
||
|
with open(f'txt/{slug}.md') as f:
|
||
|
meta, content = frontmatter.parse(f.read())
|
||
|
text = {**meta}
|
||
|
text['content']=markdown.markdown(content)
|
||
|
return render_template('text.html', text=text)
|
||
|
except FileNotFoundError:
|
||
|
return 'File not found!'
|
||
|
|
||
|
# ---
|
||
|
# Run the app
|
||
|
# ---
|
||
|
|
||
|
app.run(port=port, debug=debug)
|