Skip to content

sane_lists

Sane List Extension for Python-Markdown.

Modify the behavior of Lists in Python-Markdown to act in a sane manor.

See https://pythonhosted.org/Markdown/extensions/sane_lists.html for documentation.

Original code Copyright 2011 Waylan Limberg

All changes Copyright 2011-2014 The Python Markdown Project

License: BSD

Classes

SaneOListProcessor(parser)

Bases: OListProcessor

Sane ordered list processor.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/sane_lists.py
def __init__(self, parser):
    super(SaneOListProcessor, self).__init__(parser)
    self.CHILD_RE = re.compile(r'^[ ]{0,%d}((\d+\.))[ ]+(.*)' %
                               (self.tab_length - 1))

Attributes

parser = parser instance-attribute
tab_length = parser.markdown.tab_length instance-attribute
TAG = 'ol' class-attribute instance-attribute
STARTSWITH = '1' class-attribute instance-attribute
RE = re.compile('^[ ]{0,%d}\\d+\\.[ ]+(.*)' % self.tab_length - 1) instance-attribute
INDENT_RE = re.compile('^[ ]{%d,%d}((\\d+\\.)|[*+-])[ ]+.*' % (self.tab_length, self.tab_length * 2 - 1)) instance-attribute
SIBLING_TAGS = ['ol'] class-attribute instance-attribute
CHILD_RE = re.compile('^[ ]{0,%d}((\\d+\\.))[ ]+(.*)' % self.tab_length - 1) instance-attribute

Functions

lastChild(parent)

Return the last child of an etree element.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def lastChild(self, parent):
    """Return the last child of an etree element."""
    if len(parent):
        return parent[-1]
    else:
        return None
detab(text)

Remove a tab from the front of each line of the given text.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def detab(self, text):
    """Remove a tab from the front of each line of the given text."""
    newtext = []
    lines = text.split('\n')
    for line in lines:
        if line.startswith(' '*self.tab_length):
            newtext.append(line[self.tab_length:])
        elif not line.strip():
            newtext.append('')
        else:
            break
    return '\n'.join(newtext), '\n'.join(lines[len(newtext):])
looseDetab(text, level=1)

Remove a tab from front of lines but allowing dedented lines.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def looseDetab(self, text, level=1):
    """Remove a tab from front of lines but allowing dedented lines."""
    lines = text.split('\n')
    for i in range(len(lines)):
        if lines[i].startswith(' '*self.tab_length*level):
            lines[i] = lines[i][self.tab_length*level:]
    return '\n'.join(lines)
test(parent, block)
Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def test(self, parent, block):
    return bool(self.RE.match(block))
run(parent, blocks)
Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def run(self, parent, blocks):
    # Check fr multiple items in one block.
    items = self.get_items(blocks.pop(0))
    sibling = self.lastChild(parent)

    if sibling is not None and sibling.tag in self.SIBLING_TAGS:
        # Previous block was a list item, so set that as parent
        lst = sibling
        # make sure previous item is in a p- if the item has text,
        # then it isn't in a p
        if lst[-1].text:
            # since it's possible there are other children for this
            # sibling, we can't just SubElement the p, we need to
            # insert it as the first item.
            p = util.etree.Element('p')
            p.text = lst[-1].text
            lst[-1].text = ''
            lst[-1].insert(0, p)
        # if the last item has a tail, then the tail needs to be put in a p
        # likely only when a header is not followed by a blank line
        lch = self.lastChild(lst[-1])
        if lch is not None and lch.tail:
            p = util.etree.SubElement(lst[-1], 'p')
            p.text = lch.tail.lstrip()
            lch.tail = ''

        # parse first block differently as it gets wrapped in a p.
        li = util.etree.SubElement(lst, 'li')
        self.parser.state.set('looselist')
        firstitem = items.pop(0)
        self.parser.parseBlocks(li, [firstitem])
        self.parser.state.reset()
    elif parent.tag in ['ol', 'ul']:
        # this catches the edge case of a multi-item indented list whose
        # first item is in a blank parent-list item:
        # * * subitem1
        #     * subitem2
        # see also ListIndentProcessor
        lst = parent
    else:
        # This is a new list so create parent with appropriate tag.
        lst = util.etree.SubElement(parent, self.TAG)
        # Check if a custom start integer is set
        if not self.parser.markdown.lazy_ol and self.STARTSWITH != '1':
            lst.attrib['start'] = self.STARTSWITH

    self.parser.state.set('list')
    # Loop through items in block, recursively parsing each with the
    # appropriate parent.
    for item in items:
        if item.startswith(' '*self.tab_length):
            # Item is indented. Parse with last item as parent
            self.parser.parseBlocks(lst[-1], [item])
        else:
            # New item. Create li and parse with it as parent
            li = util.etree.SubElement(lst, 'li')
            self.parser.parseBlocks(li, [item])
    self.parser.state.reset()
