Use jinja for error page.
This commit is contained in:
parent
0b15790f3d
commit
a6ae129595
@ -30,7 +30,7 @@ from PyQt5.QtWebKitWidgets import QWebPage
|
|||||||
|
|
||||||
from qutebrowser.config import config
|
from qutebrowser.config import config
|
||||||
from qutebrowser.network import networkmanager
|
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):
|
class BrowserPage(QWebPage):
|
||||||
@ -118,7 +118,7 @@ class BrowserPage(QWebPage):
|
|||||||
log.webview.debug("Error domain: {}, error code: {}".format(
|
log.webview.debug("Error domain: {}, error code: {}".format(
|
||||||
info.domain, info.error))
|
info.domain, info.error))
|
||||||
title = "Error loading page: {}".format(urlstr)
|
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='')
|
title=title, url=urlstr, error=info.errorString, icon='')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -7,16 +7,16 @@ Based on html/error.html from dwb
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>{title}</title>
|
<title>{{ title }}</title>
|
||||||
<!--<link rel='icon' type='image/png' href="{icon}">-->
|
<!--<link rel='icon' type='image/png' href="{{ icon }}">-->
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
body {{
|
body {
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}}
|
}
|
||||||
|
|
||||||
#errorContainer {{
|
#errorContainer {
|
||||||
background: #fff;
|
background: #fff;
|
||||||
min-width: 35em;
|
min-width: 35em;
|
||||||
max-width: 35em;
|
max-width: 35em;
|
||||||
@ -26,25 +26,25 @@ Based on html/error.html from dwb
|
|||||||
padding: 10px;
|
padding: 10px;
|
||||||
border: 2px solid #eee;
|
border: 2px solid #eee;
|
||||||
-webkit-border-radius: 5px;
|
-webkit-border-radius: 5px;
|
||||||
}}
|
}
|
||||||
|
|
||||||
#errorTitleText {{
|
#errorTitleText {
|
||||||
font-size: 118%;
|
font-size: 118%;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}}
|
}
|
||||||
|
|
||||||
#errorMessageText {{
|
#errorMessageText {
|
||||||
font-size: 80%;
|
font-size: 80%;
|
||||||
}}
|
}
|
||||||
</style>
|
</style>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function tryagain()
|
function tryagain()
|
||||||
{{
|
{
|
||||||
location.reload();
|
location.reload();
|
||||||
}}
|
}
|
||||||
function searchFor(uri) {{
|
function searchFor(uri) {
|
||||||
location.href = uri;
|
location.href = uri;
|
||||||
}}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@ -53,8 +53,8 @@ Based on html/error.html from dwb
|
|||||||
<p id="errorTitleText">Unable to load page</p>
|
<p id="errorTitleText">Unable to load page</p>
|
||||||
</div>
|
</div>
|
||||||
<div id="errorMessage">
|
<div id="errorMessage">
|
||||||
<p>Problem occurred while loading the URL {url}</p>
|
<p>Problem occurred while loading the URL {{ url }}</p>
|
||||||
<p id="errorMessageText">{error}</p>
|
<p id="errorMessageText">{{ error }}</p>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
58
qutebrowser/utils/jinja.py
Normal file
58
qutebrowser/utils/jinja.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||||
|
|
||||||
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
||||||
|
#
|
||||||
|
# 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/>.
|
||||||
|
|
||||||
|
"""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)
|
Loading…
Reference in New Issue
Block a user