MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / run

Method run

markdown/extensions/toc.py:387–438  ·  view source on GitHub ↗
(self, doc: etree.Element)

Source from the content-addressed store, hash-verified

385 return div
386
387 def run(self, doc: etree.Element) -> None:
388 # Get a list of id attributes
389 used_ids = set()
390 for el in doc.iter():
391 if "id" in el.attrib:
392 used_ids.add(el.attrib["id"])
393 id_counters: dict[str, int] = {}
394
395 toc_tokens = []
396 for el in doc.iter():
397 if isinstance(el.tag, str) and self.header_rgx.match(el.tag):
398 self.set_level(el)
399 innerhtml = render_inner_html(remove_fnrefs(el), self.md)
400 name = strip_tags(innerhtml)
401
402 # Do not override pre-existing ids
403 if "id" not in el.attrib:
404 el.attrib["id"] = unique(self.slugify(html.unescape(name), self.sep), used_ids, id_counters)
405
406 data_toc_label = ''
407 if 'data-toc-label' in el.attrib:
408 data_toc_label = run_postprocessors(unescape(el.attrib['data-toc-label']), self.md)
409 # Overwrite name with sanitized value of `data-toc-label`.
410 name = escape_cdata(strip_tags(data_toc_label))
411 # Remove the data-toc-label attribute as it is no longer needed
412 del el.attrib['data-toc-label']
413
414 if int(el.tag[-1]) >= self.toc_top and int(el.tag[-1]) <= self.toc_bottom:
415 toc_tokens.append({
416 'level': int(el.tag[-1]),
417 'id': unescape(el.attrib["id"]),
418 'name': name,
419 'html': innerhtml,
420 'data-toc-label': data_toc_label
421 })
422
423 if self.use_anchors:
424 self.add_anchor(el, el.attrib["id"])
425 if self.use_permalinks not in [False, None]:
426 self.add_permalink(el, el.attrib["id"])
427
428 toc_tokens = nest_toc_tokens(toc_tokens)
429 div = self.build_toc_div(toc_tokens)
430 if self.marker:
431 self.replace_marker(doc, div)
432
433 # serialize and attach to markdown instance.
434 toc = self.md.serializer(div)
435 for pp in self.md.postprocessors:
436 toc = pp.run(toc)
437 self.md.toc_tokens = toc_tokens
438 self.md.toc = toc
439
440
441class TocExtension(Extension):

Callers 2

run_postprocessorsFunction · 0.45
build_toc_divMethod · 0.45

Calls 15

set_levelMethod · 0.95
add_anchorMethod · 0.95
add_permalinkMethod · 0.95
build_toc_divMethod · 0.95
replace_markerMethod · 0.95
render_inner_htmlFunction · 0.85
remove_fnrefsFunction · 0.85
strip_tagsFunction · 0.85
uniqueFunction · 0.85
run_postprocessorsFunction · 0.85
unescapeFunction · 0.85
escape_cdataFunction · 0.85

Tested by

no test coverage detected