File: test_highlighting.py

package info (click to toggle)
sphinx 8.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 26,712 kB
  • sloc: python: 105,846; javascript: 6,474; perl: 451; makefile: 178; sh: 37; xml: 19; ansic: 2
file content (131 lines) | stat: -rw-r--r-- 3,958 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Test the Pygments highlighting bridge."""

from __future__ import annotations

from typing import TYPE_CHECKING
from unittest import mock

import pygments
import pytest
from pygments.formatters.html import HtmlFormatter
from pygments.lexer import RegexLexer
from pygments.token import Name, Text

from sphinx.highlighting import PygmentsBridge

if TYPE_CHECKING:
    from sphinx.testing.util import SphinxTestApp

if tuple(map(int, pygments.__version__.split('.')[:2])) < (2, 18):
    from pygments.formatter import Formatter

    Formatter.__class_getitem__ = classmethod(lambda cls, name: cls)  # type: ignore[attr-defined]


class MyLexer(RegexLexer):
    name = 'testlexer'

    tokens = {
        'root': [
            ('a', Name),
            ('b', Text),
        ],
    }


class MyFormatter(HtmlFormatter[str]):
    def format(self, tokensource, outfile):
        for tok in tokensource:
            outfile.write(tok[1])


class ComplainOnUnhighlighted(PygmentsBridge):
    def unhighlighted(self, source):
        raise AssertionError('should highlight %r' % source)


@pytest.mark.sphinx('html', testroot='root')
def test_add_lexer(app: SphinxTestApp) -> None:
    app.add_lexer('test', MyLexer)

    bridge = PygmentsBridge('html')
    ret = bridge.highlight_block('ab', 'test')
    assert '<span class="n">a</span>b' in ret


def test_detect_interactive() -> None:
    bridge = ComplainOnUnhighlighted('html')
    blocks = [
        """
        >>> testing()
        True
        """,
    ]
    for block in blocks:
        ret = bridge.highlight_block(block.lstrip(), 'python')
        assert ret.startswith('<div class="highlight">')


def test_lexer_options() -> None:
    bridge = PygmentsBridge('html')
    ret = bridge.highlight_block('//comment', 'php', opts={'startinline': True})
    assert '<span class="c1">//comment</span>' in ret


def test_set_formatter() -> None:
    PygmentsBridge.html_formatter = MyFormatter
    try:
        bridge = PygmentsBridge('html')
        ret = bridge.highlight_block('foo\n', 'python')
        assert ret == 'foo\n'
    finally:
        PygmentsBridge.html_formatter = HtmlFormatter


@mock.patch('sphinx.highlighting.logger')
def test_default_highlight(logger):
    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">&quot;Hello sphinx world&quot;</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'
    )

    # python: highlights as python3
    ret = bridge.highlight_block('print("Hello sphinx world")', 'python')
    assert ret == (
        '<div class="highlight"><pre><span></span><span class="nb">print</span>'
        '<span class="p">(</span>'
        '<span class="s2">&quot;Hello sphinx world&quot;</span>'
        '<span class="p">)</span>\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="p">(</span>'
        '<span class="s2">&quot;Hello sphinx world&quot;</span>'
        '<span class="p">)</span>\n</pre></div>\n'
    )

    # python: raises error if highlighting failed
    ret = bridge.highlight_block('reST ``like`` text', 'python')
    logger.warning.assert_called_with(
        'Lexing literal_block %r as "%s" resulted in an error at token: %r. '
        'Retrying in relaxed mode.',
        'reST ``like`` text',
        'python',
        '`',
        type='misc',
        subtype='highlighting_failure',
        location=None,
    )