3 \@s~dZddlmZddlmZddlmZddlmZddlm Z m Z m Z dd l Z Gd d d eZ Gd d 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: Contact: markdown@freewisdom.org License: BSD (see ../docs/LICENSE for details) Dependencies: * [Python 2.4+](http://python.org) * [Markdown 2.0+](http://packages.python.org/Markdown/) * [Pygments (optional)](http://pygments.org) )absolute_import)unicode_literals) Extension) Preprocessor) CodeHiliteCodeHiliteExtensionparse_hl_linesNc@seZdZddZdS)FencedCodeExtensioncCs"|j||jjdt|ddS)z7 Add FencedBlockPreprocessor to the Markdown instance. Zfenced_code_blockz>normalize_whitespaceN)registerExtension preprocessorsaddFencedBlockPreprocessor)selfmd md_globalsr,build/lib/markdown/extensions/fenced_code.pyextendMarkdownds z"FencedCodeExtension.extendMarkdownN)__name__ __module__ __qualname__rrrrrr bsr csNeZdZejdejejBejBZdZ dZ fddZ ddZ dd Z ZS) raT (?P^(?:~{3,}|`{3,}))[ ]* # Opening ``` or ~~~ (\{?\.?(?P[a-zA-Z0-9_+-]*))?[ ]* # Optional {, and lang # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P"|')(?P.*?)(?P=quot))?[ ]* }?[ ]*\n # Optional closing } (?P.*?)(?<=\n) (?P=fence)[ ]*$z
%s
z class="%s"cs tt|j|d|_i|_dS)NF)superr__init__checked_for_codehilitecodehilite_conf)rr) __class__rrrysz FencedBlockPreprocessor.__init__c CsF|js2x$|jjD]}t|tr|j|_PqWd|_dj|}x|jj |}|r6d}|j drn|j |j d}|jrt |j d|jdd|jdd|jd d|jd d|j dpd |jd dt |j d d}|j}n|j||j|j df}|jjj|dd}d|d |j|||jd f}q>Pq>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&)safez%s %s %s)rmarkdownregisteredExtensions isinstancer configrjoinFENCED_BLOCK_REsearchgroupLANG_TAGrr Zhilite CODE_WRAP_escape htmlStashstorestartendsplit) rlinesexttextmr Z highliterr! placeholderrrrruns8            (zFencedBlockPreprocessor.runcCs4|jdd}|jdd}|jdd}|jdd}|S) z basic html escaping &z&z>"z")replace)rZtxtrrrr3s     zFencedBlockPreprocessor._escape)rrrrecompile MULTILINEDOTALLVERBOSEr.r2r1rr>r3 __classcell__rr)rrrms *rcCs t|dS)N)configs)r )rJrrr makeExtensionsrK)N)__doc__ __future__rrrrr rZ codehiliterr r rDr rrKrrrrXs     E