diff options
| author | Georg Brandl <georg@python.org> | 2015-01-02 11:12:35 +0100 |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2015-01-02 11:12:35 +0100 |
| commit | b7282d0506e16327f41c79fced3f870037052578 (patch) | |
| tree | 19f2f0f17cc82ac4a3c7abe933df405e8cbccc80 /sphinx/transforms.py | |
| parent | 912d1780970e5fa22f5bd92912ac02bc6110430e (diff) | |
| parent | 63137482d4ef4514195a4c1e51cebddbd9a2fa8a (diff) | |
| download | sphinx-b7282d0506e16327f41c79fced3f870037052578.tar.gz | |
Merge with stable.
Diffstat (limited to 'sphinx/transforms.py')
| -rw-r--r-- | sphinx/transforms.py | 93 |
1 files changed, 58 insertions, 35 deletions
diff --git a/sphinx/transforms.py b/sphinx/transforms.py index 0e103acf..a62fa99a 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -22,8 +22,6 @@ from sphinx.locale import _, init as init_locale from sphinx.util import split_index_msg from sphinx.util.nodes import traverse_translatable_index, extract_messages from sphinx.util.osutil import ustrftime, find_catalog -from sphinx.util.compat import docutils_version -from sphinx.util.pycompat import all from sphinx.domains.std import ( make_term_from_paragraph_node, make_termnodes_from_paragraph_node, @@ -36,6 +34,7 @@ default_substitutions = set([ 'today', ]) + class DefaultSubstitutions(Transform): """ Replace some substitutions if they aren't defined in the document. @@ -70,10 +69,10 @@ class MoveModuleTargets(Transform): for node in self.document.traverse(nodes.target): if not node['ids']: continue - if (node.has_key('ismod') and - node.parent.__class__ is nodes.section and - # index 0 is the section title node - node.parent.index(node) == 1): + if ('ismod' in node and + node.parent.__class__ is nodes.section and + # index 0 is the section title node + node.parent.index(node) == 1): node.parent['ids'][0:0] = node['ids'] node.parent.remove(node) @@ -88,10 +87,10 @@ class HandleCodeBlocks(Transform): # move doctest blocks out of blockquotes for node in self.document.traverse(nodes.block_quote): if all(isinstance(child, nodes.doctest_block) for child - in node.children): + in node.children): node.replace_self(node.children) # combine successive doctest blocks - #for node in self.document.traverse(nodes.doctest_block): + # for node in self.document.traverse(nodes.doctest_block): # if node not in node.parent.children: # continue # parindex = node.parent.index(node) @@ -102,6 +101,31 @@ class HandleCodeBlocks(Transform): # del node.parent[parindex+1] +class AutoNumbering(Transform): + """ + Register IDs of tables, figures and literal_blocks to assign numbers. + """ + default_priority = 210 + + def apply(self): + def has_child(node, cls): + return any(isinstance(child, cls) for child in node) + + for node in self.document.traverse(nodes.Element): + if isinstance(node, nodes.figure): + if has_child(node, nodes.caption): + self.document.note_implicit_target(node) + elif isinstance(node, nodes.image): + if has_child(node.parent, nodes.caption): + self.document.note_implicit_target(node.parent) + elif isinstance(node, nodes.table): + if has_child(node, nodes.title): + self.document.note_implicit_target(node) + elif isinstance(node, nodes.literal_block): + if has_child(node.parent, nodes.caption): + self.document.note_implicit_target(node.parent) + + class SortIds(Transform): """ Sort secion IDs so that the "id[0-9]+" one comes last. @@ -143,10 +167,7 @@ class CustomLocaleReporter(object): self.source, self.line = source, line def set_reporter(self, document): - if docutils_version < (0, 9): - document.reporter.locator = self.get_source_and_line - else: - document.reporter.get_source_and_line = self.get_source_and_line + document.reporter.get_source_and_line = self.get_source_and_line def get_source_and_line(self, lineno=None): return self.source, self.line @@ -178,7 +199,7 @@ class Locale(Transform): parser = RSTParser() - #phase1: replace reference ids with translated names + # phase1: replace reference ids with translated names for node, msg in extract_messages(self.document): msgstr = catalog.gettext(msg) # XXX add marker to untranslated parts @@ -203,7 +224,7 @@ class Locale(Transform): pass # XXX doctest and other block markup if not isinstance(patch, nodes.paragraph): - continue # skip for now + continue # skip for now processed = False # skip flag @@ -286,15 +307,14 @@ class Locale(Transform): node.children = patch.children node['translated'] = True - - #phase2: translation + # phase2: translation for node, msg in extract_messages(self.document): if node.get('translated', False): continue msgstr = catalog.gettext(msg) # XXX add marker to untranslated parts - if not msgstr or msgstr == msg: # as-of-yet untranslated + if not msgstr or msgstr == msg: # as-of-yet untranslated continue # Avoid "Literal block expected; none found." warnings. @@ -314,12 +334,13 @@ class Locale(Transform): pass # XXX doctest and other block markup if not isinstance(patch, nodes.paragraph): - continue # skip for now + continue # skip for now # auto-numbered foot note reference should use original 'ids'. def is_autonumber_footnote_ref(node): return isinstance(node, nodes.footnote_reference) and \ node.get('auto') == 1 + def list_replace_or_append(lst, old, new): if old in lst: lst[lst.index(old)] = new @@ -344,7 +365,7 @@ class Locale(Transform): for id in new['ids']: self.document.ids[id] = new list_replace_or_append( - self.document.autofootnote_refs, old, new) + self.document.autofootnote_refs, old, new) if refname: list_replace_or_append( self.document.footnote_refs.setdefault(refname, []), @@ -409,6 +430,7 @@ class Locale(Transform): if len(old_refs) != len(new_refs): env.warn_node('inconsistent term references in ' 'translated message', node) + def get_ref_key(node): case = node["refdomain"], node["reftype"] if case == ('std', 'term'): @@ -440,22 +462,23 @@ class Locale(Transform): node.children = patch.children node['translated'] = True - # Extract and translate messages for index entries. - for node, entries in traverse_translatable_index(self.document): - new_entries = [] - for type, msg, tid, main in entries: - msg_parts = split_index_msg(type, msg) - msgstr_parts = [] - for part in msg_parts: - msgstr = catalog.gettext(part) - if not msgstr: - msgstr = part - msgstr_parts.append(msgstr) - - new_entries.append((type, ';'.join(msgstr_parts), tid, main)) - - node['raw_entries'] = entries - node['entries'] = new_entries + if 'index' in env.config.gettext_enables: + # Extract and translate messages for index entries. + for node, entries in traverse_translatable_index(self.document): + new_entries = [] + for type, msg, tid, main in entries: + msg_parts = split_index_msg(type, msg) + msgstr_parts = [] + for part in msg_parts: + msgstr = catalog.gettext(part) + if not msgstr: + msgstr = part + msgstr_parts.append(msgstr) + + new_entries.append((type, ';'.join(msgstr_parts), tid, main)) + + node['raw_entries'] = entries + node['entries'] = new_entries class RemoveTranslatableInline(Transform): |
