2020-03-07 14:06:57 -06:00
|
|
|
"""General page functionality."""
|
2020-03-07 15:39:12 -06:00
|
|
|
import datetime
|
2020-03-07 10:08:23 -06:00
|
|
|
import logging
|
2020-03-07 15:39:12 -06:00
|
|
|
import os
|
2021-02-20 19:09:59 -06:00
|
|
|
import re
|
2020-03-07 10:08:23 -06:00
|
|
|
|
2020-06-19 19:54:01 -05:00
|
|
|
from flask import Blueprint, Markup, abort
|
|
|
|
from flask import current_app as app
|
2021-04-14 20:45:50 -05:00
|
|
|
from flask import redirect, request, send_from_directory
|
2020-03-07 10:08:23 -06:00
|
|
|
|
2021-02-20 23:36:03 -06:00
|
|
|
from incorporealcms.lib import get_meta_str, init_md, render
|
2021-01-17 23:02:14 -06:00
|
|
|
|
2020-03-07 10:08:23 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-03-06 18:08:24 -06:00
|
|
|
|
2020-03-07 14:06:57 -06:00
|
|
|
bp = Blueprint('pages', __name__, url_prefix='/')
|
2020-03-06 18:08:24 -06:00
|
|
|
|
|
|
|
|
2020-03-07 08:37:38 -06:00
|
|
|
@bp.route('/', defaults={'path': 'index'})
|
2020-03-06 18:08:24 -06:00
|
|
|
@bp.route('/<path:path>')
|
2020-03-07 14:06:57 -06:00
|
|
|
def display_page(path):
|
2020-03-07 10:08:23 -06:00
|
|
|
"""Get the file contents of the requested path and render the file."""
|
2021-02-20 17:41:57 -06:00
|
|
|
try:
|
2021-04-15 20:36:30 -05:00
|
|
|
resolved_path, render_type = request_path_to_instance_resource_path(path)
|
2021-04-15 21:43:29 -05:00
|
|
|
logger.debug("received request for path '%s', resolved to '%s', type '%s'",
|
|
|
|
path, resolved_path, render_type)
|
2021-02-20 17:41:57 -06:00
|
|
|
except PermissionError:
|
|
|
|
abort(400)
|
|
|
|
except IsADirectoryError:
|
2021-04-17 15:06:39 -05:00
|
|
|
return redirect(f'/{path}/', code=301)
|
2021-02-20 17:41:57 -06:00
|
|
|
except FileNotFoundError:
|
|
|
|
abort(404)
|
2020-06-19 19:58:12 -05:00
|
|
|
|
2021-04-15 20:36:30 -05:00
|
|
|
if render_type == 'file':
|
2021-04-14 20:45:50 -05:00
|
|
|
return send_from_directory(app.instance_path, resolved_path)
|
2021-04-15 21:43:29 -05:00
|
|
|
elif render_type == 'symlink':
|
2021-04-17 10:30:01 -05:00
|
|
|
logger.debug("attempting to redirect path '%s' to reverse of resource '%s'", path, resolved_path)
|
|
|
|
redirect_path = f'/{instance_resource_path_to_request_path(resolved_path)}'
|
|
|
|
logger.debug("redirect path: '%s'", redirect_path)
|
|
|
|
return redirect(redirect_path, code=301)
|
2021-04-15 20:36:30 -05:00
|
|
|
elif render_type == 'markdown':
|
2021-06-06 22:24:35 -05:00
|
|
|
logger.debug("treating path '%s' as markdown '%s'", path, resolved_path)
|
|
|
|
return handle_markdown_file_path(resolved_path)
|
2020-03-07 10:08:23 -06:00
|
|
|
else:
|
2021-04-15 20:36:30 -05:00
|
|
|
logger.exception("unsupported render_type '%s'!?", render_type)
|
|
|
|
abort(500)
|
2020-10-30 00:19:19 -05:00
|
|
|
|
|
|
|
|
2021-06-06 22:24:35 -05:00
|
|
|
def handle_markdown_file_path(resolved_path):
|
|
|
|
"""Given a location on disk, attempt to open it and render the markdown within."""
|
|
|
|
try:
|
2021-06-24 09:43:00 -05:00
|
|
|
logger.debug("opening resolved path '%s'", resolved_path)
|
2021-06-06 22:24:35 -05:00
|
|
|
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
2022-03-24 22:11:14 -05:00
|
|
|
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), tz=datetime.timezone.utc)
|
2021-06-06 22:24:35 -05:00
|
|
|
entry = entry_file.read()
|
2021-06-24 09:43:00 -05:00
|
|
|
logger.debug("resolved path '%s' read", resolved_path)
|
2021-06-06 22:24:35 -05:00
|
|
|
except OSError:
|
|
|
|
logger.exception("resolved path '%s' could not be opened!", resolved_path)
|
|
|
|
abort(500)
|
|
|
|
else:
|
2021-06-24 09:43:00 -05:00
|
|
|
try:
|
|
|
|
md = init_md()
|
|
|
|
content = Markup(md.convert(entry))
|
2021-06-24 11:37:57 -05:00
|
|
|
except ValueError:
|
|
|
|
logger.exception("error parsing/rendering markdown!")
|
|
|
|
abort(500)
|
2021-06-24 09:43:00 -05:00
|
|
|
except TypeError:
|
|
|
|
logger.exception("error loading/rendering markdown!")
|
|
|
|
abort(500)
|
|
|
|
|
2021-06-06 22:24:35 -05:00
|
|
|
logger.debug("file metadata: %s", md.Meta)
|
|
|
|
|
|
|
|
parent_navs = generate_parent_navs(resolved_path)
|
|
|
|
|
|
|
|
page_name = (get_meta_str(md, 'title') if md.Meta.get('title') else
|
|
|
|
f'/{instance_resource_path_to_request_path(resolved_path)}')
|
|
|
|
page_title = f'{page_name} - {app.config["TITLE_SUFFIX"]}' if page_name else app.config['TITLE_SUFFIX']
|
|
|
|
logger.debug("title (potentially derived): %s", page_title)
|
|
|
|
|
2022-09-16 13:31:43 -05:00
|
|
|
extra_footer = get_meta_str(md, 'footer') if md.Meta.get('footer') else None
|
|
|
|
|
2021-06-06 22:24:35 -05:00
|
|
|
template = get_meta_str(md, 'template') if md.Meta.get('template') else 'base.html'
|
|
|
|
|
|
|
|
# check if this has a HTTP redirect
|
|
|
|
redirect_url = get_meta_str(md, 'redirect') if md.Meta.get('redirect') else None
|
|
|
|
if redirect_url:
|
|
|
|
logger.debug("redirecting via meta tag to '%s'", redirect_url)
|
|
|
|
return redirect(redirect_url, code=301)
|
|
|
|
|
|
|
|
return render(template, title=page_title, description=get_meta_str(md, 'description'),
|
|
|
|
image=get_meta_str(md, 'image'), base_url=request.base_url, content=content,
|
2022-09-16 13:31:43 -05:00
|
|
|
navs=parent_navs, mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'),
|
|
|
|
extra_footer=extra_footer)
|
2021-06-06 22:24:35 -05:00
|
|
|
|
|
|
|
|
2021-02-20 17:53:32 -06:00
|
|
|
def request_path_to_instance_resource_path(path):
|
2021-02-20 17:41:57 -06:00
|
|
|
"""Turn a request URL path to the full page path.
|
|
|
|
|
|
|
|
flask.Flask.open_instance_resource will open a file like /etc/hosts if you tell it to,
|
|
|
|
which sucks, so we do a lot of work here to make sure we have a valid request to
|
|
|
|
something inside the pages dir.
|
|
|
|
"""
|
|
|
|
# check if the path is allowed
|
|
|
|
base_dir = os.path.realpath(f'{app.instance_path}/pages/')
|
2021-04-17 10:30:01 -05:00
|
|
|
verbatim_path = os.path.abspath(os.path.join(base_dir, path))
|
|
|
|
resolved_path = os.path.realpath(verbatim_path)
|
|
|
|
logger.debug("base_dir '%s', constructed resolved_path '%s' for path '%s'", base_dir, resolved_path, path)
|
2021-02-20 17:41:57 -06:00
|
|
|
|
|
|
|
# bail if the requested real path isn't inside the base directory
|
|
|
|
if base_dir != os.path.commonpath((base_dir, resolved_path)):
|
|
|
|
logger.warning("client tried to request a path '%s' outside of the base_dir!", path)
|
|
|
|
raise PermissionError
|
|
|
|
|
2021-04-17 10:30:01 -05:00
|
|
|
# see if we have a real file or if we should infer markdown rendering
|
|
|
|
if os.path.exists(resolved_path):
|
|
|
|
# if this is a file-like request but actually a directory, redirect the user
|
|
|
|
if os.path.isdir(resolved_path) and not path.endswith('/'):
|
|
|
|
logger.info("client requested a path '%s' that is actually a directory", path)
|
|
|
|
raise IsADirectoryError
|
|
|
|
|
|
|
|
# if the requested path contains a symlink, redirect the user
|
|
|
|
if verbatim_path != resolved_path:
|
|
|
|
logger.info("client requested a path '%s' that is actually a symlink to file '%s'", path, resolved_path)
|
|
|
|
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'symlink'
|
2021-04-15 21:43:29 -05:00
|
|
|
|
2021-04-17 10:30:01 -05:00
|
|
|
# derive the proper markdown or actual file depending on if this is a dir or file
|
|
|
|
if os.path.isdir(resolved_path):
|
|
|
|
resolved_path = os.path.join(resolved_path, 'index.md')
|
|
|
|
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'markdown'
|
2021-02-20 17:41:57 -06:00
|
|
|
|
2021-04-14 20:45:50 -05:00
|
|
|
logger.info("final DIRECT path = '%s' for request '%s'", resolved_path, path)
|
2021-04-15 20:36:30 -05:00
|
|
|
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'file'
|
2021-04-17 10:30:01 -05:00
|
|
|
|
|
|
|
# if we're here, this isn't direct file access, so try markdown inference
|
|
|
|
verbatim_path = os.path.abspath(os.path.join(base_dir, f'{path}.md'))
|
|
|
|
resolved_path = os.path.realpath(verbatim_path)
|
2021-02-20 17:41:57 -06:00
|
|
|
|
|
|
|
# does the final file actually exist?
|
2021-04-17 10:30:01 -05:00
|
|
|
if not os.path.exists(resolved_path):
|
|
|
|
logger.warning("requested final path '%s' does not exist!", resolved_path)
|
2021-02-20 17:41:57 -06:00
|
|
|
raise FileNotFoundError
|
|
|
|
|
2021-04-17 10:30:01 -05:00
|
|
|
# check for symlinks
|
|
|
|
if verbatim_path != resolved_path:
|
|
|
|
logger.info("client requested a path '%s' that is actually a symlink to file '%s'", path, resolved_path)
|
|
|
|
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'symlink'
|
|
|
|
|
|
|
|
logger.info("final path = '%s' for request '%s'", resolved_path, path)
|
2021-02-20 17:41:57 -06:00
|
|
|
# we checked that the file exists via absolute path, but now we need to give the path relative to instance dir
|
2021-04-17 10:30:01 -05:00
|
|
|
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'markdown'
|
2021-02-20 17:41:57 -06:00
|
|
|
|
|
|
|
|
2021-02-20 19:09:59 -06:00
|
|
|
def instance_resource_path_to_request_path(path):
|
|
|
|
"""Reverse a (presumed to exist) disk path to the canonical path that would show up in a Flask route.
|
|
|
|
|
|
|
|
This does not include the leading /, so aside from the root index case, this should be
|
|
|
|
bidirectional.
|
|
|
|
"""
|
|
|
|
return re.sub(r'^pages/', '', re.sub(r'.md$', '', re.sub(r'index.md$', '', path)))
|
|
|
|
|
|
|
|
|
2020-03-15 20:23:14 -05:00
|
|
|
def generate_parent_navs(path):
|
2021-02-20 21:47:39 -06:00
|
|
|
"""Create a series of paths/links to navigate up from the given resource path."""
|
|
|
|
if path == 'pages/index.md':
|
|
|
|
# bail and return the title suffix (generally the domain name) as a terminal case
|
2020-03-15 20:23:14 -05:00
|
|
|
return [(app.config['TITLE_SUFFIX'], '/')]
|
|
|
|
else:
|
2021-02-20 21:47:39 -06:00
|
|
|
if path.endswith('index.md'):
|
|
|
|
# index case: one dirname for foo/bar/index.md -> foo/bar, one for foo/bar -> foo
|
|
|
|
parent_resource_dir = os.path.dirname(os.path.dirname(path))
|
|
|
|
else:
|
|
|
|
# usual case: foo/buh.md -> foo
|
|
|
|
parent_resource_dir = os.path.dirname(path)
|
|
|
|
|
|
|
|
# generate the request path (i.e. what the link will be) for this path, and
|
|
|
|
# also the resource path of this parent (which is always a dir, so always index.md)
|
|
|
|
request_path = f'/{instance_resource_path_to_request_path(path)}'
|
|
|
|
parent_resource_path = os.path.join(parent_resource_dir, 'index.md')
|
|
|
|
|
|
|
|
logger.debug("resource path: '%s'; request path: '%s'; parent resource path: '%s'", path,
|
|
|
|
request_path, parent_resource_path)
|
|
|
|
|
2021-02-11 18:17:26 -06:00
|
|
|
# for issues regarding parser reuse (see lib.init_md) we reinitialize the parser here
|
|
|
|
md = init_md()
|
2021-02-20 21:47:39 -06:00
|
|
|
|
|
|
|
# read the resource
|
2021-04-14 21:35:14 -05:00
|
|
|
try:
|
|
|
|
with app.open_instance_resource(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 request_path_to_breadcrumb_display(request_path))
|
|
|
|
return generate_parent_navs(parent_resource_path) + [(page_name, request_path)]
|
|
|
|
except FileNotFoundError:
|
|
|
|
return generate_parent_navs(parent_resource_path) + [(request_path, request_path)]
|
2021-02-27 00:30:32 -06:00
|
|
|
|
|
|
|
|
|
|
|
def request_path_to_breadcrumb_display(path):
|
|
|
|
"""Given a request path, e.g. "/foo/bar/baz/", turn it into breadcrumby text "baz"."""
|
|
|
|
undired = path.rstrip('/')
|
|
|
|
leaf = undired[undired.rfind('/'):]
|
|
|
|
return leaf.strip('/')
|