Refactor about:... handling

This commit is contained in:
Florian Bruhin 2014-02-07 10:07:13 +01:00
parent 210380e4b0
commit 920683b804
4 changed files with 92 additions and 8 deletions

View File

@ -36,6 +36,7 @@ from PyQt5.QtCore import QUrl, QTimer
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
import qutebrowser.utils.config as config import qutebrowser.utils.config as config
import qutebrowser.utils.about as about
from qutebrowser.widgets.mainwindow import MainWindow from qutebrowser.widgets.mainwindow import MainWindow
from qutebrowser.widgets import CrashDialog from qutebrowser.widgets import CrashDialog
from qutebrowser.commands.keys import KeyParser from qutebrowser.commands.keys import KeyParser
@ -288,13 +289,8 @@ class QuteBrowser(QApplication):
out = repr(r) out = repr(r)
except Exception as e: # pylint: disable=broad-except except Exception as e: # pylint: disable=broad-except
out = ': '.join([e.__class__.__name__, str(e)]) out = ': '.join([e.__class__.__name__, str(e)])
about.pyeval_output = out
# FIXME we probably want some nicer interface to display these about: self.mainwindow.tabs.openurl('about:pyeval')
# pages
tab = self.mainwindow.tabs.currentWidget()
tab.setUrl(QUrl('about:pyeval'))
tab.titleChanged.emit('about:pyeval')
tab.setContent(out.encode('UTF-8'), 'text/plain')
def crash(self): def crash(self):
"""Crash for debugging purposes. """Crash for debugging purposes.

View File

@ -69,6 +69,12 @@ def version():
return ''.join(lines) return ''.join(lines)
def is_about_url(url):
"""Return True if url is an about:... or other special URL."""
url = qurl(url)
return url.toString().replace('http://', '').startswith('about:')
def _git_str(): def _git_str():
"""Try to find out git version and return a string if possible. """Try to find out git version and return a string if possible.

View File

@ -0,0 +1,75 @@
"""Handler functions for different about:... pages."""
# 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/>.
import qutebrowser.utils as utils
_html_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{title}</title>
</head>
<body>
{body}
</body>
</html>
"""
pyeval_output = None
def handle(url):
"""Handle about page with an url.
Returns HTML content.
"""
if not utils.is_about_url(url):
raise ValueError
handler = getattr(AboutHandlers, _transform_url(url))
return handler()
def _transform_url(url):
return url.replace('http://', '').replace('about:', 'about_')
def _get_html(title, snippet):
"""Add HTML boilerplate to a html snippet.
title -- The title the page should have.
snippet -- The html snippet.
"""
return _html_template.format(title=title, body=snippet).encode('UTF-8')
class AboutHandlers:
"""Handlers for about:... pages."""
@classmethod
def about_pyeval(cls):
"""Handler for about:pyeval."""
return _get_html('pyeval', '<pre>{}</pre>'.format(pyeval_output))
@classmethod
def about_version(cls):
"""Handler for about:version."""
return _get_html('Version', '<pre>{}</pre>'.format(utils.version()))

View File

@ -32,6 +32,7 @@ from PyQt5.QtPrintSupport import QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebView, QWebPage from PyQt5.QtWebKitWidgets import QWebView, QWebPage
import qutebrowser.utils as utils import qutebrowser.utils as utils
import qutebrowser.utils.about as about
import qutebrowser.utils.config as config import qutebrowser.utils.config as config
from qutebrowser.widgets.tabbar import TabWidget from qutebrowser.widgets.tabbar import TabWidget
@ -430,7 +431,13 @@ class BrowserTab(QWebView):
qurl = utils.qurl(url) qurl = utils.qurl(url)
logging.debug('New title: {}'.format(qurl.url())) logging.debug('New title: {}'.format(qurl.url()))
self.titleChanged.emit(qurl.url()) self.titleChanged.emit(qurl.url())
return self.load(qurl) if utils.is_about_url(qurl):
content = about.handle(qurl.toString())
self.setUrl(qurl)
self.setContent(content, 'text/html')
return
else:
return self.load(qurl)
def link_handler(self, url): def link_handler(self, url):
"""Handle a link. """Handle a link.