Skip to content

headerid

HeaderID Extension for Python-Markdown.

Auto-generate id attributes for HTML headers.

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

Original code Copyright 2007-2011 Waylan Limberg.

All changes Copyright 2011-2014 The Python Markdown Project

License: BSD

Classes

HeaderIdTreeprocessor(markdown_instance=None)

Bases: Treeprocessor

Assign IDs to headers.

Source code in pyrevitlib/pyrevit/coreutils/markdown/util.py
def __init__(self, markdown_instance=None):
    if markdown_instance:
        self.markdown = markdown_instance

Attributes

markdown = markdown_instance instance-attribute
IDs = set() class-attribute instance-attribute

Functions

run(doc)
Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/headerid.py
def run(self, doc):
    start_level, force_id = self._get_meta()
    slugify = self.config['slugify']
    sep = self.config['separator']
    for elem in doc:
        if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
            if force_id:
                if "id" in elem.attrib:
                    id = elem.get('id')
                else:
                    id = stashedHTML2text(''.join(elem.itertext()), self.md)
                    id = slugify(id, sep)
                elem.set('id', unique(id, self.IDs))
            if start_level:
                level = int(elem.tag[-1]) + start_level
                if level > 6:
                    level = 6
                elem.tag = 'h%d' % level

HeaderIdExtension(*args, **kwargs)

Bases: Extension

Header ID extension.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/headerid.py
def __init__(self, *args, **kwargs):
    # set defaults
    self.config = {
        'level': ['1', 'Base level for headers.'],
        'forceid': ['True', 'Force all headers to have an id.'],
        'separator': ['-', 'Word separator.'],
        'slugify': [slugify, 'Callable to generate anchors']
    }

    super(HeaderIdExtension, self).__init__(*args, **kwargs)

    warnings.warn(
        'The HeaderId Extension is pending deprecation. Use the TOC Extension instead.',
        PendingDeprecationWarning
    )

Attributes

config = {'level': ['1', 'Base level for headers.'], 'forceid': ['True', 'Force all headers to have an id.'], 'separator': ['-', 'Word separator.'], 'slugify': [slugify, 'Callable to generate anchors']} 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)
Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/headerid.py
def extendMarkdown(self, md, md_globals):
    md.registerExtension(self)
    self.processor = HeaderIdTreeprocessor()
    self.processor.md = md
    self.processor.config = self.getConfigs()
    if 'attr_list' in md.treeprocessors.keys():
        # insert after attr_list treeprocessor
        md.treeprocessors.add('headerid', self.processor, '>attr_list')
    else:
        # insert after 'prettify' treeprocessor.
        md.treeprocessors.add('headerid', self.processor, '>prettify')
reset()
Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/headerid.py
def reset(self):
    self.processor.IDs = set()

Functions

makeExtension(*args, **kwargs)

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