Make pygments dependency optional

Closes #5555
This commit is contained in:
Florian Bruhin 2021-01-08 11:52:57 +01:00
parent 3caad720b1
commit 9a8b7e8e98
8 changed files with 54 additions and 26 deletions

View File

@ -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

View File

@ -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]]

View File

@ -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"

View File

@ -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)
try:
import pygments
import pygments.lexers
import pygments.formatters
except ImportError:
# Pygments is an optional dependency
self._show_source_fallback(source)
return
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:
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)

View File

@ -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:

View File

@ -1,6 +1,9 @@
{% extends "base.html" %}
{% block content %}
{{ super() }}
{% if preamble is defined %}
<p>{{ preamble }}</p>
{% endif %}
<pre>
{{ content }}
</pre>

View File

@ -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"),

View File

@ -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',