class_docstr: Step through document and build TOC. 
class_docstr_tok: ["Step","through","document","and","build","TOC","."]
code: def run(self, doc: etree.Element) -> None:
      # Get a list of id attributes
      used_ids = set()
      for el in doc.iter():
          if "id" in el.attrib:
              used_ids.add(el.attrib["id"])
  
      toc_tokens = []
      for el in doc.iter():
          if isinstance(el.tag, str) and self.header_rgx.match(el.tag):
              self.set_level(el)
              text = get_name(el)
  
              # Do not override pre-existing ids
              if "id" not in el.attrib:
                  innertext = unescape(stashedHTML2text(text, self.md))
                  el.attrib["id"] = unique(
                      self.slugify(innertext, self.sep), used_ids
                  )
  
              if (
                  int(el.tag[-1]) >= self.toc_top
                  and int(el.tag[-1]) <= self.toc_bottom
              ):
                  toc_tokens.append(
                      {
                          "level": int(el.tag[-1]),
                          "id": el.attrib["id"],
                          "name": unescape(
                              stashedHTML2text(
                                  code_escape(
                                      el.attrib.get(
                                          "data-toc-label", text
                                      )
                                  ),
                                  self.md,
                                  strip_entities=False,
                              )
                          ),
                      }
                  )
  
              # Remove the data-toc-label attribute as it is no longer needed
              if "data-toc-label" in el.attrib:
                  del el.attrib["data-toc-label"]
  
              if self.use_anchors:
                  self.add_anchor(el, el.attrib["id"])
              if self.use_permalinks not in [False, None]:
                  self.add_permalink(el, el.attrib["id"])
  
      toc_tokens = nest_toc_tokens(toc_tokens)
      div = self.build_toc_div(toc_tokens)
      if self.marker:
          self.replace_marker(doc, div)
  
      # serialize and attach to markdown instance.
      toc = self.md.serializer(div)
      for pp in self.md.postprocessors:
          toc = pp.run(toc)
      self.md.toc_tokens = toc_tokens
      self.md.toc = toc
  
imports: from __future__ import annotations
  from .None import Extension
  from ..treeprocessors import Treeprocessor
  from ..util import code_escape, parseBoolValue, AMP_SUBSTITUTE, HTML_PLACEHOLDER_RE, AtomicString
  from ..treeprocessors import UnescapeTreeprocessor
  import re
  import html
  import unicodedata
  import xml.etree.ElementTree
  from typing import TYPE_CHECKING, Any, Iterator, MutableSet
label_desc: Use image node_id 8 for calling the TocTreeprocessor obj's underlying member method code with example usage: obj.run(doc) without return types