Merge pull request #5763 from tk0miya/refactor_SphinxTranslator.get_settings

Remove SphinxTranslator.get_settings()
This commit is contained in:
Takeshi KOMIYA 2018-12-12 18:19:39 +09:00 committed by GitHub
commit 6d1b68230c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 37 deletions

View File

@ -398,16 +398,7 @@ class SphinxTranslator(nodes.NodeVisitor):
super(SphinxTranslator, self).__init__(document) super(SphinxTranslator, self).__init__(document)
self.builder = builder self.builder = builder
self.config = builder.config self.config = builder.config
self.settings = document.settings
def get_settings(self):
# type: () -> Any
"""Get settings object with type safe.
.. note:: It is hard to check types for settings object because it's attributes
are added dynamically. This method avoids the type errors through
imitating it's type as Any.
"""
return self.document.settings
# cache a vanilla instance of nodes.document # cache a vanilla instance of nodes.document

View File

@ -252,8 +252,7 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator):
atts['class'] += ' external' atts['class'] += ' external'
if 'refuri' in node: if 'refuri' in node:
atts['href'] = node['refuri'] or '#' atts['href'] = node['refuri'] or '#'
if (self.get_settings().cloak_email_addresses and if self.settings.cloak_email_addresses and atts['href'].startswith('mailto:'):
atts['href'].startswith('mailto:')):
atts['href'] = self.cloak_mailto(atts['href']) atts['href'] = self.cloak_mailto(atts['href'])
self.in_mailto = True self.in_mailto = True
else: else:
@ -710,7 +709,7 @@ class HTMLTranslator(SphinxTranslator, BaseTranslator):
# protect runs of multiple spaces; the last one can wrap # protect runs of multiple spaces; the last one can wrap
self.body.append(' ' * (len(token) - 1) + ' ') self.body.append(' ' * (len(token) - 1) + ' ')
else: else:
if self.in_mailto and self.get_settings().cloak_email_addresses: if self.in_mailto and self.settings.cloak_email_addresses:
encoded = self.cloak_email(encoded) encoded = self.cloak_email(encoded)
self.body.append(encoded) self.body.append(encoded)

View File

@ -221,8 +221,7 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
atts['class'] += ' external' atts['class'] += ' external'
if 'refuri' in node: if 'refuri' in node:
atts['href'] = node['refuri'] or '#' atts['href'] = node['refuri'] or '#'
if (self.get_settings().cloak_email_addresses and if self.settings.cloak_email_addresses and atts['href'].startswith('mailto:'):
atts['href'].startswith('mailto:')):
atts['href'] = self.cloak_mailto(atts['href']) atts['href'] = self.cloak_mailto(atts['href'])
self.in_mailto = True self.in_mailto = True
else: else:
@ -639,7 +638,7 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
# protect runs of multiple spaces; the last one can wrap # protect runs of multiple spaces; the last one can wrap
self.body.append(' ' * (len(token) - 1) + ' ') self.body.append(' ' * (len(token) - 1) + ' ')
else: else:
if self.in_mailto and self.get_settings().cloak_email_addresses: if self.in_mailto and self.settings.cloak_email_addresses:
encoded = self.cloak_email(encoded) encoded = self.cloak_email(encoded)
self.body.append(encoded) self.body.append(encoded)
@ -777,8 +776,7 @@ class HTML5Translator(SphinxTranslator, BaseTranslator):
self._table_row_index = 0 self._table_row_index = 0
classes = [cls.strip(u' \t\n') classes = [cls.strip(u' \t\n') for cls in self.settings.table_style.split(',')]
for cls in self.get_settings().table_style.split(',')]
classes.insert(0, "docutils") # compat classes.insert(0, "docutils") # compat
if 'align' in node: if 'align' in node:
classes.append('align-%s' % node['align']) classes.append('align-%s' % node['align'])

View File

