diff options
author | Florian Bruhin <me@the-compiler.org> | 2021-01-08 11:52:57 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2021-01-09 14:20:09 +0100 |
commit | 9a8b7e8e98f0902758922a0e5f9c021475ccabc0 (patch) | |
tree | c7c95dc6a8262e97f050ec217aebc543232fbf89 | |
parent | 3caad720b119f76a057591eaac18be0fe89a56e1 (diff) | |
download | qutebrowser-9a8b7e8e98f0902758922a0e5f9c021475ccabc0.tar.gz qutebrowser-9a8b7e8e98f0902758922a0e5f9c021475ccabc0.zip |
Make pygments dependency optional
Closes #5555
-rw-r--r-- | doc/changelog.asciidoc | 4 | ||||
-rw-r--r-- | doc/help/commands.asciidoc | 1 | ||||
-rw-r--r-- | misc/requirements/requirements-qutebrowser.txt-raw | 8 | ||||
-rw-r--r-- | qutebrowser/browser/browsertab.py | 62 | ||||
-rw-r--r-- | qutebrowser/browser/commands.py | 1 | ||||
-rw-r--r-- | qutebrowser/html/pre.html | 3 | ||||
-rw-r--r-- | qutebrowser/misc/earlyinit.py | 1 | ||||
-rwxr-xr-x | setup.py | 2 |
8 files changed, 55 insertions, 27 deletions
diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index face04c4d..c338d1545 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -44,6 +44,10 @@ Major changes still relying on it. The `cssutils` project is also dead upstream, with its repository being gone after Bitbucket https://bitbucket.org/blog/sunsetting-mercurial-support-in-bitbucket[removed Mercurial support]. +- The (formerly required) `pygments` dependency is now optional. It is only + used when using `:view-source` with QtWebKit, or when forcing it via + `:view-source --pygments` on QtWebEngine. If it is unavailable, an + unhighlighted fallback version of the page's source is shown. - TODO: The former dependency on the `pkg_resources` module (part of the `setuptools` project) got dropped. - A new dependency on the `importlib_resources` module got introduced for diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index eb8e4925d..3564c9766 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -1515,6 +1515,7 @@ Show the source of the current page in a new tab. * +*-p*+, +*--pygments*+: Use pygments to generate the view. This is always the case for QtWebKit. For QtWebEngine it may display slightly different source. Some JavaScript processing may be applied. + Needs the optional Pygments dependency for highlighting. [[window-only]] diff --git a/misc/requirements/requirements-qutebrowser.txt-raw b/misc/requirements/requirements-qutebrowser.txt-raw index 2d527aeef..03e6324f6 100644 --- a/misc/requirements/requirements-qutebrowser.txt-raw +++ b/misc/requirements/requirements-qutebrowser.txt-raw @@ -1,10 +1,12 @@ Jinja2 -Pygments pyPEG2 PyYAML -colorama attrs -adblock # Optional, for improved adblocking importlib-resources +## Optional dependencies +Pygments # For :view-source --pygments or on QtWebKit +colorama # Colored log output on Windows +adblock # Improved adblocking + #@ markers: importlib-resources python_version<"3.9" diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index dda7c7720..c62c75d48 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -38,14 +38,10 @@ if TYPE_CHECKING: from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebEngineWidgets import QWebEngineHistory, QWebEnginePage -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, - urlutils, message) + urlutils, message, jinja) from qutebrowser.misc import miscwidgets, objects, sessions from qutebrowser.browser import eventfilter, inspector from qutebrowser.qt import sip @@ -177,30 +173,52 @@ class AbstractAction: raise WebTabError("{} is not a valid web action!".format(name)) self._widget.triggerPageAction(member) - def show_source( - self, - pygments: bool = False # pylint: disable=redefined-outer-name - ) -> None: + def show_source(self, pygments: bool = False) -> None: """Show the source of the current page in a new tab.""" raise NotImplementedError + def _show_html_source(self, html: str) -> None: + """Show the given HTML as source page.""" + tb = objreg.get('tabbed-browser', scope='window', window=self._tab.win_id) + new_tab = tb.tabopen(background=False, related=True) + new_tab.set_html(html, self._tab.url()) + new_tab.data.viewing_source = True + + def _show_source_fallback(self, source: str) -> None: + """Show source with pygments unavailable.""" + html = jinja.render( + 'pre.html', + title='Source', + content=source, + preamble="Note: The optional Pygments dependency wasn't found - " + "showing unhighlighted source.", + ) + self._show_html_source(html) + def _show_source_pygments(self) -> None: def show_source_cb(source: str) -> None: """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) - new_tab.set_html(highlighted, self._tab.url()) - new_tab.data.viewing_source = True + try: + import pygments + import pygments.lexers + import pygments.formatters + except ImportError: + # Pygments is an optional dependency + self._show_source_fallback(source) + return + + try: + lexer = pygments.lexers.HtmlLexer() + formatter = pygments.formatters.HtmlFormatter( + full=True, linenos='table') + except AttributeError: + # Remaining namespace package from Pygments + self._show_source_fallback(source) + return + + html = pygments.highlight(source, lexer, formatter) + self._show_html_source(html) self._tab.dump_async(show_source_cb) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 18777e250..fda645416 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1343,6 +1343,7 @@ class CommandDispatcher: the case for QtWebKit. For QtWebEngine it may display slightly different source. Some JavaScript processing may be applied. + Needs the optional Pygments dependency for highlighting. """ tab = self._current_widget() try: diff --git a/qutebrowser/html/pre.html b/qutebrowser/html/pre.html index cfcfad359..5fb9c7f40 100644 --- a/qutebrowser/html/pre.html +++ b/qutebrowser/html/pre.html @@ -1,6 +1,9 @@ {% extends "base.html" %} {% block content %} {{ super() }} +{% if preamble is defined %} + <p>{{ preamble }}</p> +{% endif %} <pre> {{ content }} </pre> diff --git a/qutebrowser/misc/earlyinit.py b/qutebrowser/misc/earlyinit.py index 413e9d111..936716078 100644 --- a/qutebrowser/misc/earlyinit.py +++ b/qutebrowser/misc/earlyinit.py @@ -228,7 +228,6 @@ def check_libraries(): 'pkg_resources': _missing_str("pkg_resources/setuptools"), 'pypeg2': _missing_str("pypeg2"), 'jinja2': _missing_str("jinja2"), - 'pygments': _missing_str("pygments"), 'yaml': _missing_str("PyYAML"), 'attr': _missing_str("attrs"), 'PyQt5.QtQml': _missing_str("PyQt5.QtQml"), @@ -71,7 +71,7 @@ try: entry_points={'gui_scripts': ['qutebrowser = qutebrowser.qutebrowser:main']}, zip_safe=True, - install_requires=['pypeg2', 'jinja2', 'pygments', 'PyYAML', 'attrs', + install_requires=['pypeg2', 'jinja2', 'PyYAML', 'attrs', 'importlib_resources>=1.1.0; python_version < "3.9"'], python_requires='>=3.6', name='qutebrowser', |