Commits

Georg Brandl  committed 4e79955

Section headings in :rst:dir:`only` directives are now correctly handled.

  • Participants
  • Parent commits 97f8346

Comments (0)

Files changed (3)

 
 * Added :confval:`pngmath_add_tooltips`.
 
+* Section headings in :rst:dir:`only` directives are now correctly
+  handled.
+
 * C++ domain now supports array definitions.
 
 

File sphinx/builders/html.py

         meta = self.env.metadata.get(docname)
 
         # local TOC and global TOC tree
-        toc = self.render_partial(self.env.get_toc_for(docname))['fragment']
+        self_toc = self.env.get_toc_for(docname, self)
+        toc = self.render_partial(self_toc)['fragment']
 
         return dict(
             parents = parents,

File sphinx/environment.py

                 # find all toctree nodes in this section and add them
                 # to the toc (just copying the toctree node which is then
                 # resolved in self.get_and_resolve_doctree)
+                if isinstance(sectionnode, addnodes.only):
+                    onlynode = addnodes.only(expr=sectionnode['expr'])
+                    blist = build_toc(sectionnode, depth)
+                    if blist:
+                        onlynode += blist.children
+                        entries.append(onlynode)
                 if not isinstance(sectionnode, nodes.section):
                     for toctreenode in traverse_in_section(sectionnode,
                                                            addnodes.toctree):
                 else:
                     anchorname = '#' + sectionnode['ids'][0]
                 numentries[0] += 1
+                # make these nodes:
+                # list_item -> compact_paragraph -> reference
                 reference = nodes.reference(
                     '', '', internal=True, refuri=docname,
                     anchorname=anchorname, *nodetext)
             self.tocs[docname] = nodes.bullet_list('')
         self.toc_num_entries[docname] = numentries[0]
 
-    def get_toc_for(self, docname):
+    def get_toc_for(self, docname, builder):
         """Return a TOC nodetree -- for use on the same page only!"""
         toc = self.tocs[docname].deepcopy()
+        self.process_only_nodes(toc, builder, docname)
         for node in toc.traverse(nodes.reference):
             node['refuri'] = node['anchorname'] or '#'
         return toc
                         toc = nodes.bullet_list('', item)
                     else:
                         toc = self.tocs[ref].deepcopy()
+                        self.process_only_nodes(toc, builder, ref)
                         if title and toc.children and len(toc.children) == 1:
                             child = toc.children[0]
                             for refnode in child.traverse(nodes.reference):
             node.replace_self(newnode or contnode)
 
         # remove only-nodes that do not belong to our builder
-        self.process_only_nodes(doctree, fromdocname, builder)
+        self.process_only_nodes(doctree, builder, fromdocname)
 
         # allow custom references to be resolved
         builder.app.emit('doctree-resolved', doctree, fromdocname)
             msg = '%s reference target not found: %%(target)s' % typ
         self.warn(refdoc, msg % {'target': target}, node.line)
 
-    def process_only_nodes(self, doctree, fromdocname, builder):
+    def process_only_nodes(self, doctree, builder, fromdocname=None):
         for node in doctree.traverse(addnodes.only):
             try:
                 ret = builder.tags.eval_condition(node['expr'])
                 elif isinstance(subnode, nodes.list_item):
                     _walk_toc(subnode, secnums, depth, titlenode)
                     titlenode = None
+                elif isinstance(subnode, addnodes.only):
+                    # at this stage we don't know yet which sections are going
+                    # to be included; just include all of them, even if it leads
+                    # to gaps in the numbering
+                    _walk_toc(subnode, secnums, depth, titlenode)
+                    titlenode = None
                 elif isinstance(subnode, addnodes.compact_paragraph):
                     numstack[-1] += 1
                     if depth > 0: