
    /Vh4                       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ddZ G d d      Z G d de      Z G d de      Zd Zy	# e$ r dZY 1w xY w)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)ClassNotFoundTFc                |    | sg S 	 t        t        t        | j                                     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)exprs    N/home/dcms/DCMS/lib/python3.12/site-packages/markdown/extensions/codehilite.pyparse_hl_linesr   *   s=     	CTZZ\*++ 	s   &- ;;c                  *    e Zd ZdZddZdddZd	dZy)

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

    c                   || _         |j                  dd       | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        |j                  dd      | _        d	|vr|j                  d
d       |d	<   d|vr|j                  dd      |d<   d|vrd|d<   d|d<   || _        y )Nlang
guess_langTuse_pygmentslang_prefix	language-pygments_formatterhtmllinenoslinenumscssclass	css_class
codehilitewrapcodeFfull)srcpopr   r   r   r    r"   options)selfr+   r-   s      r   __init__zCodeHilite.__init__s   s     'FD 9	 'L$ ?")++nd"C 'M; G29++>RTZ2[G#!(Z!>GIW$")++k<"HGJW$"&GJ    c                f   | j                   j                  d      | _         | j                  |r| j                          t        r| j
                  r	 t        | j                  fi | j                  }| j                  s|j                  d   | _        | j                   | j                   }t        | j                  t              r"	 t!        | j                  fi | j                  }n | j                  dd|i| j                  }t%        | j                   ||      S | j                   j'                  dd      }|j'                  dd      }|j'                  dd	      }|j'                  d
d      }g }| j                  r5|j)                  dj+                  | j                  | j                               | j                  d   r|j)                  d       d}|r dj+                  dj-                  |            }dj+                  | j                  d   ||      S # t        $ rl 	 | 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 w xY w# t"        $ r t!        di | j                  }Y w xY w)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.

        
r   lang_str&&amp;<&lt;>&gt;"z&quot;z{}{}r$   r%    z class="{}" z)<pre class="{}"><code{}>{}
</code></pre>
r&   )text)r#    )r+   stripr   _parseHeaderpygmentsr   r   r-   r   r   r   aliasesr    
isinstancer"   strr   r   r   replaceappendformatjoin)r.   shebanglexerr3   	formattertxtclasses	class_strs           r   hilitezCodeHilite.hilite   sP    88>>$'99))	F)$))Dt||D 99!MM!,	**+DII;7H$1137N 5d6M6M ^QUQ]Q] ^I 4D33VXVV	TXXui88 ((""30C++c6*C++c6*C++c8,CGyyv}}T-=-=tyyIJ||I&z*I)00'1BC	@GGZ( E  FF +DHH E E 1 IDLL I! F-EEEFF % N 5 M MINsI    H  J 	J AI#"J#JJJJ
JJ0/J0c                   ddl }| j                  j                  d      }|j                  d      }|j	                  d|j
                        }|j                  |      }|r	 |j                  d      j                         | _	        |j                  d      r|j                  d|       | j                  d    |j                  d      rd	| j                  d<   t        |j                  d
            | j                  d
<   n|j                  d|       dj                  |      j                  d      | _        y# t        $ r
 d| _	        Y w xY w)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   Nr2   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$   rI   Thl_lines)rer+   r   r,   compileVERBOSEsearchgrouplowerr   
IndexErrorinsertr-   r   rH   r?   )r.   rS   linesflcms         r   r@   zCodeHilite._parseHeader   s   $ 	 t$YYq\JJ  ** HHRL!GGFO113	 wwvQ#||I&.17793E*.Y''5aggj6I'JDLL$ LLB99U#))$/  ! 	!s   !$D7 7E
	E
N)r+   rD   )T)rI   boolreturnrD   )r`   None)__name__
__module____qualname____doc__r/   rO   r@   r>   r0   r   r   r   :   s    6p(9v50r0   r   c                  ,    e Zd ZU dZded<   ddZddZy)	HiliteTreeprocessorz' Highlight source code in code blocks. zdict[str, Any]configc                r    |j                  dd      }|j                  dd      }|j                  dd      }|S )zUnescape code.r7   r6   r9   r8   r5   r4   )rE   )r.   r=   s     r   code_unescapez!HiliteTreeprocessor.code_unescape  s9    ||FC(||FC( ||GS)r0   c           	        |j                  d      }|D ]  }t        |      dk(  s|d   j                  dk(  s%| j                  j	                         }|d   j
                  }|Qt        | j                  |      f| j                  j                  |j                  dd      d|}| j                  j                  j                  |j                               }|j                          d	|_        ||_         y)
z, Find code blocks and store in `htmlStash`. prer   r   codeNpygments_styledefault)
tab_lengthstylep)iterlentagrh   copyr=   r   rj   mdrp   r,   	htmlStashstorerO   clear)r.   rootblocksblocklocal_configr=   rm   placeholders           r   runzHiliteTreeprocessor.run  s    5! 	)E5zQ58<<6#9#{{//1Qx}}<!&&t,#ww11&**+;YG #	 #gg//55dkkmD  	(
%	)r0   N)r=   rD   r`   rD   )r{   zetree.Elementr`   ra   )rb   rc   rd   re   __annotations__rj   r   r>   r0   r   rg   rg      s    1)r0   rg   c                      e Zd ZdZd Zd Zy)CodeHiliteExtensionz7 Add source code highlighting to markdown code blocks. c           	     4   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| _         	 |j                         D ]U  \  }}|| j                   v r| j                  ||       't        |t              r	 t        |d      }|dg| j                   |<   W y # t        $ r Y w xY w)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`.ro   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'   rn   	noclassesr   r    r"   )preserve_noner;   )rh   items	setConfigrC   rD   r   r   )r.   kwargskeyvalues       r   r/   zCodeHiliteExtension.__init__&  s     g G Y ] T t s ^#-
4 	/ ,,. 	/JCdkk!sE* eS) .uD I %*2;C 	/ & s   *B	BBc                    t        |      }| j                         |_        |j                  j	                  |dd       |j                  |        y)z1 Add `HilitePostprocessor` to Markdown instance. rO      N)rg   
getConfigsrh   treeprocessorsregisterregisterExtension)r.   rw   hiliters      r   extendMarkdownz"CodeHiliteExtension.extendMarkdownQ  s@    %b)*
""7Hb9
T"r0   N)rb   rc   rd   re   r/   r   r>   r0   r   r   r   #  s    A)/V#r0   r   c                     t        di | S )Nr>   )r   )r   s    r   makeExtensionr   Z  s    (((r0   )r   rD   r`   z	list[int])re   
__future__r   r;   r   r   r   utilr   typingr	   r
   r   xml.etree.ElementTreeetreeElementTreerA   r   pygments.lexersr   r   pygments.formattersr   pygments.utilr   ImportErrorr   r   rg   r   r   r>   r0   r   <module>r      s    #  * ! / /))">9+H
 }0 }0F#)- #)L4#) 4#n)i	  Hs   A5 5A?>A?