get_items(block)

Break a block into list items.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def get_items(self, block):
    """Break a block into list items."""
    items = []
    for line in block.split('\n'):
        m = self.CHILD_RE.match(line)
        if m:
            # This is a new list item
            # Check first item for the start index
            if not items and self.TAG == 'ol':
                # Detect the integer value of first list item
                INTEGER_RE = re.compile(r'(\d+)')
                self.STARTSWITH = INTEGER_RE.match(m.group(1)).group()
            # Append to the list
            items.append(m.group(3))
        elif self.INDENT_RE.match(line):
            # This is an indented (possibly nested) item.
            if items[-1].startswith(' '*self.tab_length):
                # Previous item was indented. Append to that item.
                items[-1] = '%s\n%s' % (items[-1], line)
            else:
                items.append(line)
        else:
            # This is another line of previous item. Append to that item.
            items[-1] = '%s\n%s' % (items[-1], line)
    return items

SaneUListProcessor(parser)

Bases: UListProcessor

Sane unordered list processor.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/sane_lists.py
def __init__(self, parser):
    super(SaneUListProcessor, self).__init__(parser)
    self.CHILD_RE = re.compile(r'^[ ]{0,%d}(([*+-]))[ ]+(.*)' %
                               (self.tab_length - 1))

Attributes

parser = parser instance-attribute
tab_length = parser.markdown.tab_length instance-attribute
TAG = 'ul' class-attribute instance-attribute
STARTSWITH = '1' class-attribute instance-attribute
RE = re.compile('^[ ]{0,%d}[*+-][ ]+(.*)' % self.tab_length - 1) instance-attribute
INDENT_RE = re.compile('^[ ]{%d,%d}((\\d+\\.)|[*+-])[ ]+.*' % (self.tab_length, self.tab_length * 2 - 1)) instance-attribute
SIBLING_TAGS = ['ul'] class-attribute instance-attribute
CHILD_RE = re.compile('^[ ]{0,%d}(([*+-]))[ ]+(.*)' % self.tab_length - 1) instance-attribute

Functions

lastChild(parent)

Return the last child of an etree element.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def lastChild(self, parent):
    """Return the last child of an etree element."""
    if len(parent):
        return parent[-1]
    else:
        return None
detab(text)

Remove a tab from the front of each line of the given text.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def detab(self, text):
    """Remove a tab from the front of each line of the given text."""
    newtext = []
    lines = text.split('\n')
    for line in lines:
        if line.startswith(' '*self.tab_length):
            newtext.append(line[self.tab_length:])
        elif not line.strip():
            newtext.append('')
        else:
            break
    return '\n'.join(newtext), '\n'.join(lines[len(newtext):])
looseDetab(text, level=1)

Remove a tab from front of lines but allowing dedented lines.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def looseDetab(self, text, level=1):
    """Remove a tab from front of lines but allowing dedented lines."""
    lines = text.split('\n')
    for i in range(len(lines)):
        if lines[i].startswith(' '*self.tab_length*level):
            lines[i] = lines[i][self.tab_length*level:]
    return '\n'.join(lines)
test(parent, block)
Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def test(self, parent, block):
    return bool(self.RE.match(block))
run(parent, blocks)
Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def run(self, parent, blocks):
    # Check fr multiple items in one block.
    items = self.get_items(blocks.pop(0))
    sibling = self.lastChild(parent)

    if sibling is not None and sibling.tag in self.SIBLING_TAGS:
        # Previous block was a list item, so set that as parent
        lst = sibling
        # make sure previous item is in a p- if the item has text,
        # then it isn't in a p
        if lst[-1].text:
            # since it's possible there are other children for this
            # sibling, we can't just SubElement the p, we need to
            # insert it as the first item.
            p = util.etree.Element('p')
            p.text = lst[-1].text
            lst[-1].text = ''
            lst[-1].insert(0, p)
        # if the last item has a tail, then the tail needs to be put in a p
        # likely only when a header is not followed by a blank line
        lch = self.lastChild(lst[-1])
        if lch is not None and lch.tail:
            p = util.etree.SubElement(lst[-1], 'p')
            p.text = lch.tail.lstrip()
            lch.tail = ''

        # parse first block differently as it gets wrapped in a p.
        li = util.etree.SubElement(lst, 'li')
        self.parser.state.set('looselist')
        firstitem = items.pop(0)
        self.parser.parseBlocks(li, [firstitem])
        self.parser.state.reset()
    elif parent.tag in ['ol', 'ul']:
        # this catches the edge case of a multi-item indented list whose
        # first item is in a blank parent-list item:
        # * * subitem1
        #     * subitem2
        # see also ListIndentProcessor
        lst = parent
    else:
        # This is a new list so create parent with appropriate tag.
        lst = util.etree.SubElement(parent, self.TAG)
        # Check if a custom start integer is set
        if not self.parser.markdown.lazy_ol and self.STARTSWITH != '1':
            lst.attrib['start'] = self.STARTSWITH

    self.parser.state.set('list')
    # Loop through items in block, recursively parsing each with the
    # appropriate parent.
    for item in items:
        if item.startswith(' '*self.tab_length):
            # Item is indented. Parse with last item as parent
            self.parser.parseBlocks(lst[-1], [item])
        else:
            # New item. Create li and parse with it as parent
            li = util.etree.SubElement(lst, 'li')
            self.parser.parseBlocks(li, [item])
    self.parser.state.reset()
