| 1 |
"""Test the Pygments highlighting bridge.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
import pygments |
| 9 |
import pytest |
| 10 |
from pygments.formatters.html import HtmlFormatter |
| 11 |
from pygments.lexer import RegexLexer |
| 12 |
from pygments.token import Name, Text |
| 13 |
|
| 14 |
from sphinx.highlighting import PygmentsBridge |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from collections.abc import Iterable |
| 18 |
from typing import TextIO |
| 19 |
|
| 20 |
from pygments.token import _TokenType |
| 21 |
|
| 22 |
from sphinx.testing.util import SphinxTestApp |
| 23 |
|
| 24 |
if tuple(map(int, pygments.__version__.split('.')[:2])) < (2, 18): |
| 25 |
from pygments.formatter import Formatter |
| 26 |
|
| 27 |
Formatter.__class_getitem__ = classmethod(lambda cls, name: cls) # type: ignore[attr-defined] |
| 28 |
|
| 29 |
|
| 30 |
class MyLexer(RegexLexer): |
| 31 |
name = 'testlexer' |
| 32 |
|
| 33 |
tokens = { |
| 34 |
'root': [ |
| 35 |
('a', Name), |
| 36 |
('b', Text), |
| 37 |
], |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
class MyFormatter(HtmlFormatter[str]): |
| 42 |
def format( |
| 43 |
self, |
| 44 |
tokensource: Iterable[tuple[_TokenType, str]], |
| 45 |
outfile: TextIO, |
| 46 |
) -> None: |
| 47 |
outfile.writelines(tok[1] for tok in tokensource) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.sphinx('html', testroot='root') |
| 51 |
def test_add_lexer(app: SphinxTestApp) -> None: |
| 52 |
app.add_lexer('test', MyLexer) |
| 53 |
|
| 54 |
bridge = PygmentsBridge('html') |
| 55 |
ret = bridge.highlight_block('ab', 'test') |
| 56 |
assert '<span class="n">a</span>b' in ret |
| 57 |
|
| 58 |
|
| 59 |
def test_detect_interactive() -> None: |
| 60 |
bridge = PygmentsBridge('html') |
| 61 |
blocks = [ |
| 62 |
""" |
| 63 |
>>> testing() |
| 64 |
True |
| 65 |
""", |
| 66 |
] |
| 67 |
for block in blocks: |
| 68 |
ret = bridge.highlight_block(block.lstrip(), 'python') |
| 69 |
assert ret.startswith('<div class="highlight">') |
| 70 |
|
| 71 |
|
| 72 |
def test_lexer_options() -> None: |
| 73 |
bridge = PygmentsBridge('html') |
| 74 |
ret = bridge.highlight_block('//comment', 'php', opts={'startinline': True}) |
| 75 |
assert '<span class="c1">//comment</span>' in ret |
| 76 |
|
| 77 |
|
| 78 |
def test_set_formatter() -> None: |
| 79 |
PygmentsBridge.html_formatter = MyFormatter |
| 80 |
try: |
| 81 |
bridge = PygmentsBridge('html') |
| 82 |
ret = bridge.highlight_block('foo\n', 'python') |
| 83 |
assert ret == 'foo\n' |
| 84 |
finally: |
| 85 |
PygmentsBridge.html_formatter = HtmlFormatter |
| 86 |
|
| 87 |
|
| 88 |
@mock.patch('sphinx.highlighting.logger') |
| 89 |
def test_default_highlight(logger: mock.Mock) -> None: |
| 90 |
bridge = PygmentsBridge('html') |
| 91 |
|
| 92 |
# default: highlights as python3 |
| 93 |
ret = bridge.highlight_block('print "Hello sphinx world"', 'default') |
| 94 |
assert ret == ( |
| 95 |
'<div class="highlight"><pre><span></span><span class="nb">print</span> ' |
| 96 |
'<span class="s2">"Hello sphinx world"</span>\n</pre></div>\n' |
| 97 |
) |
| 98 |
|
| 99 |
# default: fallbacks to none if highlighting failed |
| 100 |
ret = bridge.highlight_block('reST ``like`` text', 'default') |
| 101 |
assert ret == ( |
| 102 |
'<div class="highlight"><pre><span></span>reST ``like`` text\n</pre></div>\n' |
| 103 |
) |
| 104 |
|
| 105 |
# python: highlights as python3 |
| 106 |
ret = bridge.highlight_block('print("Hello sphinx world")', 'python') |
| 107 |
assert ret == ( |
| 108 |
'<div class="highlight"><pre><span></span><span class="nb">print</span>' |
| 109 |
'<span class="p">(</span>' |
| 110 |
'<span class="s2">"Hello sphinx world"</span>' |
| 111 |
'<span class="p">)</span>\n</pre></div>\n' |
| 112 |
) |
| 113 |
|
| 114 |
# python3: highlights as python3 |
| 115 |
ret = bridge.highlight_block('print("Hello sphinx world")', 'python3') |
| 116 |
assert ret == ( |
| 117 |
'<div class="highlight"><pre><span></span><span class="nb">print</span>' |
| 118 |
'<span class="p">(</span>' |
| 119 |
'<span class="s2">"Hello sphinx world"</span>' |
| 120 |
'<span class="p">)</span>\n</pre></div>\n' |
| 121 |
) |
| 122 |
|
| 123 |
# python: raises error if highlighting failed |
| 124 |
ret = bridge.highlight_block('reST ``like`` text', 'python') |
| 125 |
logger.warning.assert_called_with( |
| 126 |
'Lexing literal_block %r as "%s" resulted in an error at token: %r. ' |
| 127 |
'Retrying in relaxed mode.', |
| 128 |
'reST ``like`` text', |
| 129 |
'python', |
| 130 |
'`', |
| 131 |
type='misc', |
| 132 |
subtype='highlighting_failure', |
| 133 |
location=None, |
| 134 |
) |