a
    t=ico2  ã                   @   s¾   d Z ddlmZ ddlmZ ddlmZ z<ddlmZ ddl	m
Z
mZ dd	lmZ dd
lmZ dZW n eyz   dZY n0 dd„ ZG dd„ dƒZG dd„ deƒZG dd„ deƒZdd„ ZdS )aº  
CodeHilite Extension for Python-Markdown
========================================

Adds code/syntax highlighting to standard Python-Markdown code blocks.

See <https://Python-Markdown.github.io/extensions/code_hilite>
for documentation.

Original code Copyright 2006-2008 [Waylan Limberg](http://achinghead.com/).

All changes Copyright 2008-2014 The Python Markdown Project

License: [BSD](https://opensource.org/licenses/bsd-license.php)

é   )Ú	Extensioné   )ÚTreeprocessor)ÚparseBoolValueé    )Ú	highlight)Úget_lexer_by_nameÚguess_lexer)Úget_formatter_by_name)ÚClassNotFoundTFc                 C   s8   | sg S zt tt|  ¡ ƒƒW S  ty2   g  Y S 0 dS )zÄ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)ÚlistÚmapÚintÚsplitÚ
ValueError)Úexpr© r   úo/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/markdown/extensions/codehilite.pyÚparse_hl_lines    s    r   c                   @   s*   e Zd ZdZdd„ Zd
dd„Zdd„ Zd	S )Ú
CodeHilitea	  
    Determine language of source code, and pass it on to the Pygments highlighter.

    Usage:
        code = CodeHilite(src=some_code, lang='python')
        html = code.hilite()

    Arguments:
    * src: Source string or any object with a .readline attribute.

    * lang: String name of Pygments lexer to use for highlighting. Default: `None`.

    * guess_lang: Auto-detect which lexer to use. Ignored if `lang` is set to a valid
      value. Default: `True`.

    * use_pygments: Pass code to pygments for code highlighting. If `False`, the code is
      instead wrapped for highlighting by a JavaScript library. Default: `True`.

    * pygments_formatter: The name of a Pygments formatter or a formatter class used for
      highlighting the code blocks. Default: `html`.

    * linenums: An alias to Pygments `linenos` formatter option. Default: `None`.

    * css_class: An alias to Pygments `cssclass` formatter option. Default: 'codehilite'.

    * lang_prefix: Prefix prepended to the language. Default: "language-".

    Other Options:
    Any other options are accepted and passed on to the lexer and formatter. Therefore,
    valid options include any options which are accepted by the `html` formatter or
    whichever lexer the code's language uses. Note that most lexers do not have any
    options. However, a few have very useful options, such as PHP's `startinline` option.
    Any invalid options are ignored without error.

    Formatter options: https://pygments.org/docs/formatters/#HtmlFormatter
    Lexer Options: https://pygments.org/docs/lexers/

    Additionally, when Pygments is enabled, the code's language is passed to the
    formatter as an extra option `lang_str`, whose value being `{lang_prefix}{lang}`.
    This option has no effect to the Pygments's builtin formatters.

    Advanced Usage:
        code = CodeHilite(
            src = some_code,
            lang = 'php',
            startinline = True,      # Lexer option. Snippet does not start with `<?php`.
            linenostart = 42,        # Formatter option. Snippet starts on line 42.
            hl_lines = [45, 49, 50], # Formatter option. Highlight lines 45, 49, and 50.
            linenos = 'inline'       # Formatter option. Avoid alignment problems.
        )
        html = code.hilite()

    c                 K   sž   || _ | dd ¡| _| dd¡| _| dd¡| _| dd¡| _| dd¡| _d	|vrd| d
d ¡|d	< d|vr|| dd¡|d< d|vrŒd|d< d|d< || _d S )NÚlangÚ
guess_langTÚuse_pygmentsÚlang_prefixú	language-Úpygments_formatterÚhtmlÚlinenosÚlinenumsÚcssclassÚ	css_classÚ
codehiliteZwrapcodeFÚfull)ÚsrcÚpopr   r   r   r   r   Úoptions)Úselfr#   r%   r   r   r   Ú__init__g   s    zCodeHilite.__init__Tc                 C   sâ  | j  d¡| _ | jdu r$|r$|  ¡  trF| jrFzt| jfi | j¤Ž}W nf ty°   z0| j	rvt
| j fi | j¤Ž}ntdi | j¤Ž}W n" tyª   tdi | j¤Ž}Y n0 Y n0 | jsÄ|jd | _| j› | j› }t| jtƒr"zt| jfi | j¤Ž}W n$ ty   tdi | j¤Ž}Y n0 n| jf d|i| j¤Ž}t| j ||ƒS | j  dd¡}| d	d
¡}| dd¡}| dd¡}g }| jrš| d | j| j¡¡ | jd r°| d¡ d}|rÊd d |¡¡}d | jd ||¡S dS )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.: <span class="k">).

        returns : A string of html.

        Ú