get_items(block)

Break a block into list items.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def get_items(self, block):
    """Break a block into list items."""
    items = []
    for line in block.split('\n'):
        m = self.CHILD_RE.match(line)
        if m:
            # This is a new list item
            # Check first item for the start index
            if not items and self.TAG == 'ol':
                # Detect the integer value of first list item
                INTEGER_RE = re.compile(r'(\d+)')
                self.STARTSWITH = INTEGER_RE.match(m.group(1)).group()
            # Append to the list
            items.append(m.group(3))
        elif self.INDENT_RE.match(line):
            # This is an indented (possibly nested) item.
            if items[-1].startswith(' '*self.tab_length):
                # Previous item was indented. Append to that item.
                items[-1] = '%s\n%s' % (items[-1], line)
            else:
                items.append(line)
        else:
            # This is another line of previous item. Append to that item.
            items[-1] = '%s\n%s' % (items[-1], line)
    return items

SaneListExtension(*args, **kwargs)

Bases: Extension

Add sane lists to Markdown.

Initiate Extension and set up configs.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def __init__(self, *args, **kwargs):
    """Initiate Extension and set up configs."""
    # check for configs arg for backward compat.
    # (there only ever used to be one so we use arg[0])
    if len(args):
        if args[0] is not None:
            self.setConfigs(args[0])
        warnings.warn('Extension classes accepting positional args is '
                      'pending Deprecation. Each setting should be '
                      'passed into the Class as a keyword. Positional '
                      'args are deprecated and will raise '
                      'an error in version 2.7. See the Release Notes for '
                      'Python-Markdown version 2.6 for more info.',
                      DeprecationWarning)
    # check for configs kwarg for backward compat.
    if 'configs' in kwargs.keys():
        if kwargs['configs'] is not None:
            self.setConfigs(kwargs.pop('configs', {}))
        warnings.warn('Extension classes accepting a dict on the single '
                      'keyword "config" is pending Deprecation. Each '
                      'setting should be passed into the Class as a '
                      'keyword directly. The "config" keyword is '
                      'deprecated and raise an error in '
                      'version 2.7. See the Release Notes for '
                      'Python-Markdown version 2.6 for more info.',
                      DeprecationWarning)
    # finally, use kwargs
    self.setConfigs(kwargs)

Attributes

config = {} class-attribute instance-attribute

Functions

getConfig(key, default='')

Return a setting for the given key or an empty string.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def getConfig(self, key, default=''):
    """Return a setting for the given key or an empty string."""
    if key in self.config:
        return self.config[key][0]
    else:
        return default
getConfigs()

Return all configs settings as a dict.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def getConfigs(self):
    """Return all configs settings as a dict."""
    return dict([(key, self.getConfig(key)) for key in self.config.keys()])
getConfigInfo()

Return all config descriptions as a list of tuples.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def getConfigInfo(self):
    """Return all config descriptions as a list of tuples."""
    return [(key, self.config[key][1]) for key in self.config.keys()]
setConfig(key, value)

Set a config setting for key with the given value.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def setConfig(self, key, value):
    """Set a config setting for `key` with the given `value`."""
    if isinstance(self.config[key][0], bool):
        value = parseBoolValue(value)
    if self.config[key][0] is None:
        value = parseBoolValue(value, preserve_none=True)
    self.config[key][0] = value
setConfigs(items)

Set multiple config settings given a dict or list of tuples.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/__init__.py
def setConfigs(self, items):
    """Set multiple config settings given a dict or list of tuples."""
    if hasattr(items, 'items'):
        # it's a dict
        items = items.items()
    for key, value in items:
        self.setConfig(key, value)
extendMarkdown(md, md_globals)

Override existing Processors.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/sane_lists.py
def extendMarkdown(self, md, md_globals):
    """Override existing Processors."""
    md.parser.blockprocessors['olist'] = SaneOListProcessor(md.parser)
    md.parser.blockprocessors['ulist'] = SaneUListProcessor(md.parser)

Functions

makeExtension(*args, **kwargs)

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/sane_lists.py
def makeExtension(*args, **kwargs):
    return SaneListExtension(*args, **kwargs)