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

Class PrettifyTreeprocessor

markdown/treeprocessors.py:422–458  ·  view source on GitHub ↗

Add line breaks to the html document.

Source from the content-addressed store, hash-verified

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

Callers 1

build_treeprocessorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected