From f7bcdfc8186b9beadecda34c43524b32414ec022 Mon Sep 17 00:00:00 2001 From: Markus Ongyerth Date: Wed, 28 Feb 2018 16:53:28 +0100 Subject: [PATCH] Add --pygment argument to view-source The --pygment argument allows to use the pygment version of view-source over the qtwebengine internal one. This version is slightly different in what's processed before the site is generated, so some javascript created texts can be available. --- qutebrowser/browser/browsertab.py | 29 ++++++++++++++++++- qutebrowser/browser/commands.py | 5 ++-- qutebrowser/browser/webengine/webenginetab.py | 6 +++- qutebrowser/browser/webkit/webkittab.py | 24 ++------------- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 79c3ae4d4..0d45f4ceb 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -28,6 +28,10 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QUrl, QObject, QSizeF, Qt from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QWidget, QApplication +import pygments +import pygments.lexers +import pygments.formatters + from qutebrowser.keyinput import modeman from qutebrowser.config import config from qutebrowser.utils import (utils, objreg, usertypes, log, qtutils, @@ -146,10 +150,33 @@ class AbstractAction: raise WebTabError("{} is not a valid web action!".format(name)) self._widget.triggerPageAction(member) - def show_source(self): + def show_source(self, pygment=False): """Show the source of the current page in a new tab.""" raise NotImplementedError + def _show_source_pygment(self): + + def show_source_cb(source): + """show source as soon as it's ready.""" + # workaround for https://github.com/pycqa/pylint/issues/491 + # pylint: disable=no-member + lexer = pygments.lexers.HtmlLexer() + formatter = pygments.formatters.HtmlFormatter( + full=True, linenos='table') + # pylint: enable=no-member + highlighted = pygments.highlight(source, lexer, formatter) + + tb = objreg.get('tabbed-browser', scope='window', + window=self._tab.win_id) + new_tab = tb.tabopen(background=False, related=True) + # The original URL becomes the path of a view-source: URL + # (without a host), but query/fragment should stay. + url = QUrl('view-source:' + urlstr) + new_tab.set_html(highlighted, url) + + urlstr = self._tab.url().toString(QUrl.RemoveUserInfo) + self._tab.dump_async(show_source_cb) + class AbstractPrinting: diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 6c2e3da8c..4c5e14213 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1513,7 +1513,8 @@ class CommandDispatcher: ) @cmdutils.register(instance='command-dispatcher', scope='window') - def view_source(self, edit=False): + @cmdutils.argument('pygment') + def view_source(self, edit=False, pygment=False): """Show the source of the current page in a new tab. Args: @@ -1532,7 +1533,7 @@ class CommandDispatcher: ed = editor.ExternalEditor(self._tabbed_browser) tab.dump_async(ed.edit) else: - tab.action.show_source() + tab.action.show_source(pygment) @cmdutils.register(instance='command-dispatcher', scope='window', debug=True) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 170650351..7b62305b5 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -100,7 +100,11 @@ class WebEngineAction(browsertab.AbstractAction): """Save the current page.""" self._widget.triggerPageAction(QWebEnginePage.SavePage) - def show_source(self): + def show_source(self, pygment): + if pygment: + self._show_source_pygment() + return + try: self._widget.triggerPageAction(QWebEnginePage.ViewSource) except AttributeError: diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 48c479171..07ae39748 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -55,28 +55,8 @@ class WebKitAction(browsertab.AbstractAction): """Save the current page.""" raise browsertab.UnsupportedOperationError - def show_source(self): - - def show_source_cb(source): - """Show source as soon as it's ready.""" - # WORKAROUND for https://github.com/PyCQA/pylint/issues/491 - # pylint: disable=no-member - lexer = pygments.lexers.HtmlLexer() - formatter = pygments.formatters.HtmlFormatter( - full=True, linenos='table') - # pylint: enable=no-member - highlighted = pygments.highlight(source, lexer, formatter) - - tb = objreg.get('tabbed-browser', scope='window', - window=self._tab.win_id) - new_tab = tb.tabopen(background=False, related=True) - # The original URL becomes the path of a view-source: URL - # (without a host), but query/fragment should stay. - url = QUrl('view-source:' + urlstr) - new_tab.set_html(highlighted, url) - - urlstr = self._tab.url().toString(QUrl.RemoveUserInfo) - self._tab.dump_async(show_source_cb) + def show_source(self, pygment): + self._show_source_pygment() class WebKitPrinting(browsertab.AbstractPrinting):