diff --git a/incorporealcms/lib.py b/incorporealcms/lib.py index ab2cabd..711c042 100644 --- a/incorporealcms/lib.py +++ b/incorporealcms/lib.py @@ -1,6 +1,11 @@ """Miscellaneous helper functions and whatnot.""" +import logging + import markdown from flask import current_app as app +from flask import make_response, render_template, request + +logger = logging.getLogger(__name__) def get_meta_str(md, key): @@ -19,3 +24,27 @@ def init_md(): # extensions our app depends on, like the meta extension return markdown.Markdown(extensions=app.config['MARKDOWN_EXTENSIONS'] + ['meta'], extension_configs=app.config['MARKDOWN_EXTENSION_CONFIGS']) + + +def render(template_name_or_list, **context): + """Wrap Flask's render_template. + + * Determine the proper site theme to use in the template and provide it. + """ + PAGE_STYLES = { + 'dark': 'css/dark.css', + 'light': 'css/light.css', + } + + selected_style = request.args.get('style', None) + if selected_style: + user_style = selected_style + else: + user_style = request.cookies.get('user-style') + logger.debug("user style cookie: %s", user_style) + context['user_style'] = PAGE_STYLES.get(user_style, PAGE_STYLES.get(app.config['DEFAULT_PAGE_STYLE'])) + + resp = make_response(render_template(template_name_or_list, **context)) + if selected_style: + resp.set_cookie('user-style', selected_style) + return resp diff --git a/incorporealcms/pages.py b/incorporealcms/pages.py index 27c6182..5c570a3 100644 --- a/incorporealcms/pages.py +++ b/incorporealcms/pages.py @@ -9,7 +9,7 @@ from flask import current_app as app from flask import make_response, redirect, render_template, request from tzlocal import get_localzone -from incorporealcms.lib import get_meta_str, init_md +from incorporealcms.lib import get_meta_str, init_md, render logger = logging.getLogger(__name__) @@ -54,30 +54,6 @@ def display_page(path): mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z')) -def render(template_name_or_list, **context): - """Wrap Flask's render_template. - - * Determine the proper site theme to use in the template and provide it. - """ - PAGE_STYLES = { - 'dark': 'css/dark.css', - 'light': 'css/light.css', - } - - selected_style = request.args.get('style', None) - if selected_style: - user_style = selected_style - else: - user_style = request.cookies.get('user-style') - logger.debug("user style cookie: %s", user_style) - context['user_style'] = PAGE_STYLES.get(user_style, PAGE_STYLES.get(app.config['DEFAULT_PAGE_STYLE'])) - - resp = make_response(render_template(template_name_or_list, **context)) - if selected_style: - resp.set_cookie('user-style', selected_style) - return resp - - def request_path_to_instance_resource_path(path): """Turn a request URL path to the full page path.