2024-03-03 17:15:23 +00:00
|
|
|
# Sane List Extension for Python-Markdown
|
|
|
|
# =======================================
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
# Modify the behavior of Lists in Python-Markdown to act in a sane manor.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
# See https://Python-Markdown.github.io/extensions/sane_lists
|
|
|
|
# for documentation.
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
# Original code Copyright 2011 [Waylan Limberg](http://achinghead.com)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
# All changes Copyright 2011-2014 The Python Markdown Project
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
# License: [BSD](https://opensource.org/licenses/bsd-license.php)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
"""
|
2024-03-03 17:15:23 +00:00
|
|
|
Modify the behavior of Lists in Python-Markdown to act in a sane manor.
|
|
|
|
|
|
|
|
See [documentation](https://Python-Markdown.github.io/extensions/sane_lists)
|
|
|
|
for details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
from . import Extension
|
|
|
|
from ..blockprocessors import OListProcessor, UListProcessor
|
|
|
|
import re
|
2024-03-03 17:15:23 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING: # pragma: no cover
|
|
|
|
from .. import blockparser
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SaneOListProcessor(OListProcessor):
|
2024-03-03 17:15:23 +00:00
|
|
|
""" Override `SIBLING_TAGS` to not include `ul` and set `LAZY_OL` to `False`. """
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
SIBLING_TAGS = ['ol']
|
2024-03-03 17:15:23 +00:00
|
|
|
""" Exclude `ul` from list of siblings. """
|
2022-01-24 04:07:52 +00:00
|
|
|
LAZY_OL = False
|
2024-03-03 17:15:23 +00:00
|
|
|
""" Disable lazy list behavior. """
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
def __init__(self, parser: blockparser.BlockParser):
|
2022-01-24 04:07:52 +00:00
|
|
|
super().__init__(parser)
|
2018-09-17 00:27:00 +00:00
|
|
|
self.CHILD_RE = re.compile(r'^[ ]{0,%d}((\d+\.))[ ]+(.*)' %
|
|
|
|
(self.tab_length - 1))
|
|
|
|
|
|
|
|
|
|
|
|
class SaneUListProcessor(UListProcessor):
|
2024-03-03 17:15:23 +00:00
|
|
|
""" Override `SIBLING_TAGS` to not include `ol`. """
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
SIBLING_TAGS = ['ul']
|
2024-03-03 17:15:23 +00:00
|
|
|
""" Exclude `ol` from list of siblings. """
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
def __init__(self, parser: blockparser.BlockParser):
|
2022-01-24 04:07:52 +00:00
|
|
|
super().__init__(parser)
|
2018-09-17 00:27:00 +00:00
|
|
|
self.CHILD_RE = re.compile(r'^[ ]{0,%d}(([*+-]))[ ]+(.*)' %
|
|
|
|
(self.tab_length - 1))
|
|
|
|
|
|
|
|
|
|
|
|
class SaneListExtension(Extension):
|
|
|
|
""" Add sane lists to Markdown. """
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def extendMarkdown(self, md):
|
2018-09-17 00:27:00 +00:00
|
|
|
""" Override existing Processors. """
|
2022-01-24 04:07:52 +00:00
|
|
|
md.parser.blockprocessors.register(SaneOListProcessor(md.parser), 'olist', 40)
|
|
|
|
md.parser.blockprocessors.register(SaneUListProcessor(md.parser), 'ulist', 30)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def makeExtension(**kwargs): # pragma: no cover
|
|
|
|
return SaneListExtension(**kwargs)
|