qutebrowser/qutebrowser/utils/jinja.py

101 lines
3.0 KiB
Python
Raw Normal View History

2014-08-28 17:48:43 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2016-01-04 07:12:39 +01:00
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-08-28 17:48:43 +02:00
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2015-03-26 07:08:13 +01:00
"""Utilities related to jinja2."""
2014-08-28 17:48:43 +02:00
import os
2014-08-28 17:48:43 +02:00
import os.path
import traceback
2014-08-28 17:48:43 +02:00
import jinja2
import jinja2.exceptions
2014-08-28 17:48:43 +02:00
from qutebrowser.utils import utils, log
2014-08-28 17:48:43 +02:00
from PyQt5.QtCore import QUrl
2014-08-28 17:48:43 +02:00
class Loader(jinja2.BaseLoader):
2014-09-24 22:22:02 +02:00
"""Jinja loader which uses utils.read_file to load templates.
Attributes:
_subdir: The subdirectory to find templates in.
"""
2014-08-28 17:48:43 +02:00
def __init__(self, subdir):
self._subdir = subdir
2014-08-28 17:48:43 +02:00
2014-08-29 06:14:52 +02:00
def get_source(self, _env, template):
path = os.path.join(self._subdir, template)
2014-08-28 17:48:43 +02:00
try:
source = utils.read_file(path)
except OSError:
2014-08-28 17:48:43 +02:00
raise jinja2.TemplateNotFound(template)
# Currently we don't implement auto-reloading, so we always return True
# for up-to-date.
return source, path, lambda: True
def _guess_autoescape(template_name):
2015-03-31 20:49:29 +02:00
"""Turn auto-escape on/off based on the file type.
2014-08-28 17:48:43 +02:00
Based on http://jinja.pocoo.org/docs/dev/api/#autoescaping
"""
if template_name is None or '.' not in template_name:
return False
ext = template_name.rsplit('.', 1)[1]
return ext in ('html', 'htm', 'xml')
def resource_url(path):
2015-10-17 17:33:05 +02:00
"""Load images from a relative path (to qutebrowser).
Arguments:
path: The relative path to the image
"""
image = utils.resource_filename(path)
return QUrl.fromLocalFile(image).toString(QUrl.FullyEncoded)
def file_url(path):
"""Return a file:// url (as string) to the given local path.
Arguments:
path: The absolute path to the local file
"""
return QUrl.fromLocalFile(path).toString(QUrl.FullyEncoded)
def render(template, **kwargs):
"""Render the given template and pass the given arguments to it."""
try:
return _env.get_template(template).render(**kwargs)
except jinja2.exceptions.UndefinedError:
log.misc.exception("UndefinedError while rendering " + template)
2016-03-26 00:24:54 +01:00
err_path = os.path.join('html', 'undef_error.html')
err_template = utils.read_file(err_path)
tb = traceback.format_exc()
return err_template.format(pagename=template, traceback=tb)
_env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)
_env.globals['resource_url'] = resource_url
_env.globals['file_url'] = file_url