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

Method run

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

Source from the content-addressed store, hash-verified

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