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

Class PrettifyTreeprocessor

markdown/treeprocessors.py:426–462  ·  view source on GitHub ↗

Add line breaks to the html document.

Source from the content-addressed store, hash-verified

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

Callers 1

build_treeprocessorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected