How to use the markdown.treeprocessors.Treeprocessor function in Markdown

To help you get started, we’ve selected a few Markdown examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Jaymon / bang / bang / md / extensions / __init__.py View on Github external
"""
    def __init__(self, md):
        """For some reason the vanilla BlockParser takes a parser instead of a 
        Markdown instance like every other processor, so this normalizes that so
        it acts like all the others"""
        self.md = md
        super(Blockprocessor, self).__init__(md.parser)

    def test(self, parent, block):
        raise NotImplementedError()

    def run(self, parent, blocks):
        raise NotImplementedError()


class Treeprocessor(BaseTreeprocessor):
    """
    http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm
    https://python-markdown.github.io/extensions/api/#treeprocessors
    https://github.com/waylan/Python-Markdown/blob/master/markdown/treeprocessors.py
    https://python-markdown.github.io/extensions/api/#working_with_et
    http://effbot.org/zone/element-index.htm#documentation
    """
    def dump(self, elem):
        """dump elem to stdout to debug"""
        return etree.dump(elem)

    def get_tags(self, elem, *tags):
        """go through and return all the *tags elements that are children of elem"""

        # http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree.ElementTree._ElementInterface.getiterator-method
        if len(tags) == 1:
github tornadoweb / tornado / website / markdown / extensions / toc.py View on Github external
"""
Table of Contents Extension for Python-Markdown
* * *

