\& @sdZddlmZddlmZddlmZddlmZddlZyFdd l m Z dd l m Z m Z mZdd lmZd Z Wnek rd Z YnXddZGdddeZGdddeZGdddeZiddZdS)a  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/) )absolute_import)unicode_literals) Extension) TreeprocessorN) highlight)get_lexer_by_name guess_lexer TextLexer) HtmlFormatterTFc CsD|s gSyttt|jSWntk r?gSYnXdS)zSupport 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)listmapintsplit ValueError)exprr+build/lib/markdown/extensions/codehilite.pyparse_hl_lines#s  rc @sUeZdZdZddddddddddd Zd d Zd d ZdS) CodeHiliteaX 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() NT codehilitedefaultFc Cs[||_||_||_||_||_||_||_||_| pQg|_dS)N) srclanglinenums guess_lang css_classstyle noclasses tab_lengthhl_lines) selfrrrrrrr r!r"rrr__init__Ns        zCodeHilite.__init__cCs|jjd|_|jdkr1|jntryt|j}WnXtk ry(|jrxt|j}n t }Wntk rt }YnXYnXt d|j d|j d|j d|jd|j}t|j||S|jjdd }|jd d }|jd d }|jdd}g}|jr_|jd|jn|j rx|jdnd}|rddj|}nd|j ||fSdS)a7 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.  NZlinenosZcssclassrr r"&z&z>"z"z language-%srz class="%s" z(
%s
)rstripr _parseHeaderpygmentsr rrr r r rrrr r"rreplaceappendjoin)r#Zlexer formattertxtZclassesZ class_strrrrhilite[sB            zCodeHilite.hilitec Cs'ddl}|jjd}|jd}|jd|j}|j|}|ry|jdj|_ Wnt k rd|_ YnX|jdr|j d|n|j dkr|jdrd|_ nt |jd |_n|j d|dj|jd|_dS) aD 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" rNr%a (?:(?:^::+)|(?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))? rpathshebangTr")rerrpopcompileVERBOSEsearchgrouplowerr IndexErrorinsertrrr"r1r,)r#r7linesZflcmrrrr-s$   zCodeHilite._parseHeader)__name__ __module__ __qualname____doc__r$r4r-rrrrr3s    2rc@s"eZdZdZddZdS)HiliteTreeprocessorz% Hilight source code in code blocks. cCs|jd}x|D]}|j}t|dkr|djdkrt|djd|jdd|jdd|jdd|jd d |jd d |jj}|jj j |j d d }|j d|_||_qqWdS)z* Find code blocks and store in htmlStash. prerrcoderrrrpygments_styler r!safeTpN) getiterator getchildrenlentagrtextconfigmarkdownr! htmlStashstorer4clear)r#rootblocksblockZchildrenrI placeholderrrrruns   %        zHiliteTreeprocessor.runN)rCrDrErFr[rrrrrGs rGc@s.eZdZdZddZddZdS)CodeHiliteExtensionz4 Add source code hilighting to markdown codeblocks. cCsiddgd6ddgd6ddgd6d d gd 6d d gd6ddgd6|_x|D]\}}|dkrd}n|dkrd}n|dkrd}n|dkrtjdt|r|jddqn|j||q^WdS)Nz0Use lines numbers. True=yes, False=no, None=autorFzHDepreciated! Use 'linenums' instead. Force line numbers - Default: FalseZ force_linenosTz,Automatic language detection - Default: Truerrz6Set class name for wrapper
- Default: codehiliterrz>Pygments HTML Formatter Style (Colorscheme) - Default: defaultrJz8Use inline styles instead of CSS classes - Default falser TrueFalseNonezhThe "force_linenos" config setting to the CodeHilite extension is deprecrecated. Use "linenums" instead.)rRwarningswarnDeprecationWarning setConfig)r#configskeyvaluerrrr$s*          zCodeHiliteExtension.__init__cCsBt|}|j|_|jjd|d|j|dS)z/ Add HilitePostprocessor to Markdown instance. r4zs"     )