summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Turner Arthur <justinarthur@gmail.com>2021-08-20 03:38:30 -0500
committerJustin Turner Arthur <justinarthur@gmail.com>2021-08-21 00:20:04 -0500
commitadcda091d968d25e83a3c69c13388c667ff5edcb (patch)
tree47fb1041923dc6984a9b8138caa5c6ae784eeeb1
parentb174b77a14b19cc94d48c6cdef0dc49068dbf086 (diff)
downloadsphinx-git-adcda091d968d25e83a3c69c13388c667ff5edcb.tar.gz
Check complete ancestry of text nodes for smartquotes eligibility.
Fixes sphinx-doc/sphinx#9564.
-rw-r--r--CHANGES2
-rw-r--r--sphinx/util/nodes.py16
-rw-r--r--tests/roots/test-smartquotes/index.rst4
-rw-r--r--tests/roots/test-smartquotes/literals.rst12
-rw-r--r--tests/test_smartquotes.py19
5 files changed, 46 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index 55ccdf146..4f5512917 100644
--- a/CHANGES
+++ b/CHANGES
@@ -38,6 +38,8 @@ Bugs fixed
* #9267: html theme: CSS and JS files added by theme were loaded twice
* #9535 comment: C++, fix parsing of defaulted function parameters that are
function pointers.
+* #9564: smartquotes: don't adjust typography for text with
+ language-highlighted ``:code:`` role.
Testing
--------
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py
index 7ce0933c6..78663e4c7 100644
--- a/sphinx/util/nodes.py
+++ b/sphinx/util/nodes.py
@@ -589,14 +589,16 @@ NON_SMARTQUOTABLE_PARENT_NODES = (
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 be0d9b89c..7dfd01a28 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 000000000..ed77c8031
--- /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 4879dedc6..fda34a28e 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 '<p>– “Sphinx” is a tool that makes it easy …</p>' 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()