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

Method run

markdown/extensions/def_list.py:43–94  ·  view source on GitHub ↗
(self, parent: etree.Element, blocks: list[str])

Source from the content-addressed store, hash-verified

41 return bool(self.RE.search(block)) and not util.nearing_recursion_limit()
42
43 def run(self, parent: etree.Element, blocks: list[str]) -> bool | None:
44
45 raw_block = blocks.pop(0)
46 m = self.RE.search(raw_block)
47 terms = [term.strip() for term in
48 raw_block[:m.start()].split('\n') if term.strip()]
49 block = raw_block[m.end():]
50 no_indent = self.NO_INDENT_RE.match(block)
51 if no_indent:
52 d, theRest = (block, None)
53 else:
54 d, theRest = self.detab(block)
55 if d:
56 d = '{}\n{}'.format(m.group(2), d)
57 else:
58 d = m.group(2)
59 sibling = self.lastChild(parent)
60 if not terms and sibling is None:
61 # This is not a definition item. Most likely a paragraph that
62 # starts with a colon at the beginning of a document or list.
63 blocks.insert(0, raw_block)
64 return False
65 if not terms and sibling.tag == 'p':
66 # The previous paragraph contains the terms
67 state = 'looselist'
68 terms = sibling.text.split('\n')
69 parent.remove(sibling)
70 # Acquire new sibling
71 sibling = self.lastChild(parent)
72 else:
73 state = 'list'
74
75 if sibling is not None and sibling.tag == 'dl':
76 # This is another item on an existing list
77 dl = sibling
78 if not terms and len(dl) and dl[-1].tag == 'dd' and len(dl[-1]):
79 state = 'looselist'
80 else:
81 # This is a new list
82 dl = etree.SubElement(parent, 'dl')
83 # Add terms
84 for term in terms:
85 dt = etree.SubElement(dl, 'dt')
86 dt.text = term
87 # Add definition
88 self.parser.state.set(state)
89 dd = etree.SubElement(dl, 'dd')
90 self.parser.parseBlocks(dd, [d])
91 self.parser.state.reset()
92
93 if theRest:
94 blocks.insert(0, theRest)
95
96
97class DefListIndentProcessor(ListIndentProcessor):

Callers

nothing calls this directly

Calls 7

searchMethod · 0.80
lastChildMethod · 0.80
removeMethod · 0.80
setMethod · 0.80
parseBlocksMethod · 0.80
detabMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected