Skip to content

Commit baacc26

Browse files
authored
Merge pull request #9565 from JustinTArthur/9564-fix_highlighted_code_role_smartquotes
Check complete ancestry of text nodes for smartquotes eligibility.
2 parents 5c3eebc + 276f943 commit baacc26

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Bugs fixed
3838
* #9267: html theme: CSS and JS files added by theme were loaded twice
3939
* #9535 comment: C++, fix parsing of defaulted function parameters that are
4040
function pointers.
41+
* #9564: smartquotes: don't adjust typography for text with
42+
language-highlighted ``:code:`` role.
4143

4244
Testing
4345
--------

sphinx/util/nodes.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,16 @@ def copy_source_info(src: Element, dst: Element) -> None:
589589

590590
def is_smartquotable(node: Node) -> bool:
591591
"""Check whether the node is smart-quotable or not."""
592-
if isinstance(node.parent, NON_SMARTQUOTABLE_PARENT_NODES):
593-
return False
594-
elif node.parent.get('support_smartquotes', None) is False:
595-
return False
596-
elif getattr(node, 'support_smartquotes', None) is False:
592+
for pnode in traverse_parent(node.parent):
593+
if isinstance(pnode, NON_SMARTQUOTABLE_PARENT_NODES):
594+
return False
595+
elif pnode.get('support_smartquotes', None) is False:
596+
return False
597+
598+
if getattr(node, 'support_smartquotes', None) is False:
597599
return False
598-
else:
599-
return True
600+
601+
return True
600602

601603

602604
def process_only_nodes(document: Node, tags: "Tags") -> None:

tests/roots/test-smartquotes/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ test-smartquotes
22
================
33

44
-- "Sphinx" is a tool that makes it easy ...
5+
6+
.. toctree::
7+
8+
literals
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
literals
2+
========
3+
4+
.. role:: python(code)
5+
:language: python
6+
.. default-role:: python
7+
8+
Standard :code:`code role with 'quotes'`
9+
10+
This is a Python :python:`{'code': 'role', 'with': 'quotes'}`.
11+
12+
This is a ``literal with 'quotes'``

tests/test_smartquotes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
import pytest
12+
from html5lib import HTMLParser
1213

1314

1415
@pytest.mark.sphinx(buildername='html', testroot='smartquotes', freshenv=True)
@@ -19,6 +20,24 @@ def test_basic(app, status, warning):
1920
assert '<p>– “Sphinx” is a tool that makes it easy …</p>' in content
2021

2122

23+
@pytest.mark.sphinx(buildername='html', testroot='smartquotes', freshenv=True)
24+
def test_literals(app, status, warning):
25+
app.build()
26+
27+
with (app.outdir / 'literals.html').open() as html_file:
28+
etree = HTMLParser(namespaceHTMLElements=False).parse(html_file)
29+
30+
for code_element in etree.iter('code'):
31+
code_text = ''.join(code_element.itertext())
32+
33+
if code_text.startswith('code role'):
34+
assert "'quotes'" in code_text
35+
elif code_text.startswith('{'):
36+
assert code_text == "{'code': 'role', 'with': 'quotes'}"
37+
elif code_text.startswith('literal'):
38+
assert code_text == "literal with 'quotes'"
39+
40+
2241
@pytest.mark.sphinx(buildername='text', testroot='smartquotes', freshenv=True)
2342
def test_text_builder(app, status, warning):
2443
app.build()

0 commit comments

Comments
 (0)