(self, doc: etree.Element)
| 363 | return div |
| 364 | |
| 365 | def run(self, doc: etree.Element) -> None: |
| 366 | # Get a list of id attributes |
| 367 | used_ids = set() |
| 368 | for el in doc.iter(): |
| 369 | if "id" in el.attrib: |
| 370 | used_ids.add(el.attrib["id"]) |
| 371 | |
| 372 | toc_tokens = [] |
| 373 | for el in doc.iter(): |
| 374 | if isinstance(el.tag, str) and self.header_rgx.match(el.tag): |
| 375 | self.set_level(el) |
| 376 | innerhtml = render_inner_html(remove_fnrefs(el), self.md) |
| 377 | name = strip_tags(innerhtml) |
| 378 | |
| 379 | # Do not override pre-existing ids |
| 380 | if "id" not in el.attrib: |
| 381 | el.attrib["id"] = unique(self.slugify(html.unescape(name), self.sep), used_ids) |
| 382 | |
| 383 | data_toc_label = '' |
| 384 | if 'data-toc-label' in el.attrib: |
| 385 | data_toc_label = run_postprocessors(unescape(el.attrib['data-toc-label']), self.md) |
| 386 | # Overwrite name with sanitized value of `data-toc-label`. |
| 387 | name = escape_cdata(strip_tags(data_toc_label)) |
| 388 | # Remove the data-toc-label attribute as it is no longer needed |
| 389 | del el.attrib['data-toc-label'] |
| 390 | |
| 391 | if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom: |
| 392 | toc_tokens.append({ |
| 393 | 'level': int(el.tag[-1]), |
| 394 | 'id': unescape(el.attrib["id"]), |
| 395 | 'name': name, |
| 396 | 'html': innerhtml, |
| 397 | 'data-toc-label': data_toc_label |
| 398 | }) |
| 399 | |
| 400 | if self.use_anchors: |
| 401 | self.add_anchor(el, el.attrib["id"]) |
| 402 | if self.use_permalinks not in [False, None]: |
| 403 | self.add_permalink(el, el.attrib["id"]) |
| 404 | |
| 405 | toc_tokens = nest_toc_tokens(toc_tokens) |
| 406 | div = self.build_toc_div(toc_tokens) |
| 407 | if self.marker: |
| 408 | self.replace_marker(doc, div) |
| 409 | |
| 410 | # serialize and attach to markdown instance. |
| 411 | toc = self.md.serializer(div) |
| 412 | for pp in self.md.postprocessors: |
| 413 | toc = pp.run(toc) |
| 414 | self.md.toc_tokens = toc_tokens |
| 415 | self.md.toc = toc |
| 416 | |
| 417 | |
| 418 | class TocExtension(Extension): |
no test coverage detected