Renderers

The renderers define the output of the markdown text.

Usage

There are two ways to add a renderer:

from mpiece.renderer import HtmlRenderer
from mpiece import markdown

class CustomRenderer(HtmlRenderer):
        pass

renderer = CustomRenderer()
text_md = "*Hello world*"
result = markdown(text_md, renderer=renderer)

# output: <p><em>Hello world</em></p>

or

from mpiece.renderer import HtmlRenderer
from mpiece import Markdown

class CustomRenderer(HtmlRenderer):
        pass

markdown = Markdown()
markdown.renderer = CustomRenderer()
text_md = "*Hello world*"
result = markdown(text_md)

# output: <p><em>Hello world</em></p>

Renderers list

class mpiece.renderer.Renderer

Base renderer class. All renderer classes should be subclasses of this class.

This class and their subclass are used in the mpiece.markdown() function or in the mpiece.core.MPiece.parse() method.

post_process_text(text)

Process the rendered text.

Parameters:text (str) – Rendered text
Return str:
class mpiece.renderer.HtmlRenderer(use_underline=True, use_paragraph=True, escape_html=True)

Transform the lexer results in html code.

Parameters:
  • use_underline (bool) –
    • True: The markdown _text_ will transform in <ins>text</ins>
    • False: The markdown _text_ will transform in <em>text</em>
  • use_paragraph (bool) –
    • True: The new line in the markdown text will transform in <p></p> html tag.
    • False: The new line in the markdown text will transform in <br> html tag.
  • escape_html (bool) –
    • True: Escape the html tag in the markdown text.
    • False: No escape the html tag in the markdown text.
escape(text)

Escape dangerous html characters.

Parameters:text (str) – Html text without escape.
Returns:Html text escaped.
escape_args(*args)

Escape html characters of all arguments

Parameters:*args ([str]) – List of html text without escape.
Returns:list of all arguments escaped.

Check if a link has an invalid scheme. Also transform the & character in &amp; character.

Parameters:
  • link (str) – Link checked.
  • smart_amp (bool) – Transform the ‘&’ characters in ‘&amp;’ characters.
Returns:

Return the link if the scheme is valid. If not return an empty string.

scheme_blacklist = ('javascript', 'data', 'vbscript')

Blacklist of link schemes