diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 3db074681..6a61fcacb 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -36,6 +36,7 @@ from PyQt5.QtCore import QUrl, QTimer import qutebrowser.commands.utils as cmdutils import qutebrowser.utils.config as config +import qutebrowser.utils.about as about from qutebrowser.widgets.mainwindow import MainWindow from qutebrowser.widgets import CrashDialog from qutebrowser.commands.keys import KeyParser @@ -288,13 +289,8 @@ class QuteBrowser(QApplication): out = repr(r) except Exception as e: # pylint: disable=broad-except out = ': '.join([e.__class__.__name__, str(e)]) - - # FIXME we probably want some nicer interface to display these about: - # pages - tab = self.mainwindow.tabs.currentWidget() - tab.setUrl(QUrl('about:pyeval')) - tab.titleChanged.emit('about:pyeval') - tab.setContent(out.encode('UTF-8'), 'text/plain') + about.pyeval_output = out + self.mainwindow.tabs.openurl('about:pyeval') def crash(self): """Crash for debugging purposes. diff --git a/qutebrowser/utils/__init__.py b/qutebrowser/utils/__init__.py index 04be77d03..0b9320617 100644 --- a/qutebrowser/utils/__init__.py +++ b/qutebrowser/utils/__init__.py @@ -69,6 +69,12 @@ def version(): 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(): """Try to find out git version and return a string if possible. diff --git a/qutebrowser/utils/about.py b/qutebrowser/utils/about.py new file mode 100644 index 000000000..33898a336 --- /dev/null +++ b/qutebrowser/utils/about.py @@ -0,0 +1,75 @@ +"""Handler functions for different about:... pages.""" + +# Copyright 2014 Florian Bruhin (The Compiler) +# +# 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 . + +import qutebrowser.utils as utils + + +_html_template = """ + + + + + {title} + + +{body} + + +""" + + +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', '
{}
'.format(pyeval_output)) + + @classmethod + def about_version(cls): + """Handler for about:version.""" + return _get_html('Version', '
{}
'.format(utils.version())) diff --git a/qutebrowser/widgets/browser.py b/qutebrowser/widgets/browser.py index 90a2b9e2e..e57bbc909 100644 --- a/qutebrowser/widgets/browser.py +++ b/qutebrowser/widgets/browser.py @@ -32,6 +32,7 @@ from PyQt5.QtPrintSupport import QPrintPreviewDialog from PyQt5.QtWebKitWidgets import QWebView, QWebPage import qutebrowser.utils as utils +import qutebrowser.utils.about as about import qutebrowser.utils.config as config from qutebrowser.widgets.tabbar import TabWidget @@ -430,7 +431,13 @@ class BrowserTab(QWebView): qurl = utils.qurl(url) logging.debug('New title: {}'.format(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): """Handle a link.