
    $g4                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ e
r	dd	lmc m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n# e$ r dZY nw xY wddZ G d d          Z G d de          Z G d de          Zd Zd	S )z
Adds code/syntax highlighting to standard Python-Markdown code blocks.

See the [documentation](https://Python-Markdown.github.io/extensions/code_hilite)
for details.
    )annotations   )	Extension   )Treeprocessor)parseBoolValue)TYPE_CHECKINGCallableAnyN)	highlight)get_lexer_by_nameguess_lexer)get_formatter_by_name)ClassNotFoundTFexprstrreturn	list[int]c                    | sg S 	 t          t          t          |                                                     S # t          $ r g cY S w xY 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 integers, the line numbers to emphasize.
    )listmapintsplit
ValueError)r   s    ^/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/markdown/extensions/codehilite.pyparse_hl_linesr   *   sZ      	CTZZ\\**+++   			s   3: A	A	c                  ,    e Zd ZdZddZddd	ZddZdS )
CodeHiliteau	  
    Determine language of source code, and pass it on to the Pygments highlighter.

    Usage:

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

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

    Keyword arguments:
        lang (str): String name of Pygments lexer to use for highlighting. Default: `None`.
        guess_lang (bool): Auto-detect which lexer to use.
            Ignored if `lang` is set to a valid value. Default: `True`.
        use_pygments (bool): Pass code to Pygments for code highlighting. If `False`, the code is
            instead wrapped for highlighting by a JavaScript library. Default: `True`.
        pygments_formatter (str): The name of a Pygments formatter or a formatter class used for
            highlighting the code blocks. Default: `html`.
        linenums (bool): An alias to Pygments `linenos` formatter option. Default: `None`.
        css_class (str): An alias to Pygments `cssclass` formatter option. Default: 'codehilite'.
        lang_prefix (str): 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:

    ```python
    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()
    ```

    srcr   c                   || _         |                    dd           | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        |                    dd          | _        d	|vr|                    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
codehilitewrapcodeFfull)r   popr!   r"   r#   r$   r&   options)selfr   r0   s      r   __init__zCodeHilite.__init__s   s     'FD 9 9	 'L$ ? ?")++nd"C"C 'M; G G29++>RTZ2[2[G##!(Z!>!>GIW$$")++k<"H"HGJW$$"&GJ    Tshebangboolr   c                   | j                             d          | _         | j        |r|                                  t          r+| j        r#	 t          | j        fi | j        }nc# t          $ rV 	 | j	        rt          | j         fi | j        }nt          di | j        }n!# t          $ r t          di | j        }Y nw xY wY nw xY w| j        s|j        d         | _        | j         | j         }t          | j        t                    r:	 t!          | j        fi | j        }n5# t"          $ r t!          di | j        }Y nw xY w | j        dd|i| j        }t%          | j         ||          S | j                             dd          }|                    d	d
          }|                    dd          }|                    dd          }g }| j        r3|                    d                    | j        | j                             | j        d         r|                    d           d}|r(d                    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*   )r8   )r'    )r   stripr!   _parseHeaderpygmentsr#   r   r0   r   r"   r   aliasesr$   
isinstancer&   r   r   r   r   replaceappendformatjoin)r1   r4   lexerr9   	formattertxtclasses	class_strs           r   hilitezCodeHilite.hilite   s    8>>$''9 )	) )		F)$)DDt|DD F F FF J +DH E E E E 1 I IDL I I! F F F-EEEEEEEFF 9 -!M!,	*7DI77H$1377 WN 5d6M ^ ^QUQ] ^ ^II$ N N N 5 M M M MIIIN 4D3VVXVVV	TXui888 (""300C++c6**C++c6**C++c8,,CGy Kv}}T-=tyIIJJJ|I& +z***I D)00'1B1BCC	@GGZ(  sH   A& &
C10B"!C"C =C?C  CCD% %EENonec                   ddl }| j                            d          }|                    d          }|                    d|j                  }|                    |          }|r	 |                    d                                          | _	        n# t          $ r
 d| _	        Y nw xY w|                    d          r|                    d|           | j        d         |                    d          r
