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

Method run

markdown/blockprocessors.py:359–416  ·  view source on GitHub ↗
(self, parent: etree.Element, blocks: list[str])

Source from the content-addressed store, hash-verified

357 return bool(self.RE.match(block))
358
359 def run(self, parent: etree.Element, blocks: list[str]) -> None:
360 # Check for multiple items in one block.
361 items = self.get_items(blocks.pop(0))
362 sibling = self.lastChild(parent)
363
364 if sibling is not None and sibling.tag in self.SIBLING_TAGS:
365 # Previous block was a list item, so set that as parent
366 lst = sibling
367 # make sure previous item is in a `p` - if the item has text,
368 # then it isn't in a `p`
369 if lst[-1].text:
370 # since it's possible there are other children for this
371 # sibling, we can't just `SubElement` the `p`, we need to
372 # insert it as the first item.
373 p = etree.Element('p')
374 p.text = lst[-1].text
375 lst[-1].text = ''
376 lst[-1].insert(0, p)
377 # if the last item has a tail, then the tail needs to be put in a `p`
378 # likely only when a header is not followed by a blank line
379 lch = self.lastChild(lst[-1])
380 if lch is not None and lch.tail:
381 p = etree.SubElement(lst[-1], 'p')
382 p.text = lch.tail.lstrip()
383 lch.tail = ''
384
385 # parse first block differently as it gets wrapped in a `p`.
386 li = etree.SubElement(lst, 'li')
387 self.parser.state.set('looselist')
388 firstitem = items.pop(0)
389 self.parser.parseBlocks(li, [firstitem])
390 self.parser.state.reset()
391 elif parent.tag in ['ol', 'ul']:
392 # this catches the edge case of a multi-item indented list whose
393 # first item is in a blank parent-list item:
394 # * * subitem1
395 # * subitem2
396 # see also `ListIndentProcessor`
397 lst = parent
398 else:
399 # This is a new list so create parent with appropriate tag.
400 lst = etree.SubElement(parent, self.TAG)
401 # Check if a custom start integer is set
402 if not self.LAZY_OL and self.STARTSWITH != '1':
403 lst.attrib['start'] = self.STARTSWITH
404
405 self.parser.state.set('list')
406 # Loop through items in block, recursively parsing each with the
407 # appropriate parent.
408 for item in items:
409 if item.startswith(' '*self.tab_length):
410 # Item is indented. Parse with last item as parent
411 self.parser.parseBlocks(lst[-1], [item])
412 else:
413 # New item. Create `li` and parse with it as parent
414 li = etree.SubElement(lst, 'li')
415 self.parser.parseBlocks(li, [item])
416 self.parser.state.reset()

Callers

nothing calls this directly

Calls 5

get_itemsMethod · 0.95
lastChildMethod · 0.80
setMethod · 0.80
parseBlocksMethod · 0.80
resetMethod · 0.45

Tested by

no test coverage detected