"""General page functionality.""" import datetime import logging import os import re from flask import Blueprint, Markup, abort from flask import current_app as app from flask import redirect, request from tzlocal import get_localzone from incorporealcms.lib import get_meta_str, init_md, render logger = logging.getLogger(__name__) bp = Blueprint('pages', __name__, url_prefix='/') @bp.route('/', defaults={'path': 'index'}) @bp.route('/') def display_page(path): """Get the file contents of the requested path and render the file.""" try: resolved_path = request_path_to_instance_resource_path(path) logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path) except PermissionError: abort(400) except IsADirectoryError: return redirect(f'{path}/', code=301) except FileNotFoundError: abort(404) try: with app.open_instance_resource(resolved_path, 'r') as entry_file: mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone()) entry = entry_file.read() except OSError: logger.error("resolved path '%s' could not be opened!", resolved_path) abort(500) else: md = init_md() content = Markup(md.convert(entry)) 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) return render('base.html', title=page_title, description=get_meta_str(md, 'description'), image=get_meta_str(md, 'image'), base_url=request.base_url, content=content, navs=parent_navs, mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z')) def request_path_to_instance_resource_path(path): """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/') resolved_path = os.path.realpath(os.path.join(base_dir, path)) logger.debug("base_dir: %s, constructed resolved_path: %s", base_dir, resolved_path) # 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 # if this is a file-like requset 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 # derive the proper markdown file depending on if this is a dir or file if os.path.isdir(resolved_path): absolute_resource = os.path.join(resolved_path, 'index.md') else: absolute_resource = f'{resolved_path}.md' logger.info("final path = '%s' for request '%s'", absolute_resource, path) # does the final file actually exist? if not os.path.exists(absolute_resource): logger.warning("requested final path '%s' does not exist!", absolute_resource) raise FileNotFoundError # we checked that the file exists via absolute path, but now we need to give the path relative to instance dir return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', '') 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))) def generate_parent_navs(path): """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 return [(app.config['TITLE_SUFFIX'], '/')] else: 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) # for issues regarding parser reuse (see lib.init_md) we reinitialize the parser here md = init_md() # read the resource 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 return generate_parent_navs(parent_resource_path) + [(page_name, request_path)]