d	| 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   Nr7   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(   r4   Thl_lines)rer   r   r/   compileVERBOSEsearchgrouplowerr!   
IndexErrorinsertr0   r   rL   rD   )r1   rW   linesflcms         r   rE   zCodeHilite._parseHeader   sa   $ 				 t$$YYq\\JJ  *  HHRLL 	 !GGFOO1133		 ! ! ! 			!wwv $Q###|I&.17793E3E.*.Y''5aggj6I6I'J'JDL$$ LLB99U##))$//s   ',B B('B(N)r   r   )T)r4   r5   r   r   )r   rS   )__name__
__module____qualname____doc__r2   rR   rE   rC   r3   r   r   r   :   sa        6 6p   (9 9 9 9 9v50 50 50 50 50 50r3   r   c                  .    e Zd ZU dZded<   ddZddZdS )HiliteTreeprocessorz' Highlight source code in code blocks. zdict[str, Any]configr8   r   r   c                    |                     dd          }|                     dd          }|                     dd          }|S )zUnescape code.r=   r<   r?   r>   r;   r:   )rI   )r1   r8   s     r   code_unescapez!HiliteTreeprocessor.code_unescape  sB    ||FC((||FC(( ||GS))r3   rootetree.ElementrS   c           	        |                     d          }|D ]}t          |          dk    r|d         j        dk    r| j                                        }|d         j        }|Ot          |                     |          f| j        j	        |
                    dd          d|}| j        j                            |                                          }|                                 d	|_        ||_        dS )
z, Find code blocks and store in `htmlStash`. prer   r   codeNpygments_styledefault)
tab_lengthstylep)iterlentagri   copyr8   r   rk   mdrs   r/   	htmlStashstorerR   clear)r1   rl   blocksblocklocal_configr8   rp   placeholders           r   runzHiliteTreeprocessor.run  s   5!! 	) 	)E5zzQ58<6#9#9#{//11Qx}<!&&t,,#w1&**+;YGG  #	  #g/55dkkmmDD  	(
%	) 	)r3   N)r8   r   r   r   )rl   rm   r   rS   )rc   rd   re   rf   __annotations__rk   r   rC   r3   r   rh   rh      sP         11   ) ) ) ) ) )r3   rh   c                      e Zd ZdZd Zd ZdS )CodeHiliteExtensionz7 Add source code highlighting to markdown code blocks. c           	     D   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 ]i\  }}|| j         v r|                     ||           %t          |t                    r#	 t          |d          }n# t          $ r Y nw xY w|dg| j         |<   jd S )NzOUse lines numbers. True|table|inline=yes, False=no, None=auto. Default: `None`.Tz/Automatic language detection - Default: `True`.r,   z9Set class name for wrapper <div> - Default: `codehilite`.rr   z@Pygments HTML Formatter Style (Colorscheme). Default: `default`.Fz;Use inline styles instead of CSS classes - Default `False`.z\Highlight code blocks with pygments. Disable if using a JavaScript library. Default: `True`.r%   zTPrefix prepended to the language when `use_pygments` is false. Default: `language-`.r'   zDUse a specific formatter for Pygments highlighting. Default: `html`.)r)   r"   r+   rq   	noclassesr#   r$   r&   )preserve_nonerA   )ri   items	setConfigrH   r   r   r   )r1   kwargskeyvalues       r   r2   zCodeHiliteExtension.__init__&  s&    g G Y ] T t s ^#-
 
4 	/ ,,.. 	/ 	/JCdk!!sE**** eS))  .uD I I I%   $)2;C  	/ 	/s   1B
BBc                    t          |          }|                                 |_        |j                            |dd           |                    |            dS )z1 Add `HilitePostprocessor` to Markdown instance. rR      N)rh   
getConfigsri   treeprocessorsregisterregisterExtension)r1   rz   hiliters      r   extendMarkdownz"CodeHiliteExtension.extendMarkdownQ  sU    %b))**
""7Hb999
T"""""r3   N)rc   rd   re   rf   r2   r   rC   r3   r   r   r   #  s9        AA)/ )/ )/V# # # # #r3   r   c                     t          di | S )NrC   )r   )r   s    r   makeExtensionr   Z  s    (((((r3   )r   r   r   r   )rf   
__future__r   rA   r   r   r   utilr   typingr	   r
   r   xml.etree.ElementTreeetreeElementTreerF   r   pygments.lexersr   r   pygments.formattersr   pygments.utilr   ImportErrorr   r   rh   r   r   rC   r3   r   <module>r      s    # " " " " "       * * * * * * ! ! ! ! ! ! / / / / / / / / / / *)))))))))"""""">>>>>>>>999999++++++HH   HHH    }0 }0 }0 }0 }0 }0 }0 }0F#) #) #) #) #)- #) #) #)L4# 4# 4# 4# 4#) 4# 4# 4#n) ) ) ) )s   A AA