(c) 2008 [Jack Miller](http://codezen.org)

Dependencies:
* [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/)

"""
import markdown
from markdown import etree
import re

class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
    # Iterator wrapper to get parent and child all at once
    def iterparent(self, root):
        for parent in root.getiterator():
            for child in parent:
                yield parent, child

    def run(self, doc):
        div = etree.Element("div")
        div.attrib["class"] = "toc"
        last_li = None

        # Add title to the div
        if self.config["title"][0]:
            header = etree.SubElement(div, "span")
            header.attrib["class"] = "toctitle"
            header.text = self.config["title"][0]
github djblets / djblets / djblets / markdown / extensions / wysiwyg.py View on Github external
list_el = self.lastChild(parent)

        # The element should always be an <ol>, but better safe than sorry.
        if list_el.tag == 'ol' and 'start' in list_el.attrib:
            try:
                start = int(list_el.attrib['start'])
            except ValueError:
                start = 1

            if start &gt; 1:
                # Set a style= attribute to force the text to render with the
                # particular start value.
                list_el.attrib['style'] = 'counter-reset: li %s' % (start - 1)


class TrimTrailingEmptyParagraphs(Treeprocessor):
    """Removes empty paragraphs from the end of the tree.

    This will remove any trailing empty paragraphs formerly added by
    SmartEmptyBlockProcessor. This step must be done after all the blocks are
    processed, so that we have a complete picture of the state of the tree.
    It's therefore performed right before we prettify the tree.
    """

    def run(self, root):
        """Run the processor on the root of the tree."""
        num_children = len(root)
        start_i = num_children

        # Loop through the children from end to beginning, counting how many
        # of them are empty <p> elements.
        for child in reversed(root):</p></ol>
github jpfleury / gedit-markdown / python-markdown / python2 / extensions / codehilite.py View on Github external
if m.group('path'):
                # path exists - restore first line
                lines.insert(0, fl)
            if m.group('shebang'):
                # shebang exists - use line numbers
                self.linenos = True
        else:
            # No match
            lines.insert(0, fl)

        self.src = "\n".join(lines).strip("\n")



# ------------------ The Markdown Extension -------------------------------
class HiliteTreeprocessor(markdown.treeprocessors.Treeprocessor):
    """ Hilight source code in code blocks. """

    def run(self, root):
        """ Find code blocks and store in htmlStash. """
        blocks = root.getiterator('pre')
        for block in blocks:
            children = block.getchildren()
            if len(children) == 1 and children[0].tag == 'code':
                code = CodeHilite(children[0].text,
                            linenos=self.config['force_linenos'],
                            guess_lang=self.config['guess_lang'],
                            css_class=self.config['css_class'],
                            style=self.config['pygments_style'],
                            noclasses=self.config['noclasses'],
                            tab_length=self.markdown.tab_length)
                placeholder = self.markdown.htmlStash.store(code.hilite(),
github jpfleury / gedit-markdown / python-markdown / python3 / extensions / toc.py View on Github external
* * *

(c) 2008 [Jack Miller](http://codezen.org)

Dependencies:
* [Markdown 2.1+](http://packages.python.org/Markdown/)

"""
import markdown
from markdown.util import etree
from markdown.extensions.headerid import slugify, unique, itertext

import re


class TocTreeprocessor(markdown.treeprocessors.Treeprocessor):
    # Iterator wrapper to get parent and child all at once
    def iterparent(self, root):
        for parent in root.getiterator():
            for child in parent:
                yield parent, child

    def run(self, doc):
        marker_found = False

        div = etree.Element("div")
        div.attrib["class"] = "toc"
        last_li = None

        # Add title to the div
        if self.config["title"]:
            header = etree.SubElement(div, "span")
github DarkStarSword / 3d-fixes / md2helix_blogger.py View on Github external
'''
Format markdown into HTML to be a bit more consistent with the formatting on
the Helix blog:
- Strip <h1>, since that will be on the page title
- Replace </h1><h2> with <p><u>
'''

import markdown
from markdown.util import etree

class md2helixExtension(markdown.extensions.Extension):
	def extendMarkdown(self, md, md_globals):
		md.treeprocessors.add('md2helix_blogger', md2helixProcessor(), '_end')

class md2helixProcessor(markdown.treeprocessors.Treeprocessor):
	def run(self, root):
		for node in root:
			# Only remove top-level h1 tags since getiterator won't
			# track parents, and I don't care about sub-levels.
			if node.tag == 'h1':
				print '___REMOVING HEADER: "%s"___' % node.text
				root.remove(node)
		for node in root.getiterator():
			if node.tag == 'h2':
				print 'Downgrading header: "%s"' % node.text
				node.tag = 'p'
				child = etree.SubElement(node, 'u')
				child.text = node.text
				node.text = ""
		return root
</u></p></h2>
github GNOME / gobject-introspection / giscanner / mdextensions.py View on Github external
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor


class RemoveOuterP(Treeprocessor):
    def run(self, root):
        if len(root) == 1 and root[0].tag == "p":
            root[0].tag = "span"


class InlineMarkdown(Extension):
    def extendMarkdown(self, md, md_globals):
        md.treeprocessors.add("remove_outer_p", RemoveOuterP(md), "_end")
github apache / allura / Allura / allura / lib / markdown_extensions.py View on Github external
self._rewrite(tag, attr)
        val = tag.get(attr)
        val = urljoin(config.get('base_url', 'http://sourceforge.net/'),val)
        tag[attr] = val

class HTMLSanitizer(markdown.postprocessors.Postprocessor):

    def run(self, text):
        try:
            p = feedparser._HTMLSanitizer('utf-8')
        except TypeError: # $@%## pre-released versions from SOG
            p = feedparser._HTMLSanitizer('utf-8', '')
        p.feed(text.encode('utf-8'))
        return unicode(p.output(), 'utf-8')

class LineOrientedTreeProcessor(markdown.treeprocessors.Treeprocessor):
    '''Once MD is satisfied with the etree, this runs to replace \n with <br>
    within <p>s.
    '''

    def __init__(self, md):
        self._markdown = md
    
    def run(self, root):
        for node in root.getiterator('p'):
            if not node.text: continue
            if '\n' not in node.text: continue
            text = self._markdown.serializer(node)
            text = self._markdown.postprocessors['raw_html'].run(text)
            text = text.strip().encode('utf-8')
            if '\n' not in text: continue
            new_text = (text</p>
github zestedesavoir / zds-site / zds / utils / templatetags / mkd_ext / smartImg.py View on Github external
#! /usr/bin/env python

import markdown
from markdown.treeprocessors import Treeprocessor
from markdown.blockprocessors import BlockProcessor
import re
from markdown import util
class SmartImgProcessor(Treeprocessor):
    def __init__(self, parser, configs):
        Treeprocessor.__init__(self, parser)

        self.configs = configs
    
    def run(self, root):
        
        elemsToInspect = [root]
        while len(elemsToInspect) > 0:
            elem = elemsToInspect.pop()
            for nelem in elem:
                if nelem.tag in self.configs["PARENTS"]:
                    elemsToInspect.append(nelem)
                elif nelem.tag == "p" and len(list(nelem.itertext())) == 0 :
                    lelems = list(nelem.iter())
github gisce / markdown-i18n / markdown_i18n / parser.py View on Github external
import os
import re
import HTMLParser

from markdown.treeprocessors import Treeprocessor
from markdown.util import etree

from babel.messages.catalog import Catalog
from babel.messages import pofile, mofile
from babel.support import Translations
from six import binary_type

TRANSLATE_TAGS_RE = re.compile('^(li|p|h[1-6]|th|td)$')


class I18NTreeProcessor(Treeprocessor):

    def __init__(self, md, extension):
        self.extension = extension
        super(I18NTreeProcessor, self).__init__(md)
        self.parser = HTMLParser.HTMLParser()

    def translate(self, catalog, translations, root):
        children = root.getchildren()
        for idx, child in enumerate(children):
            if re.match(TRANSLATE_TAGS_RE, child.tag):
                translatable = child.text or ''
                translatable += '\n'.join([
                    etree.tostring(c) for c in
                        child.getchildren()
                ])
                if translatable: