def index_toctree(app, pagename: str, startdepth: int, collapse: bool = True, **kwargs): """ Returns the "local" (starting at `startdepth`) TOC tree containing the current page, rendered as HTML bullet lists. This is the equivalent of `context["toctree"](**kwargs)` in sphinx templating, but using the startdepth-local instead of global TOC tree. """ # this is a variant of the function stored in `context["toctree"]`, which is # defined as `lambda **kwargs: self._get_local_toctree(pagename, **kwargs)` # with `self` being the HMTLBuilder and the `_get_local_toctree` basically # returning: # return self.render_partial(TocTree(self.env).get_toctree_for( # pagename, self, collapse, **kwargs))['fragment'] if "includehidden" not in kwargs: kwargs["includehidden"] = False if kwargs.get("maxdepth") == "": kwargs.pop("maxdepth") toctree = TocTree(app.env) ancestors = toctree.get_toctree_ancestors(pagename) try: indexname = ancestors[-startdepth] except IndexError: # eg for index.rst, but also special pages such as genindex, py-modindex, search # those pages don't have a "current" element in the toctree, so we can # directly return an empty string instead of using the default sphinx # toctree.get_toctree_for(pagename, app.builder, collapse, **kwargs) return "" toctree_element = _get_local_toctree_for( toctree, indexname, pagename, app.builder, collapse, **kwargs ) return app.builder.render_partial(toctree_element)["fragment"]
def index_link_node(app, fromdocname, refs): toc = TocTree(app.env) par_nodes = [] labels = { 'DEF': 'Définitions:', 'USE': 'Usages:', } for type in ['DEF', 'USE']: relevant_refs = [ref for ref in refs if ref['type'] == type] if not relevant_refs: continue par_node = nodes.paragraph(classes=["glossary-refs"]) par_node += nodes.strong(labels[type], labels[type]) for ref in refs: if ref['type'] != type: continue ref_node = nodes.reference('', '') docnames = toc.get_toctree_ancestors(ref['docname']) if not docnames: docnames = [ref['docname']] for i, docname in enumerate(reversed(docnames)): if i < len(docnames) - 1: for child in app.env.titles[docname].children: ref_node.append(child) ref_node.append(nodes.Text('/', '/')) else: strong_node = nodes.strong('', '') for child in app.env.titles[docname].children: strong_node.append(child) ref_node.append(strong_node) ref_node['refdocname'] = ref['docname'] ref_node['refuri'] = app.builder.get_relative_uri( fromdocname, ref['docname']) ref_node['refuri'] += "#" + ref['target_id'] par_node += ref_node par_nodes.append(par_node) return par_nodes