Skip to content

Commit

Permalink
Fix highlighting lexers
Browse files Browse the repository at this point in the history
- Ensure `pycon3` is always normalised to `pycon`
  • Loading branch information
AA-Turner committed Sep 24, 2022
1 parent cd24c54 commit 9ced736
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
10 changes: 2 additions & 8 deletions sphinx/ext/doctest.py
Expand Up @@ -94,15 +94,9 @@ def run(self) -> List[Node]:
# only save if it differs from code
node['test'] = test
if self.name == 'doctest':
if self.config.highlight_language in ('py', 'python'):
node['language'] = 'pycon'
else:
node['language'] = 'pycon3' # default
node['language'] = 'pycon'
elif self.name == 'testcode':
if self.config.highlight_language in ('py', 'python'):
node['language'] = 'python'
else:
node['language'] = 'python3' # default
node['language'] = 'python'
elif self.name == 'testoutput':
# don't try to highlight output
node['language'] = 'none'
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/viewcode.py
Expand Up @@ -244,7 +244,7 @@ def collect_pages(app: Sphinx) -> Generator[Tuple[str, Dict[str, Any], str], Non
# construct a page name for the highlighted source
pagename = posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/'))
# highlight the source using the builder's highlighter
if env.config.highlight_language in ('python3', 'default', 'none'):
if env.config.highlight_language in {'default', 'none'}:
lexer = env.config.highlight_language
else:
lexer = 'python'
Expand Down
2 changes: 2 additions & 0 deletions sphinx/highlighting.py
Expand Up @@ -120,6 +120,8 @@ def get_lexer(self, source: str, lang: str, opts: Optional[Dict] = None,
lang = 'pycon'
else:
lang = 'python'
if lang == 'pycon3':
lang = 'pycon'

if lang in lexers:
# just return custom lexers here (without installing raiseonerror filter)
Expand Down
4 changes: 2 additions & 2 deletions sphinx/transforms/post_transforms/code.py
Expand Up @@ -109,9 +109,9 @@ def is_pyconsole(node: nodes.literal_block) -> bool:
return False # skip parsed-literal node

language = node.get('language')
if language in ('pycon', 'pycon3'):
if language in {'pycon', 'pycon3'}:
return True
elif language in ('py', 'py3', 'python', 'python3', 'default'):
elif language in {'py', 'python', 'py3', 'python3', 'default'}:
return node.rawsource.startswith('>>>')
elif language == 'guess':
try:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_ext_doctest.py
Expand Up @@ -29,16 +29,16 @@ def test_highlight_language_default(app, status, warning):
app.build()
doctree = app.env.get_doctree('doctest')
for node in doctree.findall(nodes.literal_block):
assert node['language'] in ('python3', 'pycon3', 'none')
assert node['language'] in {'python', 'pycon', 'none'}


@pytest.mark.sphinx('dummy', testroot='ext-doctest',
confoverrides={'highlight_language': 'python'})
def test_highlight_language_python2(app, status, warning):
def test_highlight_language_python3(app, status, warning):
app.build()
doctree = app.env.get_doctree('doctest')
for node in doctree.findall(nodes.literal_block):
assert node['language'] in ('python', 'pycon', 'none')
assert node['language'] in {'python', 'pycon', 'none'}


def test_is_allowed_version():
Expand Down
23 changes: 16 additions & 7 deletions tests/test_highlighting.py
Expand Up @@ -81,14 +81,23 @@ def test_default_highlight(logger):
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">&quot;Hello sphinx world&quot;</span>\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: raises error if highlighting failed
ret = bridge.highlight_block('reST ``like`` text', 'python3')
# 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('Could not lex literal_block as "%s". '
'Highlighting skipped.', 'python3',
'Highlighting skipped.', 'python',
type='misc', subtype='highlighting_failure',
location=None)

0 comments on commit 9ced736

Please sign in to comment.