sane_lists.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. Sane List Extension for Python-Markdown
  3. =======================================
  4. Modify the behavior of Lists in Python-Markdown to act in a sane manor.
  5. See <https://Python-Markdown.github.io/extensions/sane_lists>
  6. for documentation.
  7. Original code Copyright 2011 [Waylan Limberg](http://achinghead.com)
  8. All changes Copyright 2011-2014 The Python Markdown Project
  9. License: [BSD](https://opensource.org/licenses/bsd-license.php)
  10. """
  11. from . import Extension
  12. from ..blockprocessors import OListProcessor, UListProcessor
  13. import re
  14. class SaneOListProcessor(OListProcessor):
  15. SIBLING_TAGS = ['ol']
  16. LAZY_OL = False
  17. def __init__(self, parser):
  18. super().__init__(parser)
  19. self.CHILD_RE = re.compile(r'^[ ]{0,%d}((\d+\.))[ ]+(.*)' %
  20. (self.tab_length - 1))
  21. class SaneUListProcessor(UListProcessor):
  22. SIBLING_TAGS = ['ul']
  23. def __init__(self, parser):
  24. super().__init__(parser)
  25. self.CHILD_RE = re.compile(r'^[ ]{0,%d}(([*+-]))[ ]+(.*)' %
  26. (self.tab_length - 1))
  27. class SaneListExtension(Extension):
  28. """ Add sane lists to Markdown. """
  29. def extendMarkdown(self, md):
  30. """ Override existing Processors. """
  31. md.parser.blockprocessors.register(SaneOListProcessor(md.parser), 'olist', 40)
  32. md.parser.blockprocessors.register(SaneUListProcessor(md.parser), 'ulist', 30)
  33. def makeExtension(**kwargs): # pragma: no cover
  34. return SaneListExtension(**kwargs)