NÚtextr   r   Úlang_strú&ú&amp;ú<ú&lt;ú>ú&gt;ú"z&quot;z{}{}r   r   Ú z class="{}"ú z)<pre class="{}"><code{}>{}
</code></pre>
r   )r)   )r)   )r   )r#   Ústripr   Ú_parseHeaderÚpygmentsr   r   r%   r   r   r	   Úaliasesr   Ú
isinstancer   Ústrr
   r   r   ÚreplaceÚappendÚformatÚjoin)r&   ÚshebangÚlexerr*   Ú	formatterÚtxtÚclassesZ	class_strr   r   r   Úhilite{   sP    
ýzCodeHilite.hilitec                 C   sØ   ddl }| j d¡}| d¡}| d|j¡}| |¡}|r¶z| d¡ ¡ | _	W n t
yf   d| _	Y n0 | d¡r~| d|¡ | jd du r | d¡r d	| jd< t| d
¡ƒ| jd
< n| d|¡ d |¡ d¡| _dS )aH  
        Determines language of a code block from shebang line and whether the
        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 identification 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"
        r   Nr(   aœ  
            (?:(?:^::+)|(?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))?
            r   Úpathr   r>   TZhl_lines)Úrer#   r   r$   ÚcompileÚVERBOSEÚsearchÚgroupÚlowerr   Ú
IndexErrorÚinsertr%   r   r=   r4   )r&   rE   ÚlinesÚflÚcÚmr   r   r   r5   ¶   s&    
ù	


zCodeHilite._parseHeaderN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   rC   r5   r   r   r   r   r   0   s   6
;r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚHiliteTreeprocessorz' Highlight source code in code blocks. c                 C   s(   |  dd¡}|  dd¡}|  dd¡}|S )zUnescape code.r.   r-   r0   r/   r,   r+   )r:   )r&   r)   r   r   r   Úcode_unescapeô   s    z!HiliteTreeprocessor.code_unescapec                 C   s   |  d¡}|D ]|}t|ƒdkr|d jdkr| j ¡ }t|  |d j¡f| jj	| 
dd¡dœ|¤Ž}| jj | ¡ ¡}| ¡  d|_||_qd	S )
z* Find code blocks and store in htmlStash. Úprer   r   ÚcodeÚpygments_styleÚdefault)Ú
tab_lengthÚstyleÚpN)ÚiterÚlenÚtagÚconfigÚcopyr   rV   r)   Úmdr[   r$   Z	htmlStashÚstorerC   Úclear)r&   ÚrootÚblocksÚblockZlocal_configrX   Úplaceholderr   r   r   Úruný   s     

ÿ
ýüzHiliteTreeprocessor.runN)rQ   rR   rS   rT   rV   rj   r   r   r   r   rU   ñ   s   	rU   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCodeHiliteExtensionz6 Add source code highlighting to markdown codeblocks. c              	   K   s¢   d dgddgddgddgdd	gdd
gddgddgdœ| _ | ¡ D ]\\}}|| j v r`|  ||¡ q@t|tƒrŽzt|dd}W n tyŒ   Y n0 |dg| j |< q@d S )Nz=Use lines numbers. True|table|inline=yes, False=no, None=autoTz,Automatic language detection - Default: Truer!   z6Set class name for wrapper <div> - Default: codehiliterZ   z>Pygments HTML Formatter Style (Colorscheme) - Default: defaultFz8Use inline styles instead of CSS classes - Default falsez[Use Pygments to Highlight code blocks. Disable if using a JavaScript library. Default: Truer   zQPrefix prepended to the language when use_pygments is false. Default: "language-"r   zBUse a specific formatter for Pygments highlighting.Default: "html")r   r   r    rY   Z	noclassesr   r   r   )Zpreserve_noner2   )ra   ÚitemsZ	setConfigr8   r9   r   r   )r&   ÚkwargsÚkeyÚvaluer   r   r   r'     sD    ÿÿÿÿÿÿþÿê

zCodeHiliteExtension.__init__c                 C   s0   t |ƒ}|  ¡ |_|j |dd¡ | | ¡ dS )z/ Add HilitePostprocessor to Markdown instance. rC   é   N)rU   Z
getConfigsra   ÚtreeprocessorsÚregisterZregisterExtension)r&   rc   Zhiliterr   r   r   ÚextendMarkdown@  s    
z"CodeHiliteExtension.extendMarkdownN)rQ   rR   rS   rT   r'   rs   r   r   r   r   rk     s   +rk   c                  K   s   t f i | ¤ŽS )N)rk   )rm   r   r   r   ÚmakeExtensionI  s    rt   N)rT   r2   r   rq   r   Úutilr   r6   r   Zpygments.lexersr   r	   Zpygments.formattersr
   Zpygments.utilr   ÚImportErrorr   r   rU   rk   rt   r   r   r   r   Ú<module>   s"   
 B!7