2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2014-02-07 11:01:58 +01:00
|
|
|
# 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/>.
|
|
|
|
|
2014-04-17 17:44:27 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2014-02-17 12:23:52 +01:00
|
|
|
|
2014-05-27 15:46:39 +02:00
|
|
|
import qutebrowser
|
2014-04-22 15:28:48 +02:00
|
|
|
import cgi
|
2014-02-21 07:35:53 +01:00
|
|
|
|
2014-05-25 20:26:26 +02:00
|
|
|
import qutebrowser.utils.log as log
|
2014-05-27 15:33:44 +02:00
|
|
|
import qutebrowser.utils.version as version
|
2014-06-03 14:57:57 +02:00
|
|
|
from qutebrowser.network.schemehandler import (SchemeHandler,
|
|
|
|
SpecialNetworkReply)
|
2014-05-27 15:33:44 +02:00
|
|
|
from qutebrowser.utils.misc import read_file
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
|
2014-02-19 11:04:13 +01:00
|
|
|
_HTML_TEMPLATE = """
|
2014-02-07 11:01:58 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>{title}</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{body}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2014-05-07 09:03:40 +02:00
|
|
|
pyeval_output = ":pyeval was never called"
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _get_html(title, snippet):
|
|
|
|
"""Add HTML boilerplate to a html snippet.
|
|
|
|
|
2014-02-19 10:58:32 +01:00
|
|
|
Args:
|
|
|
|
title: The title the page should have.
|
|
|
|
snippet: The html snippet.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
HTML content as bytes.
|
2014-02-07 11:01:58 +01:00
|
|
|
"""
|
2014-05-25 20:26:14 +02:00
|
|
|
return _HTML_TEMPLATE.format(title=title, body=snippet).encode(
|
|
|
|
'UTF-8', errors='xmlcharrefreplace')
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
class QuteSchemeHandler(SchemeHandler):
|
2014-02-20 23:08:27 +01:00
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Scheme handler for qute: URLs."""
|
2014-02-20 23:08:27 +01:00
|
|
|
|
2014-04-22 17:53:27 +02:00
|
|
|
def createRequest(self, _op, request, _outgoing_data):
|
2014-02-20 23:08:27 +01:00
|
|
|
"""Create a new request.
|
|
|
|
|
|
|
|
Args:
|
2014-02-21 07:34:49 +01:00
|
|
|
request: const QNetworkRequest & req
|
2014-04-22 17:53:27 +02:00
|
|
|
_op: Operation op
|
|
|
|
_outgoing_data: QIODevice * outgoingData
|
2014-02-20 23:08:27 +01:00
|
|
|
|
|
|
|
Return:
|
|
|
|
A QNetworkReply.
|
|
|
|
"""
|
2014-06-20 16:33:01 +02:00
|
|
|
path = request.url().path()
|
|
|
|
# An url like "qute:foo" is split as "scheme:path", not "scheme:host".
|
2014-06-20 19:50:44 +02:00
|
|
|
log.misc.debug("url: {}, path: {}".format(
|
|
|
|
request.url().toDisplayString(), path))
|
2014-04-22 15:46:46 +02:00
|
|
|
try:
|
2014-06-20 16:33:01 +02:00
|
|
|
handler = getattr(QuteHandlers, path)
|
2014-04-22 15:46:46 +02:00
|
|
|
except AttributeError:
|
|
|
|
data = bytes()
|
|
|
|
else:
|
|
|
|
data = handler()
|
2014-04-25 16:53:23 +02:00
|
|
|
return SpecialNetworkReply(request, data, 'text/html', self.parent())
|
2014-02-20 23:08:27 +01:00
|
|
|
|
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
class QuteHandlers:
|
2014-02-07 20:21:50 +01:00
|
|
|
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handlers for qute:... pages."""
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def pyeval(cls):
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handler for qute:pyeval. Return HTML content as bytes."""
|
2014-04-22 15:28:48 +02:00
|
|
|
text = cgi.escape(pyeval_output)
|
|
|
|
return _get_html('pyeval', '<pre>{}</pre>'.format(text))
|
2014-02-07 11:01:58 +01:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def version(cls):
|
2014-02-21 07:18:04 +01:00
|
|
|
"""Handler for qute:version. Return HTML content as bytes."""
|
2014-05-27 15:33:44 +02:00
|
|
|
text = cgi.escape(version.version())
|
2014-05-27 15:46:39 +02:00
|
|
|
html = '<h1>Version info</h1>'
|
|
|
|
html += '<p>{}</p>'.format(text.replace('\n', '<br/>'))
|
|
|
|
html += '<h1>Copyright info</h1>'
|
|
|
|
html += '<p>{}</p>'.format(qutebrowser.__copyright__)
|
|
|
|
html += version.GPL_BOILERPLATE_HTML
|
2014-05-27 15:33:44 +02:00
|
|
|
return _get_html('Version', html)
|
2014-05-25 20:26:26 +02:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def log(cls):
|
2014-05-25 20:26:26 +02:00
|
|
|
"""Handler for qute:log. Return HTML content as bytes."""
|
2014-06-19 12:06:36 +02:00
|
|
|
if log.ram_handler is None:
|
|
|
|
text = "Log output was disabled."
|
|
|
|
else:
|
|
|
|
text = cgi.escape(log.ram_handler.dump_log())
|
2014-05-25 20:26:26 +02:00
|
|
|
return _get_html('log', '<pre>{}</pre>'.format(text))
|
2014-05-27 15:33:44 +02:00
|
|
|
|
|
|
|
@classmethod
|
2014-06-20 16:33:01 +02:00
|
|
|
def gpl(cls):
|
2014-05-27 15:33:44 +02:00
|
|
|
"""Handler for qute:gpl. Return HTML content as bytes."""
|
|
|
|
return read_file('html/COPYING.html').encode('ASCII')
|