ó Šç\c@@s dZddlmZddlmZddlmZddlmZddlZyFdd l m Z dd l m Z m Z mZdd lmZeZ Wnek r±eZ nXd „Zd efd„ƒYZdefd„ƒYZdefd„ƒYZid„ZdS(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: 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(tabsolute_import(tunicode_literalsi(t Extensioni(t TreeprocessorN(t highlight(tget_lexer_by_namet guess_lexert TextLexer(t HtmlFormattercC@s@|s gSyttt|jƒƒƒSWntk r;gSXdS(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(tlisttmaptinttsplitt ValueError(texpr((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pytparse_hl_lines#s  t CodeHilitec B@sDeZdZddedddeddd„ Zd„Zd„ZRS(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 codehiliteudefaultic C@s[||_||_||_||_||_||_||_||_| pQg|_dS(N( tsrctlangtlinenumst guess_langt css_clasststylet noclassest tab_lengththl_lines( tselfRRRRRRRRR((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyt__init__Ns        c C@s°|jjdƒ|_|jdkr1|jƒntrìyt|jƒ}WnVtk r¢y(|jrxt |jƒ}n t ƒ}Wq£tk ržt ƒ}q£XnXt d|j d|j d|jd|jd|jƒ}t|j||ƒS|jjddƒ}|jd d ƒ}|jd d ƒ}|jd dƒ}g}|jr]|jd|jƒn|j rv|jdƒnd}|r˜ddj|ƒ}nd|j ||fSdS(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.: ). returns : A string of html. u tlinenostcssclassRRRu&u&uu>u"u"u language-%sulinenumsuu class="%s"u u(
%s
N(RtstripRtNonet _parseHeadertpygmentsRR RRRRRRRRRRtreplacetappendtjoin(Rtlexert formatterttxttclassest class_str((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pythilite[sB            cC@s&ddl}|jjdƒ}|jdƒ}|jd|jƒ}|j|ƒ}|r÷y|jdƒjƒ|_ Wnt k rd|_ nX|jdƒr²|j d|ƒn|j dkrÜ|jdƒrÜt|_ nt|jdƒƒ|_n|j d|ƒdj|ƒjdƒ|_dS( 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" iNu uœ (?:(?:^::+)|(?P^[#]!)) # Shebang or 2 or more colons (?P(?:/\w+)*[/ ])? # Zero or 1 path (?P[\w+-]*) # The language \s* # Arbitrary whitespace # Optional highlight lines, single- or double-quote-delimited (hl_lines=(?P"|')(?P.*?)(?P=quot))? ulangupathushebanguhl_lines(treRR tpoptcompiletVERBOSEtsearchtgrouptlowerRt IndexErrorRtinsertRtTrueRRR$R(RR+tlinestfltctm((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR s$    N( t__name__t __module__t__doc__RR4tFalseRR*R (((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR3s    2tHiliteTreeprocessorcB@seZdZd„ZRS(u% Hilight source code in code blocks. cC@sõ|jdƒ}xß|D]×}|jƒ}t|ƒdkr|djdkrt|djd|jdd|jdd |jd d |jd d |jdd|jjƒ}|jj j |j ƒdt ƒ}|j ƒd|_||_qqWdS(u* Find code blocks and store in htmlStash. upreiiucodeRulinenumsRu guess_langRu css_classRupygments_styleRu noclassesRtsafeupN(t getiteratort getchildrentlenttagRttexttconfigtmarkdownRt htmlStashtstoreR*R4tclear(Rtroottblockstblocktchildrentcodet placeholder((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pytrunÊs   %        (R9R:R;RO(((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyR=ÇstCodeHiliteExtensioncB@s eZdZd„Zd„ZRS(u4 Add source code hilighting to markdown codeblocks. cC@sùiddgd6tdgd6tdgd6ddgd 6d d gd 6td gd6|_x›|D]“\}}|dkrt}n|dkr”t}n|dkr©d}n|dkrátjdtƒ|rá|jdtƒqán|j||ƒq^WdS(Nu0Use lines numbers. True=yes, False=no, None=autoulinenumsuHDepreciated! Use 'linenums' instead. Force line numbers - Default: Falseu force_linenosu,Automatic language detection - Default: Trueu guess_langu codehiliteu6Set class name for wrapper
- Default: codehiliteu css_classudefaultu>Pygments HTML Formatter Style (Colorscheme) - Default: defaultupygments_styleu8Use inline styles instead of CSS classes - Default falseu noclassesuTrueuFalseuNoneuhThe "force_linenos" config setting to the CodeHilite extension is deprecrecated. Use "linenums" instead.(RR<R4RDtwarningstwarntDeprecationWarningt setConfig(Rtconfigstkeytvalue((sB/usr/lib/python2.7/site-packages/markdown/extensions/codehilite.pyRäs*          cC@sBt|ƒ}|jƒ|_|jjd|dƒ|j|ƒdS(u/ Add HilitePostprocessor to Markdown instance. uhiliteus"     ”)