Submit
Path:
~
/
/
proc
/
thread-self
/
root
/
proc
/
thread-self
/
root
/
usr
/
lib
/
python2.7
/
site-packages
/
markdown
/
extensions
/
File Content:
codehilite.pyc
� �(�Sc @@ s d Z d d l m Z d d l m Z d d l m Z d d l m Z d d l Z yF d d l m Z d d l m Z m Z m Z d d l m Z e Z Wn e k r� e Z n Xd � Z d e f d � � YZ d e f d � � YZ d e f d � � YZ i d � Z d S( u CodeHilite Extension for Python-Markdown ======================================== Adds code/syntax highlighting to standard Python-Markdown code blocks. Copyright 2006-2008 [Waylan Limberg](http://achinghead.com/). Project website: <http://packages.python.org/Markdown/extensions/code_hilite.html> Contact: markdown@freewisdom.org License: BSD (see ../LICENSE.md for details) Dependencies: * [Python 2.3+](http://python.org/) * [Markdown 2.0+](http://packages.python.org/Markdown/) * [Pygments](http://pygments.org/) i ( t absolute_import( t unicode_literalsi ( t Extensioni ( t TreeprocessorN( t highlight( t get_lexer_by_namet guess_lexert TextLexer( t HtmlFormatterc C@ s@ | s g Sy t t t | j � � � SWn t k r; g SXd S( u� Support our syntax for emphasizing certain lines of code. expr should be like '1 2' to emphasize lines 1 and 2 of a code block. Returns a list of ints, the line numbers to emphasize. N( t listt mapt intt splitt ValueError( t expr( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyt parse_hl_lines# s t CodeHilitec B@ sD e Z d Z d d e d d d e d d d � Z d � Z d � Z RS( uX Determine language of source code, and pass it into the pygments hilighter. Basic Usage: >>> code = CodeHilite(src = 'some text') >>> html = code.hilite() * src: Source string or any object with a .readline attribute. * linenums: (Boolean) Set line numbering to 'on' (True), 'off' (False) or 'auto'(None). Set to 'auto' by default. * guess_lang: (Boolean) Turn language auto-detection 'on' or 'off' (on by default). * css_class: Set class name of wrapper div ('codehilite' by default). * hl_lines: (List of integers) Lines to emphasize, 1-indexed. Low Level Usage: >>> code = CodeHilite() >>> code.src = 'some text' # String or anything with a .readline attr. >>> code.linenos = True # True or False; Turns line numbering on or of. >>> html = code.hilite() u codehiliteu defaulti c C@ s[ | | _ | | _ | | _ | | _ | | _ | | _ | | _ | | _ | pQ g | _ d S( N( t srct langt linenumst guess_langt css_classt stylet noclassest tab_lengtht hl_lines( t selfR R R R R R R R R ( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyt __init__N s c C@ s� | j j d � | _ | j d k r1 | j � n t r� y t | j � } WnV t k r� y( | j rx t | j � } n t � } Wq� t k r� t � } q� Xn Xt d | j d | j d | j d | j d | j � } t | j | | � S| j j d d � } | j d d � } | j d d � } | j d d � } g } | j r]| j d | j � n | j rv| j d � n d } | r�d d j | � } n d | j | | f Sd S( u7 Pass code to the [Pygments](http://pygments.pocoo.org/) highliter with optional line numbers. The output should then be styled with css to your liking. No styles are applied by default - only styling hooks (i.e.: <span class="k">). returns : A string of html. u t linenost cssclassR R R u &u &u <u <u >u >u "u "u language-%su linenumsu u class="%s"u u( <pre class="%s"><code%s>%s</code></pre> N( R t stripR t Nonet _parseHeadert pygmentsR R R R R R R R R R R R t replacet appendt join( R t lexert formattert txtt classest class_str( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyt hilite[ sB c C@ s& d d l } | j j d � } | j d � } | j d | j � } | j | � } | r� y | j d � j � | _ Wn t k r� d | _ n X| j d � r� | j d | � n | j d k r� | j d � r� t | _ n t | j d � � | _ n | j d | � d j | � j d � | _ d S( uD Determines language of a code block from shebang line and whether said line should be removed or left in place. If the sheband line contains a path (even a single /) then it is assumed to be a real shebang line and left alone. However, if no path is given (e.i.: #!python or :::python) then it is assumed to be a mock shebang for language identifitation of a code fragment and removed from the code block prior to processing for code highlighting. When a mock shebang (e.i: #!python) is found, line numbering is turned on. When colons are found in place of a shebang (e.i.: :::python), line numbering is left in the current state - off by default. Also parses optional list of highlight lines, like: :::python hl_lines="1 3" i Nu u� (?:(?:^::+)|(?P<shebang>^[#]!)) # Shebang or 2 or more colons (?P<path>(?:/\w+)*[/ ])? # Zero or 1 path (?P<lang>[\w+-]*) # The language \s* # Arbitrary whitespace # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))? u langu pathu shebangu hl_lines( t reR R t popt compilet VERBOSEt searcht groupt lowerR t IndexErrorR t insertR t TrueR R R$ R ( R R+ t linest flt ct m( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR � s$ N( t __name__t __module__t __doc__R R4 t FalseR R* R ( ( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR 3 s 2t HiliteTreeprocessorc B@ s e Z d Z d � Z RS( u% Hilight source code in code blocks. c C@ s� | j d � } x� | D]� } | j � } t | � d k r | d j d k r t | d j d | j d d | j d d | j d d | j d d | j d d | j j �} | j j j | j � d t �} | j � d | _ | | _ q q Wd S( u* Find code blocks and store in htmlStash. u prei i u codeR u linenumsR u guess_langR u css_classR u pygments_styleR u noclassesR t safeu pN( t getiteratort getchildrent lent tagR t textt configt markdownR t htmlStasht storeR* R4 t clear( R t roott blockst blockt childrent codet placeholder( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyt run� s % ( R9 R: R; RO ( ( ( sB /usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR= � s t CodeHiliteExtensionc B@ s e Z d Z d � Z d � Z RS( u4 Add source code hilighting to markdown codeblocks. c C@ s� i d d g d 6t d g d 6t d g d 6d d g d 6d d g d 6t d g d 6| _ x� | D]� \ } } | d k r t } n | d k r� t } n | d k r� d } n | d k r� t j d t � | r� | j d t � q� n | j | | � q^ Wd S( Nu0 Use lines numbers. True=yes, False=no, None=autou linenumsuH Depreciated! Use 'linenums' instead. Force line numbers - Default: Falseu force_linenosu, Automatic language detection - Default: Trueu guess_langu codehiliteu6 Set class name for wrapper <div> - Default: codehiliteu css_classu defaultu>