3 \ @ s~ d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z m Z mZ dd lZG d d deZ G dd d eZdddZd S )an Fenced Code Extension for Python Markdown ========================================= This extension adds Fenced Code Blocks to Python-Markdown. >>> import markdown >>> text = ''' ... A paragraph before a fenced code block: ... ... ~~~ ... Fenced code block ... ~~~ ... ''' >>> html = markdown.markdown(text, extensions=['fenced_code']) >>> print html
A paragraph before a fenced code block:
Fenced code block
Works with safe_mode also (we check this because we are using the HtmlStash):
>>> print markdown.markdown(text, extensions=['fenced_code'], safe_mode='replace')
A paragraph before a fenced code block:
Fenced code block
Include tilde's in a code block and wrap with blank lines:
>>> text = '''
... ~~~~~~~~
...
... ~~~~
... ~~~~~~~~'''
>>> print markdown.markdown(text, extensions=['fenced_code'])
~~~~
Language tags:
>>> text = '''
... ~~~~{.python}
... # Some python code
... ~~~~'''
>>> print markdown.markdown(text, extensions=['fenced_code'])
# Some python code
Optionally backticks instead of tildes as per how github's code block markdown is identified:
>>> text = '''
... `````
... # Arbitrary code
... ~~~~~ # these tildes will not close the block
... `````'''
>>> print markdown.markdown(text, extensions=['fenced_code'])
# Arbitrary code
~~~~~ # these tildes will not close the block
If the codehighlite extension and Pygments are installed, lines can be highlighted:
>>> text = '''
... ```hl_lines="1 3"
... line 1
... line 2
... line 3
... ```'''
>>> print markdown.markdown(text, extensions=['codehilite', 'fenced_code'])
line 1
line 2
line 3
Copyright 2007-2008 [Waylan Limberg](http://achinghead.com/).
Project website: .*?)(?<=\n)
(?P=fence)[ ]*$z%s
z class="%s"c s t t| j| d| _i | _d S )NF)superr __init__checked_for_codehilitecodehilite_conf)r r ) __class__r r r y s z FencedBlockPreprocessor.__init__c C sF | j s2x$| jjD ]}t|tr|j| _P qW d| _ dj|}x| jj |}|r6d}|j
drn| j|j
d }| jrt|j
d| jd d | jd d | jd d | jd
d |j
dpd| jd d t
|j
d
d}|j }n| j|| j|j
df }| jjj|dd}d|d|j |||j d f }q>P q>W |jdS )z6 Match and store Fenced Code Blocks in the HtmlStash. T
langcodelinenumsr
guess_lang css_classZpygments_styleN noclasseshl_lines)r" r# r$ styler r% r&