From 7165a4f1e474d68e20c8ec812ca59eb1a38e1b03 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sat, 27 May 2017 16:47:31 +0200 Subject: [PATCH] Fix #3788: Docutils emits warnings for unsupported languages This monkey-patches docutils.languages.get_language() to suppress Docutils emitted warning when the document settings use a language_code for which Docutils has no ready-made localisation. The language_code is needed for functioning of the smartquotes transform. --- CHANGES | 1 + sphinx/application.py | 10 ++++++++++ sphinx/environment/__init__.py | 16 ++++++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index d85606bcc..b8dd5169a 100644 --- a/CHANGES +++ b/CHANGES @@ -34,6 +34,7 @@ Bugs fixed * #3796: env.resolve_references() crashes when non-document node given * #3803: Sphinx crashes with invalid PO files * #3791: PDF "continued on next page" for long tables isn't internationalized +* #3788: smartquotes emits warnings for unsupported languages Testing -------- diff --git a/sphinx/application.py b/sphinx/application.py index 075f828fb..ba5aa2ca2 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -22,7 +22,9 @@ from collections import deque from six import iteritems from six.moves import cStringIO +import docutils from docutils import nodes +from docutils.languages import get_language as docutils_get_language from docutils.parsers.rst import directives, roles import sphinx @@ -106,6 +108,14 @@ ENV_PICKLE_FILENAME = 'environment.pickle' logger = logging.getLogger(__name__) +# monkey patch docutils get_language +def patched_docutils_get_language(language_code, reporter=None): # NOQA + return docutils_get_language(language_code) + + +docutils.languages.get_language = patched_docutils_get_language + + class Sphinx(object): def __init__(self, srcdir, confdir, outdir, doctreedir, buildername, diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index 8aefa64f9..dbade8d4e 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -25,7 +25,7 @@ from six.moves import cPickle as pickle from docutils.io import NullOutput from docutils.core import Publisher -from docutils.utils import Reporter, get_source_line +from docutils.utils import Reporter, get_source_line, normalize_language_tag from docutils.utils.smartquotes import smartchars from docutils.parsers.rst import roles from docutils.parsers.rst.languages import en as english @@ -672,18 +672,22 @@ class BuildEnvironment(object): self.settings['trim_footnote_reference_space'] = \ self.config.trim_footnote_reference_space self.settings['gettext_compact'] = self.config.gettext_compact - language = (self.config.language or 'en').replace('_', '-') + + language = self.config.language or 'en' self.settings['language_code'] = language + self.settings['smart_quotes'] = True if self.config.html_use_smartypants is not None: warnings.warn("html_use_smartypants option is deprecated. Smart " "quotes are on by default; if you want to disable " "or customize them, use the smart_quotes option in " "docutils.conf.", RemovedInSphinx17Warning) - if language in smartchars.quotes: - self.settings['smart_quotes'] = self.config.html_use_smartypants - elif language in smartchars.quotes: # We enable smartypants by default - self.settings['smart_quotes'] = True + self.settings['smart_quotes'] = self.config.html_use_smartypants + for tag in normalize_language_tag(language): + if tag in smartchars.quotes: + break + else: + self.settings['smart_quotes'] = False docutilsconf = path.join(self.srcdir, 'docutils.conf') # read docutils.conf from source dir, not from current dir