Skip to content

extra

Python-Markdown Extra Extension.

A compilation of various Python-Markdown extensions that imitates PHP Markdown Extra.

Note that each of the individual extensions still need to be available on your PYTHONPATH. This extension simply wraps them all up as a convenience so that only one extension needs to be listed when initiating Markdown. See the documentation for each individual extension for specifics about that extension.

There may be additional extensions that are distributed with Python-Markdown that are not included here in Extra. Those extensions are not part of PHP Markdown Extra, and therefore, not part of Python-Markdown Extra. If you really would like Extra to include additional extensions, we suggest creating your own clone of Extra under a differant name. You could also edit the extensions global variable defined below, but be aware that such changes may be lost when you upgrade to any future version of Python-Markdown.

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

Copyright The Python Markdown Project

License: BSD

Attributes

extensions = ['markdown.extensions.smart_strong', 'markdown.extensions.fenced_code', 'markdown.extensions.footnotes', 'markdown.extensions.attr_list', 'markdown.extensions.def_list', 'markdown.extensions.tables', 'markdown.extensions.abbr'] module-attribute

Classes

ExtraExtension(*args, **kwargs)

Bases: Extension

Add various extensions to Markdown class.

Config is a dumb holder which gets passed to actual ext later.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/extra.py
def __init__(self, *args, **kwargs):
    """Config is a dumb holder which gets passed to actual ext later."""
    self.config = kwargs.pop('configs', {})
    self.config.update(kwargs)

Attributes

config = kwargs.pop('configs', {}) 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)

Register extension instances.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/extra.py
def extendMarkdown(self, md, md_globals):
    """Register extension instances."""
    md.registerExtensions(extensions, self.config)
    if not md.safeMode:
        # Turn on processing of markdown text within raw html
        md.preprocessors['html_block'].markdown_in_raw = True
        md.parser.blockprocessors.add('markdown_block',
                                      MarkdownInHtmlProcessor(md.parser),
                                      '_begin')
        md.parser.blockprocessors.tag_counter = -1
        md.parser.blockprocessors.contain_span_tags = re.compile(
            r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE)

MarkdownInHtmlProcessor(parser)

Bases: BlockProcessor

Process Markdown Inside HTML Blocks.

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

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/extra.py
def test(self, parent, block):
    return block == util.TAG_PLACEHOLDER % \
                    unicode(self.parser.blockprocessors.tag_counter + 1)
run(parent, blocks, tail=None, nest=False)
Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/extra.py
def run(self, parent, blocks, tail=None, nest=False):
    self._tag_data = self.parser.markdown.htmlStash.tag_data

    self.parser.blockprocessors.tag_counter += 1
    tag = self._tag_data[self.parser.blockprocessors.tag_counter]

    # Create Element
    markdown_value = tag['attrs'].pop('markdown')
    element = util.etree.SubElement(parent, tag['tag'], tag['attrs'])

    # Slice Off Block
    if nest:
        self.parser.parseBlocks(parent, tail)  # Process Tail
        block = blocks[1:]
    else:  # includes nests since a third level of nesting isn't supported
        block = blocks[tag['left_index'] + 1: tag['right_index']]
        del blocks[:tag['right_index']]

    # Process Text
    if (self.parser.blockprocessors.contain_span_tags.match(  # Span Mode
            tag['tag']) and markdown_value != 'block') or \
            markdown_value == 'span':
        element.text = '\n'.join(block)
    else:                                                     # Block Mode
        i = self.parser.blockprocessors.tag_counter + 1
        if len(self._tag_data) > i and self._tag_data[i]['left_index']:
            first_subelement_index = self._tag_data[i]['left_index'] - 1
            self.parser.parseBlocks(
                element, block[:first_subelement_index])
            if not nest:
                block = self._process_nests(element, block)
        else:
            self.parser.parseBlocks(element, block)

Functions

makeExtension(*args, **kwargs)

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