mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Make SphinxSmartQuotes more inteligent
This commit is contained in:
parent
776bd09f43
commit
24a6578e09
@ -20,11 +20,10 @@ from os import path
|
|||||||
from copy import copy
|
from copy import copy
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from six import BytesIO, itervalues, class_types, next, iteritems
|
from six import BytesIO, itervalues, class_types, next
|
||||||
from six.moves import cPickle as pickle
|
from six.moves import cPickle as pickle
|
||||||
|
|
||||||
from docutils.utils import Reporter, get_source_line, normalize_language_tag
|
from docutils.utils import Reporter, get_source_line
|
||||||
from docutils.utils.smartquotes import smartchars
|
|
||||||
from docutils.frontend import OptionParser
|
from docutils.frontend import OptionParser
|
||||||
|
|
||||||
from sphinx import addnodes, versioning
|
from sphinx import addnodes, versioning
|
||||||
@ -643,29 +642,10 @@ class BuildEnvironment(object):
|
|||||||
self.config.trim_footnote_reference_space
|
self.config.trim_footnote_reference_space
|
||||||
self.settings['gettext_compact'] = self.config.gettext_compact
|
self.settings['gettext_compact'] = self.config.gettext_compact
|
||||||
|
|
||||||
language = self.config.language or 'en'
|
self.settings['language_code'] = self.config.language or 'en'
|
||||||
self.settings['language_code'] = language
|
|
||||||
if 'smart_quotes' not in self.settings:
|
|
||||||
self.settings['smart_quotes'] = self.config.smartquotes
|
|
||||||
|
|
||||||
# some conditions exclude smart quotes, overriding smart_quotes
|
# Allow to disable by 3rd party extension (workaround)
|
||||||
for valname, vallist in iteritems(self.config.smartquotes_excludes):
|
self.settings.setdefault('smart_quotes', True)
|
||||||
if valname == 'builders':
|
|
||||||
# this will work only for checking first build target
|
|
||||||
if self.app.builder.name in vallist:
|
|
||||||
self.settings['smart_quotes'] = False
|
|
||||||
break
|
|
||||||
elif valname == 'languages':
|
|
||||||
if self.config.language in vallist:
|
|
||||||
self.settings['smart_quotes'] = False
|
|
||||||
break
|
|
||||||
|
|
||||||
# confirm selected language supports smart_quotes or not
|
|
||||||
for tag in normalize_language_tag(language):
|
|
||||||
if tag in smartchars.quotes:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
self.settings['smart_quotes'] = False
|
|
||||||
|
|
||||||
def read_doc(self, docname, app=None):
|
def read_doc(self, docname, app=None):
|
||||||
# type: (unicode, Sphinx) -> None
|
# type: (unicode, Sphinx) -> None
|
||||||
|
@ -93,21 +93,14 @@ class SphinxStandaloneReader(SphinxBaseReader):
|
|||||||
Locale, CitationReferences, DefaultSubstitutions, MoveModuleTargets,
|
Locale, CitationReferences, DefaultSubstitutions, MoveModuleTargets,
|
||||||
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds,
|
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds,
|
||||||
RemoveTranslatableInline, PreserveTranslatableMessages, FilterSystemMessages,
|
RemoveTranslatableInline, PreserveTranslatableMessages, FilterSystemMessages,
|
||||||
RefOnlyBulletListTransform, UnreferencedFootnotesDetector
|
RefOnlyBulletListTransform, UnreferencedFootnotesDetector, SphinxSmartQuotes,
|
||||||
] # type: List[Transform]
|
] # type: List[Transform]
|
||||||
|
|
||||||
def __init__(self, app, *args, **kwargs):
|
def __init__(self, app, *args, **kwargs):
|
||||||
# type: (Sphinx, Any, Any) -> None
|
# type: (Sphinx, Any, Any) -> None
|
||||||
self.transforms = self.transforms + app.registry.get_transforms()
|
self.transforms = self.transforms + app.registry.get_transforms()
|
||||||
self.smart_quotes = app.env.settings['smart_quotes']
|
|
||||||
SphinxBaseReader.__init__(self, app, *args, **kwargs) # type: ignore
|
SphinxBaseReader.__init__(self, app, *args, **kwargs) # type: ignore
|
||||||
|
|
||||||
def get_transforms(self):
|
|
||||||
transforms = SphinxBaseReader.get_transforms(self)
|
|
||||||
if self.smart_quotes:
|
|
||||||
transforms.append(SphinxSmartQuotes)
|
|
||||||
return transforms
|
|
||||||
|
|
||||||
|
|
||||||
class SphinxI18nReader(SphinxBaseReader):
|
class SphinxI18nReader(SphinxBaseReader):
|
||||||
"""
|
"""
|
||||||
|
@ -12,8 +12,9 @@
|
|||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
from docutils.transforms import Transform, Transformer
|
from docutils.transforms import Transform, Transformer
|
||||||
from docutils.transforms.parts import ContentsFilter
|
from docutils.transforms.parts import ContentsFilter
|
||||||
from docutils.utils import new_document
|
|
||||||
from docutils.transforms.universal import SmartQuotes
|
from docutils.transforms.universal import SmartQuotes
|
||||||
|
from docutils.utils import new_document, normalize_language_tag
|
||||||
|
from docutils.utils.smartquotes import smartchars
|
||||||
|
|
||||||
from sphinx import addnodes
|
from sphinx import addnodes
|
||||||
from sphinx.locale import _
|
from sphinx.locale import _
|
||||||
@ -339,6 +340,39 @@ class SphinxSmartQuotes(SmartQuotes, SphinxTransform):
|
|||||||
|
|
||||||
refs: sphinx.parsers.RSTParser
|
refs: sphinx.parsers.RSTParser
|
||||||
"""
|
"""
|
||||||
|
def apply(self):
|
||||||
|
# type: () -> None
|
||||||
|
if not self.is_available():
|
||||||
|
return
|
||||||
|
|
||||||
|
SmartQuotes.apply(self)
|
||||||
|
|
||||||
|
def is_available(self):
|
||||||
|
# type: () -> bool
|
||||||
|
builders = self.config.smartquotes_excludes.get('builders', [])
|
||||||
|
languages = self.config.smartquotes_excludes.get('languages', [])
|
||||||
|
|
||||||
|
if self.document.settings.smart_quotes is False:
|
||||||
|
# disabled by 3rd party extension (workaround)
|
||||||
|
return False
|
||||||
|
elif self.config.smartquotes is False:
|
||||||
|
# disabled by confval smartquotes
|
||||||
|
return False
|
||||||
|
elif self.app.builder.name in builders:
|
||||||
|
# disabled by confval smartquotes_excludes['builders']
|
||||||
|
return False
|
||||||
|
elif self.config.language in languages:
|
||||||
|
# disabled by confval smartquotes_excludes['languages']
|
||||||
|
return False
|
||||||
|
|
||||||
|
# confirm selected language supports smart_quotes or not
|
||||||
|
language = self.env.settings['language_code']
|
||||||
|
for tag in normalize_language_tag(language):
|
||||||
|
if tag in smartchars.quotes:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def smartquotes_action(self):
|
def smartquotes_action(self):
|
||||||
# type: () -> unicode
|
# type: () -> unicode
|
||||||
|
Loading…
Reference in New Issue
Block a user