diff --git a/CHANGES b/CHANGES index 26282a80744..950ce56796c 100644 --- a/CHANGES +++ b/CHANGES @@ -61,6 +61,8 @@ Bugs fixed class inherites the class having ``_name`` attribute * #9537: autodoc: Some objects under ``typing`` module are not displayed well with the HEAD of 3.10 +* #9564: smartquotes: don't adjust typography for text with + language-highlighted ``:code:`` role. * #9512: sphinx-build: crashed with the HEAD of Python 3.10 Testing diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index 7ce0933c602..78663e4c707 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -589,14 +589,16 @@ def copy_source_info(src: Element, dst: Element) -> None: def is_smartquotable(node: Node) -> bool: """Check whether the node is smart-quotable or not.""" - if isinstance(node.parent, NON_SMARTQUOTABLE_PARENT_NODES): - return False - elif node.parent.get('support_smartquotes', None) is False: - return False - elif getattr(node, 'support_smartquotes', None) is False: + for pnode in traverse_parent(node.parent): + if isinstance(pnode, NON_SMARTQUOTABLE_PARENT_NODES): + return False + elif pnode.get('support_smartquotes', None) is False: + return False + + if getattr(node, 'support_smartquotes', None) is False: return False - else: - return True + + return True def process_only_nodes(document: Node, tags: "Tags") -> None: diff --git a/tests/roots/test-smartquotes/index.rst b/tests/roots/test-smartquotes/index.rst index be0d9b89c08..7dfd01a2862 100644 --- a/tests/roots/test-smartquotes/index.rst +++ b/tests/roots/test-smartquotes/index.rst @@ -2,3 +2,7 @@ test-smartquotes ================ -- "Sphinx" is a tool that makes it easy ... + +.. toctree:: + + literals diff --git a/tests/roots/test-smartquotes/literals.rst b/tests/roots/test-smartquotes/literals.rst new file mode 100644 index 00000000000..ed77c8031fc --- /dev/null +++ b/tests/roots/test-smartquotes/literals.rst @@ -0,0 +1,12 @@ +literals +======== + +.. role:: python(code) + :language: python +.. default-role:: python + +Standard :code:`code role with 'quotes'` + +This is a Python :python:`{'code': 'role', 'with': 'quotes'}`. + +This is a ``literal with 'quotes'`` diff --git a/tests/test_smartquotes.py b/tests/test_smartquotes.py index 4879dedc61f..fda34a28edd 100644 --- a/tests/test_smartquotes.py +++ b/tests/test_smartquotes.py @@ -9,6 +9,7 @@ """ import pytest +from html5lib import HTMLParser @pytest.mark.sphinx(buildername='html', testroot='smartquotes', freshenv=True) @@ -19,6 +20,24 @@ def test_basic(app, status, warning): assert '

– “Sphinx” is a tool that makes it easy …

' in content +@pytest.mark.sphinx(buildername='html', testroot='smartquotes', freshenv=True) +def test_literals(app, status, warning): + app.build() + + with (app.outdir / 'literals.html').open() as html_file: + etree = HTMLParser(namespaceHTMLElements=False).parse(html_file) + + for code_element in etree.iter('code'): + code_text = ''.join(code_element.itertext()) + + if code_text.startswith('code role'): + assert "'quotes'" in code_text + elif code_text.startswith('{'): + assert code_text == "{'code': 'role', 'with': 'quotes'}" + elif code_text.startswith('literal'): + assert code_text == "literal with 'quotes'" + + @pytest.mark.sphinx(buildername='text', testroot='smartquotes', freshenv=True) def test_text_builder(app, status, warning): app.build()