| 1 |
# Sane List Extension for Python-Markdown |
| 2 |
# ======================================= |
| 3 |
|
| 4 |
# Modify the behavior of Lists in Python-Markdown to act in a sane manor. |
| 5 |
|
| 6 |
# See https://Python-Markdown.github.io/extensions/sane_lists |
| 7 |
# for documentation. |
| 8 |
|
| 9 |
# Original code Copyright 2011 [Waylan Limberg](https://github.com/waylan) |
| 10 |
|
| 11 |
# All changes Copyright 2011-2014 The Python Markdown Project |
| 12 |
|
| 13 |
# License: [BSD](https://opensource.org/licenses/bsd-license.php) |
| 14 |
|
| 15 |
""" |
| 16 |
Modify the behavior of Lists in Python-Markdown to act in a sane manor. |
| 17 |
|
| 18 |
See [documentation](https://Python-Markdown.github.io/extensions/sane_lists) |
| 19 |
for details. |
| 20 |
""" |
| 21 |
|
| 22 |
from __future__ import annotations |
| 23 |
|
| 24 |
from . import Extension |
| 25 |
from ..blockprocessors import OListProcessor, UListProcessor |
| 26 |
import re |
| 27 |
from typing import TYPE_CHECKING |
| 28 |
|
| 29 |
if TYPE_CHECKING: # pragma: no cover |
| 30 |
from .. import blockparser |
| 31 |
|
| 32 |
|
| 33 |
class SaneOListProcessor(OListProcessor): |
| 34 |
""" Override `SIBLING_TAGS` to not include `ul` and set `LAZY_OL` to `False`. """ |
| 35 |
|
| 36 |
SIBLING_TAGS = ['ol'] |
| 37 |
""" Exclude `ul` from list of siblings. """ |
| 38 |
LAZY_OL = False |
| 39 |
""" Disable lazy list behavior. """ |
| 40 |
|
| 41 |
def __init__(self, parser: blockparser.BlockParser): |
| 42 |
super().__init__(parser) |
| 43 |
self.CHILD_RE = re.compile(r'^[ ]{0,%d}((\d+\.))[ ]+(.*)' % |
| 44 |
(self.tab_length - 1)) |
| 45 |
|
| 46 |
|
| 47 |
class SaneUListProcessor(UListProcessor): |
| 48 |
""" Override `SIBLING_TAGS` to not include `ol`. """ |
| 49 |
|
| 50 |
SIBLING_TAGS = ['ul'] |
| 51 |
""" Exclude `ol` from list of siblings. """ |
| 52 |
|
| 53 |
def __init__(self, parser: blockparser.BlockParser): |
| 54 |
super().__init__(parser) |
| 55 |
self.CHILD_RE = re.compile(r'^[ ]{0,%d}(([*+-]))[ ]+(.*)' % |
| 56 |
(self.tab_length - 1)) |
| 57 |
|
| 58 |
|
| 59 |
class SaneListExtension(Extension): |
| 60 |
""" Add sane lists to Markdown. """ |
| 61 |
|
| 62 |
def extendMarkdown(self, md): |
| 63 |
""" Register the processors. |
| 64 |
|
| 65 |
| Class Instance | Registry | Name | Priority | |
| 66 |
| ------------------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: | |
| 67 |
| [`SaneOListProcessor`][markdown.extensions.sane_lists.SaneOListProcessor] | [`blockprocessors`][markdown.blockprocessors.build_block_parser] | `olist` | `40` | |
| 68 |
| [`SaneUListProcessor`][markdown.extensions.sane_lists.SaneUListProcessor] | [`blockprocessors`][markdown.blockprocessors.build_block_parser] | `ulist` | `30` | |
| 69 |
|
| 70 |
""" |
| 71 |
# flake8: noqa: E501 65-68 |
| 72 |
md.parser.blockprocessors.register(SaneOListProcessor(md.parser), 'olist', 40) |
| 73 |
md.parser.blockprocessors.register(SaneUListProcessor(md.parser), 'ulist', 30) |
| 74 |
|
| 75 |
|
| 76 |
def makeExtension(**kwargs): # pragma: no cover |
| 77 |
return SaneListExtension(**kwargs) |