From 05caa0f0375c788a041d1fd47a174d56b255a131 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 19 Aug 2016 00:45:45 +0900 Subject: [PATCH] Fix #1953: ``Sphinx.add_node`` does not add handlers the translator installed by `html_translator_class` --- CHANGES | 2 ++ sphinx/application.py | 6 ++++++ sphinx/builders/html.py | 16 +++++----------- sphinx/config.py | 5 ++++- tests/test_api_translator.py | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CHANGES b/CHANGES index 0b2baed46..e0ec9b412 100644 --- a/CHANGES +++ b/CHANGES @@ -105,6 +105,8 @@ Bugs fixed module. Now Sphinx installs them on running application. * `sphinx.ext.autodoc` crashes if target code imports * from mock modules by `autodoc_mock_imports`. +* #1953: ``Sphinx.add_node`` does not add handlers the translator installed by + `html_translator_class` Documentation ------------- diff --git a/sphinx/application.py b/sphinx/application.py index d8cae8435..5466970a4 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -176,6 +176,12 @@ class Sphinx(object): 'This project needs at least Sphinx v%s and therefore cannot ' 'be built with this version.' % self.config.needs_sphinx) + # force preload html_translator_class + if self.config.html_translator_class: + translator_class = self.import_object(self.config.html_translator_class, + 'html_translator_class setting') + self.set_translator('html', translator_class) + # set confdir to srcdir if -C given (!= no confdir); a few pieces # of code expect a confdir to be set if self.confdir is None: diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index defee9bed..7703c165d 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -159,16 +159,11 @@ class StandaloneHTMLBuilder(Builder): self.config.trim_doctest_flags) def init_translator_class(self): - if self.translator_class is not None: - pass - elif self.config.html_translator_class: - self.translator_class = self.app.import_object( - self.config.html_translator_class, - 'html_translator_class setting') - elif self.config.html_use_smartypants: - self.translator_class = SmartyPantsHTMLTranslator - else: - self.translator_class = HTMLTranslator + if self.translator_class is None: + if self.config.html_use_smartypants: + self.translator_class = SmartyPantsHTMLTranslator + else: + self.translator_class = HTMLTranslator def get_outdated_docs(self): cfgdict = dict((name, self.config[name]) @@ -1209,7 +1204,6 @@ def setup(app): app.add_config_value('html_extra_path', [], 'html') app.add_config_value('html_last_updated_fmt', None, 'html', string_classes) app.add_config_value('html_use_smartypants', True, 'html') - app.add_config_value('html_translator_class', None, 'html', string_classes) app.add_config_value('html_sidebars', {}, 'html') app.add_config_value('html_additional_pages', {}, 'html') app.add_config_value('html_use_modindex', True, 'html') # deprecated diff --git a/sphinx/config.py b/sphinx/config.py index 9de59a9ff..f22d03be6 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -108,6 +108,9 @@ class Config(object): 'table': l_('Table %s'), 'code-block': l_('Listing %s')}, 'env'), + + # pre-initialized confval for HTML builder + html_translator_class = (None, 'html', string_classes), ) def __init__(self, dirname, filename, overrides, tags): @@ -221,7 +224,7 @@ class Config(object): def pre_init_values(self, warn): """Initialize some limited config variables before loading extensions""" - variables = ['needs_sphinx', 'suppress_warnings'] + variables = ['needs_sphinx', 'suppress_warnings', 'html_translator_class'] for name in variables: try: if name in self.overrides: diff --git a/tests/test_api_translator.py b/tests/test_api_translator.py index e8cfcb458..7a70fd4c8 100644 --- a/tests/test_api_translator.py +++ b/tests/test_api_translator.py @@ -57,7 +57,7 @@ def test_html_with_set_translator_for_html_(app, status, warning): @with_app('html', testroot='api-set-translator', - confoverrides={'html_translator_class': 'ext.ExtHTMLTranslator'}) + confoverrides={'html_translator_class': 'translator.ExtHTMLTranslator'}) def test_html_with_set_translator_for_html_and_html_translator_class( app, status, warning): # use set_translator() and html_translator_class.