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

Class PrettifyTreeprocessor

markdown/treeprocessors.py:420–456  ·  view source on GitHub ↗

Add line breaks to the html document.

Source from the content-addressed store, hash-verified

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

Callers 1

build_treeprocessorsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected