First steps at using jinja2 for qute:log.

This commit is contained in:
Florian Bruhin 2014-08-29 06:58:13 +02:00
parent 9da504c6a0
commit 1dce50309b
5 changed files with 49 additions and 92 deletions

View File

@ -1,5 +1,7 @@
<!DOCTYPE html>
<!-- vim: ft=html fileencoding=utf-8 sts=4 sw=4 et: -->
<!--
vim: ft=html fileencoding=utf-8 sts=4 sw=4 et:
-->
<html>
<head>

35
qutebrowser/html/log.html Normal file
View File

@ -0,0 +1,35 @@
{% extends "base.html" %}
{% block style %}
body {
background-color: black;
color: white;
font-size: 11px;
}
table {
border: 1px solid grey;
border-collapse: collapse;
}
pre {
margin: 2px;
}
th, td {
border: 1px solid grey;
padding-left: 5px;
padding-right: 5px;
}
{% endblock %}
{% block content %}
{{ super() }}
{% if content %}
<table>
{{ content | safe() }}
</table>
{% else %}
<p>Log output was disabled.</p>
{% endif %}
{% endblock %}

View File

@ -20,12 +20,9 @@
"""Handler functions for different qute:... pages.
Module attributes:
_HTML_TEMPLATE: The HTML boilerplate used to convert text into html.
pyeval_output: The output of the last :pyeval command.
"""
import html as pyhtml
from PyQt5.QtNetwork import QNetworkReply
import qutebrowser
@ -34,42 +31,9 @@ from qutebrowser.utils import version, utils, jinja
from qutebrowser.utils import log as logutils
_HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
{head}
</head>
<body>
{body}
</body>
</html>
"""
pyeval_output = ":pyeval was never called"
def _get_html(title, snippet, head=None):
"""Add HTML boilerplate to a html snippet.
Args:
title: The title the page should have.
snippet: The html snippet.
head: Additional stuff to put in <head>
Return:
HTML content as bytes.
"""
if head is None:
head = ""
html = _HTML_TEMPLATE.format(title=title, body=snippet, head=head).encode(
'UTF-8', errors='xmlcharrefreplace')
return html
class QuteSchemeHandler(schemehandler.SchemeHandler):
"""Scheme handler for qute: URLs."""
@ -136,35 +100,13 @@ class QuteHandlers:
@classmethod
def log(cls):
"""Handler for qute:log. Return HTML content as bytes."""
style = """
<style type="text/css">
body {
background-color: black;
color: white;
font-size: 11px;
}
table {
border: 1px solid grey;
border-collapse: collapse;
}
pre {
margin: 2px;
}
th, td {
border: 1px solid grey;
padding-left: 5px;
padding-right: 5px;
}
</style>
"""
if logutils.ram_handler is None:
html = "<p>Log output was disabled.</p>"
html_log = None
else:
html = logutils.ram_handler.dump_log(html=True)
return _get_html('log', html, head=style)
html_log = logutils.ram_handler.dump_log(html=True)
html = jinja.env.get_template('log.html').render(
title='log', content=html_log)
return html.encode('UTF-8', errors='xmlcharrefreplace')
@classmethod
def gpl(cls):

View File

@ -317,13 +317,12 @@ class RAMHandler(logging.Handler):
self.data.append(record)
def dump_log(self, html=False):
"""Dump the complete formatted log data as as string."""
if html:
fmt = self.html_formatter.format
lines = ['<table>']
else:
fmt = self.format
lines = []
"""Dump the complete formatted log data as as string.
FIXME: We should do all the HTML formatter via jinja2.
"""
lines = []
fmt = self.html_formatter.format if html else self.format
self.acquire()
try:
records = list(self.data)
@ -331,8 +330,6 @@ class RAMHandler(logging.Handler):
self.release()
for record in records:
lines.append(fmt(record))
if html:
lines.append('</table>')
return '\n'.join(lines)

View File

@ -48,25 +48,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/> or use
:open qute:gpl.
"""
GPL_BOILERPLATE_HTML = """
<p>
This program 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.
</p>
<p>
This program 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.
</p>
<p>
You should have received a copy of the GNU General Public License
along with this program. If not, see <a href="http://www.gnu.org/licenses/">
http://www.gnu.org/licenses/</a> or open <a href="qute:gpl">qute:gpl</a>.
"""
def _git_str():
"""Try to find out git version.