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-02-21 07:35:53 +01:00
|
|
|
import logging
|
2014-04-22 15:28:48 +02:00
|
|
|
import cgi
|
2014-02-21 07:35:53 +01:00
|
|
|
|
2014-02-20 23:08:27 +01:00
|
|
|
from qutebrowser.network.schemehandler import (SchemeHandler,
|
|
|
|
SpecialNetworkReply)
|
2014-02-10 15:01:05 +01:00
|
|
|
from qutebrowser.utils.version import version
|
2014-02-21 07:18:04 +01:00
|
|
|
from qutebrowser.utils.url import urlstring
|
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>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
pyeval_output = None
|
|
|
|
|
|
|
|
|
|
|
|
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-02-19 11:04:13 +01:00
|
|
|
return _HTML_TEMPLATE.format(title=title, body=snippet).encode('UTF-8')
|
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-02-21 19:51:04 +01:00
|
|
|
def _transform_url(self, url):
|
|
|
|
"""Transform a special URL to an QuteHandlers method name.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
url: The URL as string.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
The method name qute_*
|
|
|
|
"""
|
|
|
|
return url.replace('http://', '').replace('qute:', 'qute_')
|
|
|
|
|
2014-04-16 11:05:58 +02:00
|
|
|
def createRequest(self, op, request, outgoing_data):
|
2014-02-20 23:08:27 +01:00
|
|
|
"""Create a new request.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
op: Operation op
|
2014-02-21 07:34:49 +01:00
|
|
|
request: const QNetworkRequest & req
|
2014-02-20 23:08:27 +01:00
|
|
|
outgoing_data: QIODevice * outgoingData
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A QNetworkReply.
|
|
|
|
"""
|
2014-04-16 11:05:58 +02:00
|
|
|
# pylint: disable=unused-argument
|
2014-02-21 07:35:53 +01:00
|
|
|
logging.debug('request: {}'.format(request))
|
2014-02-21 19:51:04 +01:00
|
|
|
url = urlstring(request.url())
|
2014-04-22 15:46:46 +02:00
|
|
|
try:
|
|
|
|
handler = getattr(QuteHandlers, self._transform_url(url))
|
|
|
|
except AttributeError:
|
|
|
|
data = bytes()
|
|
|
|
else:
|
|
|
|
data = handler()
|
2014-02-20 23:08:27 +01:00
|
|
|
return SpecialNetworkReply(request, data, "text/html", self.parent())
|
|
|
|
|
|
|
|
|
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-02-21 07:18:04 +01:00
|
|
|
def qute_pyeval(cls):
|
|
|
|
"""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-02-21 07:18:04 +01:00
|
|
|
def qute_version(cls):
|
|
|
|
"""Handler for qute:version. Return HTML content as bytes."""
|
2014-04-22 15:28:48 +02:00
|
|
|
text = cgi.escape(version())
|
|
|
|
return _get_html('Version', '<pre>{}</pre>'.format(text))
|