this has a really basic template and whatnot at the moment, so styling/etc isn't done, but this is maybe the last major piece before I could actually see pushing this onto the site
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""Journal functionality."""
|
|
import logging
|
|
|
|
import markdown
|
|
from flask import Blueprint, Markup, abort, current_app as app, render_template
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
bp = Blueprint('journal', __name__, url_prefix='/')
|
|
md = markdown.Markdown(extensions=['meta'])
|
|
|
|
|
|
@bp.route('/', defaults={'path': 'index'})
|
|
@bp.route('/<path:path>')
|
|
def display_journal_entry(path):
|
|
"""Get the file contents of the requested path and render the file."""
|
|
resolved_path = journal_file_resolver(path)
|
|
logger.info("received request for path '%s', resolved to '%s'", path, resolved_path)
|
|
try:
|
|
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
|
logger.debug("file '%s' found", resolved_path)
|
|
entry = entry_file.read()
|
|
except FileNotFoundError:
|
|
logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path)
|
|
abort(404)
|
|
else:
|
|
content = Markup(md.convert(entry))
|
|
logger.debug("file metadata: %s", md.Meta)
|
|
title = " ".join(md.Meta.get('title')) if md.Meta.get('title') else ""
|
|
return render_template('base.html', title=title, content=content)
|
|
|
|
|
|
def journal_file_resolver(path):
|
|
"""Manipulate the request path to find appropriate journal files.
|
|
|
|
* convert dir requests to index files
|
|
|
|
Worth noting, Flask already does stuff like convert '/foo/../../../bar' to
|
|
'/bar', so we don't need to take care around file access here.
|
|
"""
|
|
if path.endswith('/'):
|
|
path = f'{path}index'
|
|
path = f'journal/{path}.md'
|
|
return path
|