MCPcopy
hub / github.com/Python-Markdown/markdown / run

Method run

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected