Skip to content

def_list

Definition List Extension for Python-Markdown.

Adds parsing of Definition Lists to Python-Markdown.

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

Original code Copyright 2008 Waylan Limberg

All changes Copyright 2008-2014 The Python Markdown Project

License: BSD

Classes

DefListProcessor(parser)

Bases: BlockProcessor

Process Definition Lists.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def __init__(self, parser):
    self.parser = parser
    self.tab_length = parser.markdown.tab_length

Attributes

parser = parser instance-attribute
tab_length = parser.markdown.tab_length instance-attribute
RE = re.compile('(^|\\n)[ ]{0,3}:[ ]{1,3}(.*?)(\\n|$)') class-attribute instance-attribute
NO_INDENT_RE = re.compile('^[ ]{0,3}[^ :]') class-attribute 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/extensions/def_list.py
def test(self, parent, block):
    return bool(self.RE.search(block))
run(parent, blocks)
Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/def_list.py
def run(self, parent, blocks):

    raw_block = blocks.pop(0)
    m = self.RE.search(raw_block)
    terms = [l.strip() for l in
             raw_block[:m.start()].split('\n') if l.strip()]
    block = raw_block[m.end():]
    no_indent = self.NO_INDENT_RE.match(block)
    if no_indent:
        d, theRest = (block, None)
    else:
        d, theRest = self.detab(block)
    if d:
        d = '%s\n%s' % (m.group(2), d)
    else:
        d = m.group(2)
    sibling = self.lastChild(parent)
    if not terms and sibling is None:
        # This is not a definition item. Most likely a paragraph that
        # starts with a colon at the begining of a document or list.
        blocks.insert(0, raw_block)
        return False
    if not terms and sibling.tag == 'p':
        # The previous paragraph contains the terms
        state = 'looselist'
        terms = sibling.text.split('\n')
        parent.remove(sibling)
        # Aquire new sibling
        sibling = self.lastChild(parent)
    else:
        state = 'list'

    if sibling is not None and sibling.tag == 'dl':
        # This is another item on an existing list
        dl = sibling
        if not terms and len(dl) and dl[-1].tag == 'dd' and len(dl[-1]):
            state = 'looselist'
    else:
        # This is a new list
        dl = etree.SubElement(parent, 'dl')
    # Add terms
    for term in terms:
        dt = etree.SubElement(dl, 'dt')
        dt.text = term
    # Add definition
    self.parser.state.set(state)
    dd = etree.SubElement(dl, 'dd')
    self.parser.parseBlocks(dd, [d])
    self.parser.state.reset()

    if theRest:
        blocks.insert(0, theRest)

DefListIndentProcessor(*args)

Bases: ListIndentProcessor

Process indented children of definition list items.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def __init__(self, *args):
    super(ListIndentProcessor, self).__init__(*args)
    self.INDENT_RE = re.compile(r'^(([ ]{%s})+)' % self.tab_length)

Attributes

parser = parser instance-attribute
tab_length = parser.markdown.tab_length instance-attribute
INDENT_RE = re.compile('^(([ ]{%s})+)' % self.tab_length) instance-attribute
ITEM_TYPES = ['dd'] class-attribute instance-attribute
LIST_TYPES = ['dl'] class-attribute 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 block.startswith(' '*self.tab_length) and \
        not self.parser.state.isstate('detabbed') and \
        (parent.tag in self.ITEM_TYPES or
            (len(parent) and parent[-1] is not None and
                (parent[-1].tag in self.LIST_TYPES)))
run(parent, blocks)
Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def run(self, parent, blocks):
    block = blocks.pop(0)
    level, sibling = self.get_level(parent, block)
    block = self.looseDetab(block, level)

    self.parser.state.set('detabbed')
    if parent.tag in self.ITEM_TYPES:
        # It's possible that this parent has a 'ul' or 'ol' child list
        # with a member.  If that is the case, then that should be the
        # parent.  This is intended to catch the edge case of an indented
        # list whose first member was parsed previous to this point
        # see OListProcessor
        if len(parent) and parent[-1].tag in self.LIST_TYPES:
            self.parser.parseBlocks(parent[-1], [block])
        else:
            # The parent is already a li. Just parse the child block.
            self.parser.parseBlocks(parent, [block])
    elif sibling.tag in self.ITEM_TYPES:
        # The sibling is a li. Use it as parent.
        self.parser.parseBlocks(sibling, [block])
    elif len(sibling) and sibling[-1].tag in self.ITEM_TYPES:
        # The parent is a list (``ol`` or ``ul``) which has children.
        # Assume the last child li is the parent of this block.
        if sibling[-1].text:
            # If the parent li has text, that text needs to be moved to a p
            # The p must be 'inserted' at beginning of list in the event
            # that other children already exist i.e.; a nested sublist.
            p = util.etree.Element('p')
            p.text = sibling[-1].text
            sibling[-1].text = ''
            sibling[-1].insert(0, p)
        self.parser.parseChunk(sibling[-1], block)
    else:
        self.create_item(sibling, block)
    self.parser.state.reset()
get_level(parent, block)

Get level of indent based on list level.

Source code in pyrevitlib/pyrevit/coreutils/markdown/blockprocessors.py
def get_level(self, parent, block):
    """Get level of indent based on list level."""
    # Get indent level
    m = self.INDENT_RE.match(block)
    if m:
        indent_level = len(m.group(1))/self.tab_length
    else:
        indent_level = 0
    if self.parser.state.isstate('list'):
        # We're in a tightlist - so we already are at correct parent.
        level = 1
    else:
        # We're in a looselist - so we need to find parent.
        level = 0
    # Step through children of tree to find matching indent level.
    while indent_level > level:
        child = self.lastChild(parent)
        if (child is not None and
           (child.tag in self.LIST_TYPES or child.tag in self.ITEM_TYPES)):
            if child.tag in self.LIST_TYPES:
                level += 1
            parent = child
        else:
            # No more child levels. If we're short of indent_level,
            # we have a code block. So we stop here.
            break
    return level, parent
create_item(parent, block)

Create a new dd and parse the block with it as the parent.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/def_list.py
def create_item(self, parent, block):
    """Create a new dd and parse the block with it as the parent."""
    dd = etree.SubElement(parent, 'dd')
    self.parser.parseBlocks(dd, [block])

DefListExtension(*args, **kwargs)

Bases: Extension

Add definition 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)

Add an instance of DefListProcessor to BlockParser.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/def_list.py
def extendMarkdown(self, md, md_globals):
    """Add an instance of DefListProcessor to BlockParser."""
    md.parser.blockprocessors.add('defindent',
                                  DefListIndentProcessor(md.parser),
                                  '>indent')
    md.parser.blockprocessors.add('deflist',
                                  DefListProcessor(md.parser),
                                  '>ulist')

Functions

makeExtension(*args, **kwargs)

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