Skip to content

Commit

Permalink
Check complete ancestry of text nodes for smartquotes eligibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinTArthur committed Aug 21, 2021
1 parent b174b77 commit adcda09
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
--------
Expand Down
16 changes: 9 additions & 7 deletions sphinx/util/nodes.py
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions tests/roots/test-smartquotes/index.rst
Expand Up @@ -2,3 +2,7 @@ test-smartquotes
================

-- "Sphinx" is a tool that makes it easy ...

.. toctree::

literals
12 changes: 12 additions & 0 deletions 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'``
19 changes: 19 additions & 0 deletions tests/test_smartquotes.py
Expand Up @@ -9,6 +9,7 @@
"""

import pytest
from html5lib import HTMLParser


@pytest.mark.sphinx(buildername='html', testroot='smartquotes', freshenv=True)
Expand All @@ -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()
Expand Down

0 comments on commit adcda09

Please sign in to comment.