o
    7d2                     @   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y=   dZY nw 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   s4   | sg S z
t tt|  W S  ty   g  Y S w )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.
    )listmapintsplit
ValueError)expr r   uC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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' 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r2|d
d |d	< d|vr>|dd|d< d|vrFd|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r| jrzt| jfi | j}W n3 tyV   z| j	r9t
| j fi | j}ntdi | j}W n tyS   tdi | j}Y nw Y nw | js`|jd | _| j | j }t| jtrzt| jfi | j}W n ty   tdi | j}Y nw | jd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}|rdd|}d| jd ||S )a6  
        Pass code to the [Pygments](https://pygments.org/) highlighter 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{   sZ   


zCodeHilite.hilitec                 C   s   ddl }| jd}|d}|d|j}||}|r[z
|d | _	W n t
y3   d| _	Y nw |dr?|d| | jd du rP|drPd	| jd< t|d
| jd
< n|d| d|d| _dS )aP  
        Determines language of a code block from shebang line and whether the
        said line should be removed or left in place. If the shebang 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                   @       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rE|d jdkrE| 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   rW   r)   mdr\   r$   Z	htmlStashstorerC   clear)r&   rootblocksblockZlocal_configrY   placeholderr   r   r   run   s$   


zHiliteTreeprocessor.runN)rQ   rR   rS   rT   rW   rk   r   r   r   r   rV      s    	rV   c                   @   rU   )CodeHiliteExtensionz7 Add source code highlighting to markdown code blocks. 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 r0| || q t|trGzt|dd}W n	 tyF   Y nw |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: codehiliter[   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    rZ   Z	noclassesr   r   r   )Zpreserve_noner2   )rb   itemsZ	setConfigr8   r9   r   r   )r&   kwargskeyvaluer   r   r   r'     sH   

zCodeHiliteExtension.__init__c                 C   s0   t |}|  |_|j|dd ||  dS )z1 Add `HilitePostprocessor` to Markdown instance. rC      N)rV   Z
getConfigsrb   treeprocessorsregisterZregisterExtension)r&   rd   Zhiliterr   r   r   extendMarkdown@  s   
z"CodeHiliteExtension.extendMarkdownN)rQ   rR   rS   rT   r'   rt   r   r   r   r   rl     s    +rl   c                  K   s   t di | S )Nr   )rl   )rn   r   r   r   makeExtensionI  s   ru   N)rT   r2   r   rr   r   utilr   r6   r   Zpygments.lexersr   r	   Zpygments.formattersr
   Zpygments.utilr   ImportErrorr   r   rV   rl   ru   r   r   r   r   <module>   s&     B!7