From a6ae1295959ac32dab5002a3d9289f68dc672968 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 28 Aug 2014 17:48:43 +0200 Subject: [PATCH] Use jinja for error page. --- qutebrowser/browser/webpage.py | 4 +-- qutebrowser/html/error.html | 32 +++++++++---------- qutebrowser/utils/jinja.py | 58 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 qutebrowser/utils/jinja.py diff --git a/qutebrowser/browser/webpage.py b/qutebrowser/browser/webpage.py index 02fd97c67..9b1cd2869 100644 --- a/qutebrowser/browser/webpage.py +++ b/qutebrowser/browser/webpage.py @@ -30,7 +30,7 @@ from PyQt5.QtWebKitWidgets import QWebPage from qutebrowser.config import config from qutebrowser.network import networkmanager -from qutebrowser.utils import message, usertypes, log, http, utils, qtutils +from qutebrowser.utils import message, usertypes, log, http, jinja, qtutils class BrowserPage(QWebPage): @@ -118,7 +118,7 @@ class BrowserPage(QWebPage): log.webview.debug("Error domain: {}, error code: {}".format( info.domain, info.error)) title = "Error loading page: {}".format(urlstr) - errpage.content = utils.read_file('html/error.html').format( + errpage.content = jinja.env.get_template('error.html').render( title=title, url=urlstr, error=info.errorString, icon='') return True diff --git a/qutebrowser/html/error.html b/qutebrowser/html/error.html index 3c6b7bf1b..fc05a54d3 100644 --- a/qutebrowser/html/error.html +++ b/qutebrowser/html/error.html @@ -7,16 +7,16 @@ Based on html/error.html from dwb - {title} - + {{ title }} + @@ -53,8 +53,8 @@ Based on html/error.html from dwb

Unable to load page

-

Problem occurred while loading the URL {url}

-

{error}

+

Problem occurred while loading the URL {{ url }}

+

{{ error }}

diff --git a/qutebrowser/utils/jinja.py b/qutebrowser/utils/jinja.py new file mode 100644 index 000000000..08efde2c9 --- /dev/null +++ b/qutebrowser/utils/jinja.py @@ -0,0 +1,58 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014 Florian Bruhin (The Compiler) +# +# 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 . + +"""Utilities related to jinja2. """ + +import os.path + +import jinja2 + +from qutebrowser.utils import utils + + +class Loader(jinja2.BaseLoader): + + """Jinja loader which uses utils.read_file to load templates.""" + + def __init__(self, subdir): + self.subdir = subdir + + def get_source(self, env, template): + path = os.path.join(self.subdir, template) + try: + source = utils.read_file(path) + except FileNotFoundError: + 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): + """Turns autoescape on/off based on the filetype. + + 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') + + +env = jinja2.Environment(loader=Loader('html'), autoescape=_guess_autoescape)