Skip to content

treeprocessors

Markdown Treeprocessors.

Classes

Treeprocessor(markdown_instance=None)

Bases: Processor

Treeprocessors are run on the ElementTree object before serialization.

Each Treeprocessor implements a "run" method that takes a pointer to an ElementTree, modifies it as necessary and returns an ElementTree object.

Treeprocessors must extend markdown.Treeprocessor.

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(root)

Main treeprocessor method.

Subclasses of Treeprocessor should implement a run method, which takes a root ElementTree. This method can return another ElementTree object, and the existing root ElementTree will be replaced, or it can modify the current tree and return None.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def run(self, root):
    """Main treeprocessor method.

    Subclasses of Treeprocessor should implement a `run` method, which
    takes a root ElementTree. This method can return another ElementTree
    object, and the existing root ElementTree will be replaced, or it can
    modify the current tree and return None.
    """
    pass  # pragma: no cover

InlineProcessor(md)

Bases: Treeprocessor

A Treeprocessor that traverses a tree, applying inline patterns.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def __init__(self, md):
    self.__placeholder_prefix = util.INLINE_PLACEHOLDER_PREFIX
    self.__placeholder_suffix = util.ETX
    self.__placeholder_length = 4 + len(self.__placeholder_prefix) \
                                  + len(self.__placeholder_suffix)
    self.__placeholder_re = util.INLINE_PLACEHOLDER_RE
    self.markdown = md
    self.inlinePatterns = md.inlinePatterns

Attributes

markdown = md instance-attribute
inlinePatterns = md.inlinePatterns instance-attribute

Functions

run(tree)

Apply inline patterns to a parsed Markdown tree.

Iterate over ElementTree, find elements with inline tag, apply inline patterns and append newly created Elements to tree. If you don't want to process your data with inline paterns, instead of normal string, use subclass AtomicString:

node.text = markdown.AtomicString("This will not be processed.")

Parameters:

Name Type Description Default
tree ElementTree

Markdown tree.

required

Returns:

Type Description
ElementTree

Tree with applied inline patterns.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def run(self, tree):
    """Apply inline patterns to a parsed Markdown tree.

    Iterate over ElementTree, find elements with inline tag, apply inline
    patterns and append newly created Elements to tree.  If you don't
    want to process your data with inline paterns, instead of normal
    string, use subclass AtomicString:

        node.text = markdown.AtomicString("This will not be processed.")

    Args:
        tree (ElementTree): Markdown tree.

    Returns:
        (ElementTree): Tree with applied inline patterns.
    """
    self.stashed_nodes = {}

    stack = [tree]

    while stack:
        currElement = stack.pop()
        insertQueue = []
        for child in currElement:
            if child.text and not isinstance(
                child.text, util.AtomicString
            ):
                text = child.text
                child.text = None
                lst = self.__processPlaceholders(
                    self.__handleInline(text), child
                )
                stack += lst
                insertQueue.append((child, lst))
            if child.tail:
                tail = self.__handleInline(child.tail)
                dumby = util.etree.Element('d')
                child.tail = None
                tailResult = self.__processPlaceholders(tail, dumby, False)
                if dumby.tail:
                    child.tail = dumby.tail
                pos = list(currElement).index(child) + 1
                tailResult.reverse()
                for newChild in tailResult:
                    currElement.insert(pos, newChild)
            if len(child):
                stack.append(child)

        for element, lst in insertQueue:
            if self.markdown.enable_attributes:
                if element.text and isString(element.text):
                    element.text = inlinepatterns.handleAttributes(
                        element.text, element
                    )
            i = 0
            for newChild in lst:
                if self.markdown.enable_attributes:
                    # Processing attributes
                    if newChild.tail and isString(newChild.tail):
                        newChild.tail = inlinepatterns.handleAttributes(
                            newChild.tail, element
                        )
                    if newChild.text and isString(newChild.text):
                        newChild.text = inlinepatterns.handleAttributes(
                            newChild.text, newChild
                        )
                element.insert(i, newChild)
                i += 1
    return tree

PrettifyTreeprocessor(markdown_instance=None)

Bases: Treeprocessor

Add linebreaks to the html 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(root)

Add linebreaks to ElementTree root object.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def run(self, root):
    """Add linebreaks to ElementTree root object."""
    self._prettifyETree(root)
    # Do <br />'s seperately as they are often in the middle of
    # inline content and missed by _prettifyETree.
    brs = root.iter('br')
    for br in brs:
        if not br.tail or not br.tail.strip():
            br.tail = '\n'
        else:
            br.tail = '\n%s' % br.tail
    # Clean up extra empty lines at end of code blocks.
    pres = root.iter('pre')
    for pre in pres:
        if len(pre) and pre[0].tag == 'code':
            pre[0].text = util.AtomicString(pre[0].text.rstrip() + '\n')

Functions

build_treeprocessors(md_instance, **kwargs)

Build the default treeprocessors for Markdown.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def build_treeprocessors(md_instance, **kwargs):
    """Build the default treeprocessors for Markdown."""
    treeprocessors = odict.OrderedDict()
    treeprocessors["inline"] = InlineProcessor(md_instance)
    treeprocessors["prettify"] = PrettifyTreeprocessor(md_instance)
    return treeprocessors

isString(s)

Check if it's string.

Source code in pyrevitlib/pyrevit/coreutils/markdown/treeprocessors.py
def isString(s):
    """Check if it's string."""
    if not isinstance(s, util.AtomicString):
        return isinstance(s, util.string_type)
    return False