mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Highlighting fallbacks by default
In 423bf7b, I tried to add fallback mechanism to ``python3``. But it breaks
the python3 highlighting on python2 environment.
This adds ``'default'`` to highlighting languages; it works like
``'python3'``, but fallbacks if failed Highlighting. And this removes
try-parse step from ``'python3'`` language. Now, it highlights regardless of
runtime environments. Thanks to Yoshiki SHIBUKAWA.
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
|
||||
from pygments.lexer import RegexLexer
|
||||
from pygments.token import Text, Name
|
||||
from pygments.filters import ErrorToken
|
||||
from pygments.formatters.html import HtmlFormatter
|
||||
|
||||
from sphinx.highlighting import PygmentsBridge
|
||||
@@ -86,3 +87,28 @@ def test_trim_doctest_flags():
|
||||
assert ret == '>>> 1+2 \n3\n'
|
||||
finally:
|
||||
PygmentsBridge.html_formatter = HtmlFormatter
|
||||
|
||||
|
||||
def test_default_highlight():
|
||||
bridge = PygmentsBridge('html')
|
||||
|
||||
# default: highlights as python3
|
||||
ret = bridge.highlight_block('print "Hello sphinx world"', 'default')
|
||||
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
|
||||
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
|
||||
|
||||
# default: fallbacks to none if highlighting failed
|
||||
ret = bridge.highlight_block('reST ``like`` text', 'default')
|
||||
assert ret == '<div class="highlight"><pre><span></span>reST ``like`` text\n</pre></div>\n'
|
||||
|
||||
# python3: highlights as python3
|
||||
ret = bridge.highlight_block('print "Hello sphinx world"', 'python3')
|
||||
assert ret == ('<div class="highlight"><pre><span></span><span class="nb">print</span> '
|
||||
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n')
|
||||
|
||||
# python3: raises error if highlighting failed
|
||||
try:
|
||||
ret = bridge.highlight_block('reST ``like`` text', 'python3')
|
||||
assert False, "highlight_block() does not raise any exceptions"
|
||||
except ErrorToken:
|
||||
pass # raise parsing error
|
||||
|
||||
Reference in New Issue
Block a user