Skip to content

postprocessors

POST-PROCESSORS.

Markdown also allows post-processors, which are similar to preprocessors in that they need to implement a "run" method. However, they are run after core processing.

Classes

Postprocessor(markdown_instance=None)

Bases: Processor

Postprocessors are run after the ElementTree it converted back into text.

Each Postprocessor implements a "run" method that takes a pointer to a text string, modifies it as necessary and returns a text string.

Postprocessors must extend markdown.Postprocessor.

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

Functions

run(text)

Main postprocessor method.

Subclasses of Postprocessor should implement a run method, which takes the html document as a single text string and returns a (possibly modified) string.

Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def run(self, text):
    """Main postprocessor method.

    Subclasses of Postprocessor should implement a `run` method, which
    takes the html document as a single text string and returns a
    (possibly modified) string.
    """
    pass  # pragma: no cover

RawHtmlPostprocessor(markdown_instance=None)

Bases: Postprocessor

Restore raw html to the document.

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

Functions

run(text)

Iterate over html stash and restore "safe" html.

Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def run(self, text):
    """Iterate over html stash and restore "safe" html."""
    replacements = OrderedDict()
    for i in range(self.markdown.htmlStash.html_counter):
        html, safe = self.markdown.htmlStash.rawHtmlBlocks[i]
        if self.markdown.safeMode and not safe:
            if unicode(self.markdown.safeMode).lower() == 'escape':
                html = self.escape(html)
            elif unicode(self.markdown.safeMode).lower() == 'remove':
                html = ''
            else:
                html = self.markdown.html_replacement_text
        if (self.isblocklevel(html) and
           (safe or not self.markdown.safeMode)):
            replacements["<p>%s</p>" %
                         (self.markdown.htmlStash.get_placeholder(i))] = \
                html + "\n"
        replacements[self.markdown.htmlStash.get_placeholder(i)] = html

    if replacements:
        pattern = re.compile("|".join(re.escape(k) for k in replacements))
        text = pattern.sub(lambda m: replacements[m.group(0)], text)

    return text
escape(html)

Basic html escaping.

Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def escape(self, html):
    """Basic html escaping."""
    html = html.replace('&', '&amp;')
    html = html.replace('<', '&lt;')
    html = html.replace('>', '&gt;')
    return html.replace('"', '&quot;')
isblocklevel(html)
Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def isblocklevel(self, html):
    m = re.match(r'^\<\/?([^ >]+)', html)
    if m:
        if m.group(1)[0] in ('!', '?', '@', '%'):
            # Comment, php etc...
            return True
        return util.isBlockLevel(m.group(1))
    return False

AndSubstitutePostprocessor(markdown_instance=None)

Bases: Postprocessor

Restore valid entities.

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

Functions

run(text)
Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def run(self, text):
    text = text.replace(util.AMP_SUBSTITUTE, "&")
    return text

UnescapePostprocessor(markdown_instance=None)

Bases: Postprocessor

Restore escaped chars.

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
RE = re.compile('%s(\\d+)%s' % (util.STX, util.ETX)) class-attribute instance-attribute

Functions

unescape(m)
Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def unescape(self, m):
    return util.int2str(int(m.group(1)))
run(text)
Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def run(self, text):
    return self.RE.sub(self.unescape, text)

Functions

build_postprocessors(md_instance, **kwargs)

Build the default postprocessors for Markdown.

Source code in pyrevitlib/pyrevit/coreutils/markdown/postprocessors.py
def build_postprocessors(md_instance, **kwargs):
    """Build the default postprocessors for Markdown."""
    postprocessors = odict.OrderedDict()
    postprocessors["raw_html"] = RawHtmlPostprocessor(md_instance)
    postprocessors["amp_substitute"] = AndSubstitutePostprocessor()
    postprocessors["unescape"] = UnescapePostprocessor()
    return postprocessors