code: def document_toc(
      env: BuildEnvironment, docname: str, tags: Tags
  ) -> Node:
      """Get the (local) table of contents for a document.
  
      Note that this is only the sections within the document.
      For a ToC tree that shows the document's place in the
      ToC structure, use `get_toctree_for`.
      """
      tocdepth = env.metadata[docname].get("tocdepth", 0)
      try:
          toc = _toctree_copy(
              env.tocs[docname], 2, tocdepth, False, tags
          )
      except KeyError:
          # the document does not exist any more:
          # return a dummy node that renders to nothing
          return nodes.paragraph()
  
      for node in toc.findall(nodes.reference):
          node["refuri"] = node["anchorname"] or "#"
      return toc
  
imports: from __future__ import annotations
  from typing import TYPE_CHECKING, Any, TypeVar
  from docutils import nodes
  from docutils.nodes import Element, Node
  from sphinx import addnodes
  from sphinx.locale import __
  from sphinx.util import logging, url_re
  from sphinx.util.matching import Matcher
  from sphinx.util.nodes import _only_node_keep_children, clean_astext
label_desc: Use image node_id 2 for calling a global function with example usage: document_toc(env, docname, tags) and returns: toc, nodes