@ -534,12 +534,12 @@ class LaTeXTranslator(SphinxTranslator):
self.elements = self.builder.context.copy() self.elements = self.builder.context.copy()
# but some have other interface in config file # but some have other interface in config file
self.elements['wrapperclass'] = self.format_docclass(self.get_settings().docclass) self.elements['wrapperclass'] = self.format_docclass(self.settings.docclass)
# we assume LaTeX class provides \chapter command except in case # we assume LaTeX class provides \chapter command except in case
# of non-Japanese 'howto' case # of non-Japanese 'howto' case
self.sectionnames = LATEXSECTIONNAMES[:] self.sectionnames = LATEXSECTIONNAMES[:]
if self.get_settings().docclass == 'howto': if self.settings.docclass == 'howto':
docclass = self.config.latex_docclass.get('howto', 'article') docclass = self.config.latex_docclass.get('howto', 'article')
if docclass[0] == 'j': # Japanese class... if docclass[0] == 'j': # Japanese class...
pass pass
@ -684,7 +684,7 @@ class LaTeXTranslator(SphinxTranslator):
self.elements['secnumdepth'] = '\\setcounter{secnumdepth}{%d}' %\ self.elements['secnumdepth'] = '\\setcounter{secnumdepth}{%d}' %\
minsecnumdepth minsecnumdepth
contentsname = self.get_settings().contentsname contentsname = self.settings.contentsname
self.elements['contentsname'] = self.babel_renewcommand('\\contentsname', self.elements['contentsname'] = self.babel_renewcommand('\\contentsname',
contentsname) contentsname)

View File

@ -96,13 +96,12 @@ class ManualPageTranslator(SphinxTranslator, BaseTranslator):
self.section_level = -1 self.section_level = -1
# docinfo set by man_pages config value # docinfo set by man_pages config value
settings = self.get_settings() self._docinfo['title'] = self.settings.title
self._docinfo['title'] = settings.title self._docinfo['subtitle'] = self.settings.subtitle
self._docinfo['subtitle'] = settings.subtitle if self.settings.authors:
if settings.authors:
# don't set it if no author given # don't set it if no author given
self._docinfo['author'] = settings.authors self._docinfo['author'] = self.settings.authors
self._docinfo['manual_section'] = settings.section self._docinfo['manual_section'] = self.settings.section
# docinfo set by other config values # docinfo set by other config values
self._docinfo['title_upper'] = self._docinfo['title'].upper() self._docinfo['title_upper'] = self._docinfo['title'].upper()

View File

@ -231,14 +231,13 @@ class TexinfoTranslator(SphinxTranslator):
def init_settings(self): def init_settings(self):
# type: () -> None # type: () -> None
self.settings = settings = self.get_settings()
elements = self.elements = self.default_elements.copy() elements = self.elements = self.default_elements.copy()
elements.update({ elements.update({
# if empty, the title is set to the first section title # if empty, the title is set to the first section title
'title': settings.title, 'title': self.settings.title,
'author': settings.author, 'author': self.settings.author,
# if empty, use basename of input file # if empty, use basename of input file
'filename': settings.texinfo_filename, 'filename': self.settings.texinfo_filename,
'release': self.escape(self.builder.config.release), 'release': self.escape(self.builder.config.release),
'project': self.escape(self.builder.config.project), 'project': self.escape(self.builder.config.project),
'copyright': self.escape(self.builder.config.copyright), 'copyright': self.escape(self.builder.config.copyright),
@ -247,7 +246,7 @@ class TexinfoTranslator(SphinxTranslator):
language=self.builder.config.language)) language=self.builder.config.language))
}) })
# title # title
title = settings.title # type: unicode title = self.settings.title # type: unicode
if not title: if not title:
title_node = self.document.next_node(nodes.title) title_node = self.document.next_node(nodes.title)
title = (title and title_node.astext()) or '<untitled>' title = (title and title_node.astext()) or '<untitled>'
@ -259,19 +258,19 @@ class TexinfoTranslator(SphinxTranslator):
elements['filename'] = elements['filename'][:-4] # type: ignore elements['filename'] = elements['filename'][:-4] # type: ignore
elements['filename'] += '.info' # type: ignore elements['filename'] += '.info' # type: ignore
# direntry # direntry
if settings.texinfo_dir_entry: if self.settings.texinfo_dir_entry:
entry = self.format_menu_entry( entry = self.format_menu_entry(
self.escape_menu(settings.texinfo_dir_entry), self.escape_menu(self.settings.texinfo_dir_entry),
'(%s)' % elements['filename'], '(%s)' % elements['filename'],
self.escape_arg(settings.texinfo_dir_description)) self.escape_arg(self.settings.texinfo_dir_description))
elements['direntry'] = ('@dircategory %s\n' elements['direntry'] = ('@dircategory %s\n'
'@direntry\n' '@direntry\n'
'%s' '%s'
'@end direntry\n') % ( '@end direntry\n') % (
self.escape_id(settings.texinfo_dir_category), entry) self.escape_id(self.settings.texinfo_dir_category), entry)
elements['copying'] = COPYING % elements elements['copying'] = COPYING % elements
# allow the user to override them all # allow the user to override them all
elements.update(settings.texinfo_elements) elements.update(self.settings.texinfo_elements)
def collect_node_names(self): def collect_node_names(self):
# type: () -> None # type: () -> None