Skip to content

fenced_code

Fenced Code Extension for Python Markdown.

This extension adds Fenced Code Blocks to Python-Markdown.

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

Original code Copyright 2007-2008 Waylan Limberg.

All changes Copyright 2008-2014 The Python Markdown Project

License: BSD

Classes

FencedCodeExtension(*args, **kwargs)

Bases: Extension

Fenced code extension.

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 FencedBlockPreprocessor to the Markdown instance.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/fenced_code.py
def extendMarkdown(self, md, md_globals):
    """Add FencedBlockPreprocessor to the Markdown instance."""
    md.registerExtension(self)

    md.preprocessors.add('fenced_code_block',
                         FencedBlockPreprocessor(md),
                         ">normalize_whitespace")

FencedBlockPreprocessor(md)

Bases: Preprocessor

Fenced code block preprocessor.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/fenced_code.py
def __init__(self, md):
    super(FencedBlockPreprocessor, self).__init__(md)

    self.checked_for_codehilite = False
    self.codehilite_conf = {}

Attributes

markdown = markdown_instance instance-attribute
FENCED_BLOCK_RE = re.compile('\n(?P<fence>^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~\n(\\{?\\.?(?P<lang>[\\w#.+-]*))?[ ]* # Optional {, and lang\n# Optional highlight lines, single- or double-quote-delimited\n(hl_lines=(?P<quot>"|\')(?P<hl_lines>.*?)(?P=quot))?[ ]*\n}?[ ]*\\n # Optional closing }\n(?P<code>.*?)(?<=\\n)\n(?P=fence)[ ]*$', re.MULTILINE | re.DOTALL | re.VERBOSE) class-attribute instance-attribute
CODE_WRAP = '<pre><code%s>%s</code></pre>' class-attribute instance-attribute
LANG_TAG = ' class="%s"' class-attribute instance-attribute
checked_for_codehilite = False instance-attribute
codehilite_conf = {} instance-attribute

Functions

run(lines)

Match and store Fenced Code Blocks in the HtmlStash.

Source code in pyrevitlib/pyrevit/coreutils/markdown/extensions/fenced_code.py
def run(self, lines):
    """Match and store Fenced Code Blocks in the HtmlStash."""
    # Check for code hilite extension
    if not self.checked_for_codehilite:
        for ext in self.markdown.registeredExtensions:
            if isinstance(ext, CodeHiliteExtension):
                self.codehilite_conf = ext.config
                break

        self.checked_for_codehilite = True

    text = "\n".join(lines)
    while 1:
        m = self.FENCED_BLOCK_RE.search(text)
        if m:
            lang = ''
            if m.group('lang'):
                lang = self.LANG_TAG % m.group('lang')

            # If config is not empty, then the codehighlite extension
            # is enabled, so we call it to highlight the code
            if self.codehilite_conf:
                highliter = CodeHilite(
                    m.group('code'),
                    linenums=self.codehilite_conf['linenums'][0],
                    guess_lang=self.codehilite_conf['guess_lang'][0],
                    css_class=self.codehilite_conf['css_class'][0],
                    style=self.codehilite_conf['pygments_style'][0],
                    use_pygments=self.codehilite_conf['use_pygments'][0],
                    lang=(m.group('lang') or None),
                    noclasses=self.codehilite_conf['noclasses'][0],
                    hl_lines=parse_hl_lines(m.group('hl_lines'))
                )

                code = highliter.hilite()
            else:
                code = self.CODE_WRAP % (lang,
                                         self._escape(m.group('code')))

            placeholder = self.markdown.htmlStash.store(code, safe=True)
            text = '%s\n%s\n%s' % (text[:m.start()],
                                   placeholder,
                                   text[m.end():])
        else:
            break
    return text.split("\n")

Functions

makeExtension(*args, **kwargs)

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