From 1f451ce2f91bfa0dd8508d608d018d691af384e4 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 28 May 2017 13:10:17 +0200 Subject: [PATCH 1/2] Fix #3781: instruct SmartQuotes of nodes to skip Relates #3666, #3787 --- sphinx/transforms/__init__.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/sphinx/transforms/__init__.py b/sphinx/transforms/__init__.py index aceffa329..1e3e163e1 100644 --- a/sphinx/transforms/__init__.py +++ b/sphinx/transforms/__init__.py @@ -9,16 +9,19 @@ :license: BSD, see LICENSE for details. """ +import docutils from docutils import nodes from docutils.transforms import Transform, Transformer from docutils.transforms.parts import ContentsFilter from docutils.utils import new_document +from docutils.transforms.universal import SmartQuotes as DocutilsSmartQuotes from sphinx import addnodes from sphinx.locale import _ from sphinx.util import logging from sphinx.util.i18n import format_date from sphinx.util.nodes import apply_source_workaround +from sphinx import addnodes if False: # For type annotation @@ -327,3 +330,33 @@ class SphinxContentsFilter(ContentsFilter): def visit_image(self, node): # type: (nodes.Node) -> None raise nodes.SkipNode + + +class SphinxSmartQuotes(DocutilsSmartQuotes): # NOQA + """ + Customized SmartQuotes to avoid transform for some extra node types. + """ + def get_tokens(self, txtnodes): + # A generator that yields ``(texttype, nodetext)`` tuples for a list + # of "Text" nodes (interface to ``smartquotes.educate_tokens()``). + + texttype = {True: 'literal', # "literal" text is not changed: + False: 'plain'} + for txtnode in txtnodes: + nodetype = texttype[isinstance(txtnode.parent, + (nodes.literal, + nodes.literal_block, + addnodes.literal_emphasis, + addnodes.literal_strong, + addnodes.desc_signature, + addnodes.productionlist, + addnodes.desc_optional, + addnodes.desc_name, + nodes.math, + nodes.image, + nodes.raw, + nodes.problematic))] + yield (nodetype, txtnode.astext()) + + +docutils.transforms.universal.SmartQuotes = SphinxSmartQuotes From 3f6bfe1e8afb03bada144d286a19d32d648df34a Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 28 May 2017 13:32:44 +0200 Subject: [PATCH 2/2] Fix flake8 --- sphinx/transforms/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sphinx/transforms/__init__.py b/sphinx/transforms/__init__.py index 1e3e163e1..c52eafda2 100644 --- a/sphinx/transforms/__init__.py +++ b/sphinx/transforms/__init__.py @@ -21,7 +21,6 @@ from sphinx.locale import _ from sphinx.util import logging from sphinx.util.i18n import format_date from sphinx.util.nodes import apply_source_workaround -from sphinx import addnodes if False: # For type annotation @@ -340,7 +339,7 @@ class SphinxSmartQuotes(DocutilsSmartQuotes): # NOQA # A generator that yields ``(texttype, nodetext)`` tuples for a list # of "Text" nodes (interface to ``smartquotes.educate_tokens()``). - texttype = {True: 'literal', # "literal" text is not changed: + texttype = {True: 'literal', # "literal" text is not changed: False: 'plain'} for txtnode in txtnodes: nodetype = texttype[isinstance(txtnode.parent,