Georg Brandl avatar Georg Brandl committed 095db1a

Allow "self" in toctrees, as a reference to the document containing the directive.

Comments (0)

Files changed (2)

sphinx/directives/other.py

                 docname = docname[:-len(suffix)]
             # absolutize filenames
             docname = docname_join(env.docname, docname)
-            if url_re.match(ref):
+            if url_re.match(ref) or ref == 'self':
                 entries.append((title, ref))
             elif docname not in env.found_docs:
                 ret.append(state.document.reporter.warning(
                     'toctree glob pattern %r didn\'t match any documents'
                     % entry, line=lineno))
     subnode = addnodes.toctree()
+    subnode['parent'] = env.docname
     subnode['entries'] = entries
     subnode['includefiles'] = includefiles
     subnode['maxdepth'] = options.get('maxdepth', -1)

sphinx/environment.py

                         para = addnodes.compact_paragraph('', '', reference)
                         item = nodes.list_item('', para)
                         toc = nodes.bullet_list('', item)
+                    elif ref == 'self':
+                        # 'self' refers to the document from which this toctree originates.
+                        ref = toctreenode['parent']
+                        if not title:
+                            title = self.titles[ref].astext()
+                        reference = nodes.reference('', '',
+                                                    refuri=ref,
+                                                    anchorname='',
+                                                    *[nodes.Text(title)])
+                        para = addnodes.compact_paragraph('', '', reference)
+                        item = nodes.list_item('', para)
+                        # Don't show subitems.
+                        toc = nodes.bullet_list('', item)
                     else:
                         toc = self.tocs[ref].deepcopy()
                         if title and toc.children and len(toc.children) == 1:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.