From 7f8f3f86b6153584a7aa96a4fe036db626d879cc Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sat, 20 Feb 2010 14:45:39 +0100 Subject: [PATCH] Get rid of the "module" node. --- sphinx/addnodes.py | 3 --- sphinx/domains/python.py | 8 ++----- sphinx/environment.py | 10 +++++---- sphinx/writers/html.py | 5 ----- sphinx/writers/latex.py | 47 +++++++++++++++------------------------- sphinx/writers/text.py | 7 ------ 6 files changed, 26 insertions(+), 54 deletions(-) diff --git a/sphinx/addnodes.py b/sphinx/addnodes.py index 2b62633f3..0a2f0f7f8 100644 --- a/sphinx/addnodes.py +++ b/sphinx/addnodes.py @@ -90,9 +90,6 @@ class abbreviation(nodes.Inline, nodes.TextElement): pass # glossary class glossary(nodes.Element): pass -# module declaration -class module(nodes.Element): pass - # start of a file, used in the LaTeX builder only class start_of_file(nodes.Element): pass diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index dfa40798e..fcd2216db 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -351,16 +351,12 @@ class PyModule(Directive): env.domaindata['py']['modules'][modname] = \ (env.docname, self.options.get('synopsis', ''), self.options.get('platform', ''), 'deprecated' in self.options) - modulenode = addnodes.module() - modulenode['modname'] = modname - modulenode['synopsis'] = self.options.get('synopsis', '') targetnode = nodes.target('', '', ids=['module-' + modname], ismod=True) self.state.document.note_explicit_target(targetnode) - ret = [modulenode, targetnode] + ret = [targetnode] # XXX this behavior of the module directive is a mess... if 'platform' in self.options: platform = self.options['platform'] - modulenode['platform'] = platform node = nodes.paragraph() node += nodes.emphasis('', _('Platforms: ')) node += nodes.Text(platform, platform) @@ -371,7 +367,7 @@ class PyModule(Directive): indextext = _('%s (module)') % modname inode = addnodes.index(entries=[('single', indextext, 'module-' + modname, modname)]) - ret.insert(0, inode) + ret.append(inode) return ret diff --git a/sphinx/environment.py b/sphinx/environment.py index f03c49562..c56683088 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -109,7 +109,8 @@ class DefaultSubstitutions(Transform): class MoveModuleTargets(Transform): """ - Move module targets to their nearest enclosing section title. + Move module targets that are the first thing in a section to the section + title. XXX Python specific """ @@ -119,9 +120,10 @@ class MoveModuleTargets(Transform): for node in self.document.traverse(nodes.target): if not node['ids']: continue - if node['ids'][0].startswith('module-') and \ - node.parent.__class__ is nodes.section and \ - node.has_key('ismod'): + 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): node.parent['ids'][0:0] = node['ids'] node.parent.remove(node) diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 1ca61254f..2db4aeaa8 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -352,11 +352,6 @@ class HTMLTranslator(BaseTranslator): def depart_acks(self, node): pass - def visit_module(self, node): - pass - def depart_module(self, node): - pass - def visit_hlist(self, node): self.body.append('') def depart_hlist(self, node): diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index c6dcf3bd2..4dc29bcff 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -220,7 +220,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.top_sectionlevel = 0 else: self.top_sectionlevel = 1 - self.next_section_target = None + self.next_section_ids = [] # flags self.verbatim = None self.in_title = 0 @@ -240,14 +240,14 @@ class LaTeXTranslator(nodes.NodeVisitor): self.generate_indices() + FOOTER % self.elements) - def hypertarget(self, target, text='', anchor=True): - #return '\\hypertarget{%s}{%s}' % (self.idescape(target), text) + def hypertarget(self, id, text='', anchor=True): + #return '\\hypertarget{%s}{%s}' % (self.idescape(id), text) return (anchor and '\\phantomsection' or '') + \ - '\\label{%s}%s' % (self.idescape(target), text) + '\\label{%s}%s' % (self.idescape(id), text) - def hyperlink(self, target): - #return '\\hyperlink{%s}{' % (self.idescape(target)) - return '\\hyperref[%s]{' % (self.idescape(target)) + def hyperlink(self, id): + #return '\\hyperlink{%s}{' % (self.idescape(id)) + return '\\hyperref[%s]{' % (self.idescape(id)) def idescape(self, id): return str(unicode(id).translate(tex_replace_map)) @@ -362,7 +362,8 @@ class LaTeXTranslator(nodes.NodeVisitor): if not self.this_is_the_title: self.sectionlevel += 1 self.body.append('\n\n') - #if node.get('ids'): + if node.get('ids'): + self.next_section_ids.extend(node['ids']) # for id in node['ids']: # if id not in self.written_ids: # self.body.append(self.hypertarget(id)) @@ -438,10 +439,12 @@ class LaTeXTranslator(nodes.NodeVisitor): self.body.append(r'\%s{' % self.sectionnames[-1]) self.context.append('}\n') - if self.next_section_target: - self.context[-1] += self.hypertarget(self.next_section_target, - anchor=False) - self.next_section_target = None + if self.next_section_ids: + for id in self.next_section_ids: + if id not in self.written_ids: + self.context[-1] += self.hypertarget(id, anchor=False) + self.written_ids.add(id) + self.next_section_ids = [] elif isinstance(parent, (nodes.topic, nodes.sidebar)): self.body.append(r'\textbf{') @@ -800,19 +803,6 @@ class LaTeXTranslator(nodes.NodeVisitor): def depart_hlistcol(self, node): pass - def visit_module(self, node): - modname = node['modname'] - self.body.append('\n' + - self.hypertarget('module-' + modname.replace(' ',''))) - #self.body.append('\n\\declaremodule[%s]{}{%s}' % ( - # modname.replace('_', ''), self.encode(modname))) - #self.body.append('\n\\modulesynopsis{%s}' % - # self.encode(node['synopsis'])) - #if node.has_key('platform'): - # self.body.append('\\platform{%s}' % self.encode(node['platform'])) - def depart_module(self, node): - pass - def latex_image_length(self, width_str): match = re.match('(\d*\.?\d*)\s*(\S*)', width_str) if not match: @@ -976,7 +966,7 @@ class LaTeXTranslator(nodes.NodeVisitor): next = node.parent[parindex+1] if isinstance(next, nodes.section): # postpone the label until after the sectioning command - self.next_section_target = node['refid'] + self.next_section_ids.append(node['refid']) return except IndexError: pass @@ -1035,9 +1025,8 @@ class LaTeXTranslator(nodes.NodeVisitor): elif uri.startswith('%'): # references to documents or labels inside documents hashindex = uri.find('#') - targetname = (hashindex == -1) and '--doc-' + uri[1:] \ - or uri[hashindex+1:] - self.body.append(self.hyperlink(targetname)) + id = (hashindex == -1) and '--doc-' + uri[1:] or uri[hashindex+1:] + self.body.append(self.hyperlink(id)) self.context.append('}') elif uri.startswith('@token'): if self.in_production_list: diff --git a/sphinx/writers/text.py b/sphinx/writers/text.py index a7e247ce7..a60e821ad 100644 --- a/sphinx/writers/text.py +++ b/sphinx/writers/text.py @@ -160,13 +160,6 @@ class TextTranslator(nodes.NodeVisitor): def depart_attribution(self, node): pass - def visit_module(self, node): - if node.has_key('platform'): - self.new_state(0) - self.add_text(_('Platform: %s') % node['platform']) - self.end_state() - raise nodes.SkipNode - def visit_desc(self, node): pass def depart_desc(self, node):