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

Class PrettifyTreeprocessor

markdown/treeprocessors.py:415–451  ·  view source on GitHub ↗

Add line breaks to the html document.

Source from the content-addressed store, hash-verified

413
414
415class PrettifyTreeprocessor(Treeprocessor):
416 """ Add line breaks to the html document. """
417
418 def _prettifyETree(self, elem: etree.Element) -> None:
419 """ Recursively add line breaks to `ElementTree` children. """
420
421 i = "\n"
422 if self.md.is_block_level(elem.tag) and elem.tag not in ['code', 'pre']:
423 if (not elem.text or not elem.text.strip()) \
424 and len(elem) and self.md.is_block_level(elem[0].tag):
425 elem.text = i
426 for e in elem:
427 if self.md.is_block_level(e.tag):
428 self._prettifyETree(e)
429 if not elem.tail or not elem.tail.strip():
430 elem.tail = i
431
432 def run(self, root: etree.Element) -> None:
433 """ Add line breaks to `Element` object and its children. """
434
435 self._prettifyETree(root)
436 # Do `<br />`'s separately as they are often in the middle of
437 # inline content and missed by `_prettifyETree`.
438 brs = root.iter('br')
439 for br in brs:
440 if not br.tail or not br.tail.strip():
441 br.tail = '\n'
442 else:
443 br.tail = '\n%s' % br.tail
444 # Clean up extra empty lines at end of code blocks.
445 pres = root.iter('pre')
446 for pre in pres:
447 if len(pre) and pre[0].tag == 'code':
448 code = pre[0]
449 # Only prettify code containing text only
450 if not len(code) and code.text is not None:
451 code.text = util.AtomicString(code.text.rstrip() + '\n')
452
453
454class UnescapeTreeprocessor(Treeprocessor):

Callers 1

build_treeprocessorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected