71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""General page functionality."""
|
|
import datetime
|
|
import logging
|
|
import os
|
|
|
|
import markdown
|
|
from flask import Blueprint, Markup, abort, current_app as app, render_template
|
|
from tzlocal import get_localzone
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
bp = Blueprint('pages', __name__, url_prefix='/')
|
|
md = markdown.Markdown(extensions=['meta', 'tables'])
|
|
|
|
|
|
@bp.route('/', defaults={'path': 'index'})
|
|
@bp.route('/<path:path>')
|
|
def display_page(path):
|
|
"""Get the file contents of the requested path and render the file."""
|
|
resolved_path = resolve_page_file(path)
|
|
logger.debug("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)
|
|
parent_navs = generate_parent_navs(path)
|
|
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
|
|
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, navs=parent_navs,
|
|
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
|
|
|
|
|
|
def resolve_page_file(path):
|
|
"""Manipulate the request path to find appropriate page file.
|
|
|
|
* 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'pages/{path}.md'
|
|
return path
|
|
|
|
|
|
def generate_parent_navs(path):
|
|
"""Create a series of paths/links to navigate up from the given path."""
|
|
# derive additional path/location stuff based on path
|
|
resolved_path = resolve_page_file(path)
|
|
parent_dir = os.path.dirname(resolved_path)
|
|
parent_path = '/'.join(path[:-1].split('/')[:-1]) + '/'
|
|
|
|
logger.debug("path: '%s'; parent path: '%s'; resolved path: '%s'; parent dir: '%s'",
|
|
path, parent_path, resolved_path, parent_dir)
|
|
|
|
if path in ('index', '/'):
|
|
return [(app.config['TITLE_SUFFIX'], '/')]
|
|
else:
|
|
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
|
entry = entry_file.read()
|
|
_ = Markup(md.convert(entry))
|
|
page_name = " ".join(md.Meta.get('title')) if md.Meta.get('title') else f'/{path}'
|
|
return generate_parent_navs(parent_path) + [(page_name, f'/{path}')]
|