3 \& @sdZddlmZddlmZddlmZddlmZddlZy4dd l m Z dd l m Z m Z mZdd lmZd Z Wnek rd Z YnXddZGdddeZGdddeZGdddeZifddZdS)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 Cs4|sgSyttt|jStk r.gSXdS)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 @s*eZdZdZddd 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>||_||_||_||_||_||_||_||_| p6g|_dS)N) srclanglinenums guess_lang css_classstyle noclasses tab_lengthhl_lines) selfrrrrrrr r!r"rrr__init__NszCodeHilite.__init__cCs4|jjd|_|jdkr |jtryt|j}WnLtk r~y|jrVt|j}nt }Wntk rxt }YnXYnXt |j |j |j |j|jd}t|j||S|jjdd}|jdd}|jdd }|jd d }g}|jr|jd |j|j r|jd d}|r ddj|}d|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.  N)ZlinenosZcssclassrr r"&z&z>"z"z language-%srz class="%s" z(
%s
)rstripr _parseHeaderpygmentsr rrr r r rrrr r"rreplaceappendjoin)r#Zlexer formattertxtclassesZ class_strrrrhilite[sB         zCodeHilite.hilitec Csddl}|jjd}|jd}|jd|j}|j|}|ry|jdj|_ Wnt k rhd|_ YnX|jdr|j d||j dkr|jdrd|_ t |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#r8linesZflcmrrrr-s$        zCodeHilite._parseHeader) NNTrNrFrN)__name__ __module__ __qualname____doc__r$r5r-rrrrr3s  2rc@seZdZdZddZdS)HiliteTreeprocessorz% Hilight source code in code blocks. c Cs|jd}x|D]}|j}t|dkr|djdkrt|dj|jd|jd|jd|jd|jd |jjd }|jj j |j d d }|j d |_||_qWdS)z* Find code blocks and store in htmlStash. prerrcoderrrpygments_styler )rrrrr r!T)safepN) getiterator getchildrenlentagrtextconfigmarkdownr! htmlStashstorer5clear)r#rootblocksblockZchildrenrJ placeholderrrrruns     zHiliteTreeprocessor.runN)rDrErFrGr\rrrrrHsrHc@s eZdZdZddZddZdS)CodeHiliteExtensionz4 Add source code hilighting to markdown codeblocks. cCsddgddgddgddgdd gdd gd |_xd|D]\\}}|d krFd}|d krRd}|dkr^d}|dkrtjdt|r|jdd|j||q2WdS)Nz0Use lines numbers. True=yes, False=no, None=autoFzHDepreciated! Use 'linenums' instead. Force line numbers - Default: FalseTz,Automatic language detection - Default: Truerz6Set class name for wrapper
- Default: codehiliterz>Pygments HTML Formatter Style (Colorscheme) - Default: defaultz8Use inline styles instead of CSS classes - Default false)r force_linenosrrrKr TrueFalseNoner^zhThe "force_linenos" config setting to the CodeHilite extension is deprecrecated. Use "linenums" instead.r)rSwarningswarnDeprecationWarning setConfig)r#configskeyvaluerrrr$s( zCodeHiliteExtension.__init__cCs0t|}|j|_|jjd|d|j|dS)z/ Add HilitePostprocessor to Markdown instance. r5zs$       )