Lexers¶
The lexers define the markdown grammar.
Usage¶
There are two ways to add a lexer class:
from mpiece.lexer import Lexer
from mpiece import markdown
text_md="**Hello world!!!**"
result = markdown(text, lexer=Lexer())
#output <p><strong>Hello world!!!</strong></p>
or
from mpiece.lexer import Lexer
from mpiece import Markdown
text_md="**Hello world!!!**"
markdown = Markdown()
markdown.lexer = Lexer()
result = markdown(text, lexer=lexer)
#output <p><strong>Hello world!!!</strong></p>
Lexers list¶
-
class
mpiece.lexer.
Lexer
(exclude=set([]), tab_size=4, escape_chars='')¶ Converts the markdown text in tokens. All lexer classes should be subclasses of this class.
This class and their subclass are used in the
mpiece.markdown()
function or in thempiece.Markdown
class.Parameters: - exclude (set) –
The markdown grammar name which you want exclude in the markdown text. The default list of markdown grammar names is:
- fenced_code
- table
- ulist
- olist
- blockquote
- header
- header2
- break_line
- new_line
- image
- link
- code_inline
- bold
- italic
- underline
- strike
- tab_size (int) – Tabulators size.
- escape_chars (str) – String with the characters escaped if they have the have the
\
character before. This string is added to the stringLexer.escape_chars
Example: from mpiece.lexer import Lexer from mpiece import markdown text_md = '*italic*, **this is bold**, `this is a code inline`' # markdown normal result = markdown(text_md) print(result) # output: <p><em>italic</em>, <strong>this is bold</strong>, <code>this is a code inline</code></p> # markdown excluding bold and code inline lexer_exclude = Lexer(exclude=set({'bold', 'code_inline'})) result = markdown(text_md, lexer=lexer_exclude) print(result) # output: <p><em>italic</em>, **this is bold**, `this is a code inline`</p>
Variables: - escape_chars (str) –
List of characters escaped if they has the character before.
Initial value: ‘*~`_[]()>.’ - order_inline ([str]) –
Order of the inline elements.
Initial value: [‘escape_backslash’, ‘code_inline’, ‘image’, ‘link’, ‘bold’, ‘italic’, ‘bold’, ‘underline’, ‘strike’] - order_block ([str]) –
Order of the block elements.
Initial value: [fenced_code’, ‘table’, ‘ulist’, ‘olist’, ‘blockquote’, ‘header’, ‘header2’, ‘break_line’, ‘new_line’] - initial_order ([str]) –
Define the initial order for the lexer class.
Initial value: self.order_block + self.order_inline
-
define_order
()¶ Make the order of the grammar inside of the markdown elements.
-
pre_process_text
(text)¶ Process the text before be rendered.
Parameters: text (str) – Markdown text. Return str: text post processed.
- exclude (set) –