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

Class OListProcessor

markdown/blockprocessors.py:331–451  ·  view source on GitHub ↗

Process ordered list blocks.

Source from the content-addressed store, hash-verified

329
330
331class OListProcessor(BlockProcessor):
332 """ Process ordered list blocks. """
333
334 TAG: str = 'ol'
335 """ The tag used for the wrapping element. """
336 STARTSWITH: str = '1'
337 """
338 The integer (as a string ) with which the list starts. For example, if a list is initialized as
339 `3. Item`, then the `ol` tag will be assigned an HTML attribute of `starts="3"`. Default: `"1"`.
340 """
341 LAZY_OL: bool = True
342 """ Ignore `STARTSWITH` if `True`. """
343 SIBLING_TAGS: list[str] = ['ol', 'ul']
344 """
345 Markdown does not require the type of a new list item match the previous list item type.
346 This is the list of types which can be mixed.
347 """
348
349 def __init__(self, parser: BlockParser):
350 super().__init__(parser)
351 # Detect an item (`1. item`). `group(1)` contains contents of item.
352 self.RE = re.compile(r'^[ ]{0,%d}\d+\.[ ]+(.*)' % (self.tab_length - 1))
353 # Detect items on secondary lines. they can be of either list type.
354 self.CHILD_RE = re.compile(r'^[ ]{0,%d}((\d+\.)|[*+-])[ ]+(.*)' %
355 (self.tab_length - 1))
356 # Detect indented (nested) items of either type
357 self.INDENT_RE = re.compile(r'^[ ]{%d,%d}((\d+\.)|[*+-])[ ]+.*' %
358 (self.tab_length, self.tab_length * 2 - 1))
359
360 def test(self, parent: etree.Element, block: str) -> bool:
361 # As in `ListIndentProcessor`, nesting stops short of the recursion limit.
362 # `UListProcessor` inherits this method, so unordered lists are covered too.
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()

Callers 1

build_block_parserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected