
    r/jY                    *   d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZmZm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 dd
lmZ ddlmZ ddlmZmZ ddlmZ erd dl m!Z! g dZ" ej#        d          Z$ G d d          Z%	 ddZ&ddZ'dS )    )annotationsN)TYPE_CHECKINGAnyBinaryIOCallableClassVarMappingSequence   )util)build_preprocessors)build_block_parser)build_treeprocessors)build_inlinepatterns)build_postprocessors)	Extension)to_html_stringto_xhtml_string)BLOCK_LEVEL_ELEMENTS)Element)MarkdownmarkdownmarkdownFromFileMARKDOWNc                      e Zd ZU dZdZeedZded<   	 d Z	d%dZ
d&dZd'dZd(dZd%dZd)dZd*dZd+dZ	 	 	 d,d-d$ZdS ).r   a	  
    A parser which converts Markdown to HTML.

    Attributes:
        Markdown.tab_length (int): The number of spaces which correspond to a single tab. Default: `4`.
        Markdown.ESCAPED_CHARS (list[str]): List of characters which get the backslash escape treatment.
        Markdown.block_level_elements (list[str]): List of HTML tags which get treated as block-level elements.
            See [`markdown.util.BLOCK_LEVEL_ELEMENTS`][] for the full list of elements.
        Markdown.registeredExtensions (list[Extension]): List of extensions which have called
            [`registerExtension`][markdown.Markdown.registerExtension] during setup.
        Markdown.doc_tag (str): Element used to wrap document. Default: `div`.
        Markdown.stripTopLevelTags (bool): Indicates whether the `doc_tag` should be removed. Default: 'True'.
        Markdown.references (dict[str, tuple[str, str]]): A mapping of link references found in a parsed document
             where the key is the reference name and the value is a tuple of the URL and title.
        Markdown.htmlStash (util.HtmlStash): The instance of the `HtmlStash` used by an instance of this class.
        Markdown.output_formats (dict[str, Callable[xml.etree.ElementTree.Element]]): A mapping of known output
             formats by name and their respective serializers. Each serializer must be a callable which accepts an
            [`Element`][xml.etree.ElementTree.Element] and returns a `str`.
        Markdown.output_format (str): The output format set by
            [`set_output_format`][markdown.Markdown.set_output_format].
        Markdown.serializer (Callable[xml.etree.ElementTree.Element]): The serializer set by
            [`set_output_format`][markdown.Markdown.set_output_format].
        Markdown.preprocessors (util.Registry): A collection of [`preprocessors`][markdown.preprocessors].
        Markdown.parser (blockparser.BlockParser): A collection of [`blockprocessors`][markdown.blockprocessors].
        Markdown.inlinePatterns (util.Registry): A collection of [`inlinepatterns`][markdown.inlinepatterns].
        Markdown.treeprocessors (util.Registry): A collection of [`treeprocessors`][markdown.treeprocessors].
        Markdown.postprocessors (util.Registry): A collection of [`postprocessors`][markdown.postprocessors].

    div)htmlxhtmlz-ClassVar[dict[str, Callable[[Element], str]]]output_formatsc                   |                     dd          | _        g d| _        	 t          j                    | _        g | _        d| _        d| _        | 	                                 i | _
        t          j                    | _        |                     |                     dg           |                     di                      |                     |                     d	d
                     |                                  dS )a  
        Creates a new Markdown instance.

        Keyword Arguments:
            extensions (list[Extension | str]): A list of extensions.

                If an item is an instance of a subclass of [`markdown.extensions.Extension`][],
                the instance will be used as-is. If an item is of type `str`, it is passed
                to [`build_extension`][markdown.Markdown.build_extension] with its corresponding
                `extension_configs` and the returned instance  of [`markdown.extensions.Extension`][]
                is used.
            extension_configs (dict[str, dict[str, Any]]): Configuration settings for extensions.
            output_format (str): Format of output. Supported formats are:

                * `xhtml`: Outputs XHTML style tags. Default.
                * `html`: Outputs HTML style tags.
            tab_length (int): Length of tabs in the source. Default: `4`

        
tab_length   )\`*_{}[]()>#+-.! T
extensionsextension_configs)r4   configsoutput_formatr   N)getr!   ESCAPED_CHARSr   copyblock_level_elementsregisteredExtensionsdocTypestripTopLevelTagsbuild_parser
referencesr   	HtmlStash	htmlStashregisterExtensionsset_output_formatreset)selfkwargss     T/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/markdown/core.py__init__zMarkdown.__init__X   s    *  &zz,::)
 )
 )
 	M/C/H/J/J!57!'+68)-)9)96::lB+G+G(.

3F(K(K 	  	M 	M 	Mvzz/7CCDDD

    returnc                    t          |           | _        t          |           | _        t	          |           | _        t          |           | _        t          |           | _	        | S )a0  
        Build the parser from the various parts.

        Assigns a value to each of the following attributes on the class instance:

        * **`Markdown.preprocessors`** ([`Registry`][markdown.util.Registry]) -- A collection of
          [`preprocessors`][markdown.preprocessors].
        * **`Markdown.parser`** ([`BlockParser`][markdown.blockparser.BlockParser]) -- A collection of
          [`blockprocessors`][markdown.blockprocessors].
        * **`Markdown.inlinePatterns`** ([`Registry`][markdown.util.Registry]) -- A collection of
          [`inlinepatterns`][markdown.inlinepatterns].
        * **`Markdown.treeprocessors`** ([`Registry`][markdown.util.Registry]) -- A collection of
          [`treeprocessors`][markdown.treeprocessors].
        * **`Markdown.postprocessors`** ([`Registry`][markdown.util.Registry]) -- A collection of
          [`postprocessors`][markdown.postprocessors].

        This method could be redefined in a subclass to build a custom parser which is made up of a different
        combination of processors and patterns.

        )
r   preprocessorsr   parserr   inlinePatternsr   treeprocessorsr   postprocessors)rF   s    rH   r?   zMarkdown.build_parser   sX    * 166(..248824882488rJ   r4   Sequence[Extension | str]r6   Mapping[str, dict[str, Any]]c           	        |D ]}t          |t                    r*|                     ||                    |i                     }t          |t                    rK|                    |            t                              d|j        j	        d|j        j
        d           |Mt          d                    |j        j	        |j        j
        t          j	        t          j
                            | S )a  
        Load a list of extensions into an instance of the `Markdown` class.

        Arguments:
            extensions (list[Extension | str]): A list of extensions.

                If an item is an instance of a subclass of [`markdown.extensions.Extension`][],
                the instance will be used as-is. If an item is of type `str`, it is passed
                to [`build_extension`][markdown.Markdown.build_extension] with its corresponding `configs` and the
                returned instance  of [`markdown.extensions.Extension`][] is used.
            configs (dict[str, dict[str, Any]]): Configuration settings for extensions.

        zSuccessfully loaded extension "r1   z".Nz*Extension "{}.{}" must be of type: "{}.{}")
isinstancestrbuild_extensionr8   r   extendMarkdownloggerdebug	__class__
__module____name__	TypeErrorformat)rF   r4   r6   exts       rH   rC   zMarkdown.registerExtensions   s    $  	 	C#s## F**3C0D0DEE#y)) ""4(((}///1G1G1GI    @GG0#-2H!,i.@    ! rJ   ext_namerV   Mapping[str, Any]r   c                |   t          |          }fdt          j                    D             }|r"|d                                         } |di |S dv r                    dd          ndf\  }	 t          j                  }t                              dz             n0# t          $ r#}dz  }|f|j
        dd         z   |_
         d}~ww xY w|r t          ||          di |S 	  |j        di |S # t          $ r3}|j
        d         }d	d
|}|f|j
        dd         z   |_
         d}~ww xY w)a  
        Build extension from a string name, then return an instance using the given `configs`.

        Arguments:
            ext_name: Name of extension as a string.
            configs: Configuration settings for extension.

        Returns:
            An instance of the extension with the given configuration settings.

        First attempt to load an entry point. The string name must be registered as an entry point in the
        `markdown.extensions` group which points to a subclass of the [`markdown.extensions.Extension`][] class.
        If multiple distributions have registered the same name, the first one found is returned.

        If no entry point is found, assume dot notation (`path.to.module:ClassName`). Load the specified class and
        return an instance. If no class is specified, import the module and call a `makeExtension` function and return
        the [`markdown.extensions.Extension`][] instance returned by that function.
        c                *    g | ]}|j         k    |S  )name).0epra   s     rH   
<listcomp>z,Markdown.build_extension.<locals>.<listcomp>   s&    \\\rS[H[H[H[H[H[rJ   r   :r   r3   z,Successfully imported extension module "%s".zFailed loading extension "%s".NzFailed to initiate extension 'z': re   )dictr   get_installed_extensionsloadsplit	importlibimport_modulerY   rZ   ImportErrorargsgetattrmakeExtensionAttributeError)	rF   ra   r6   entry_pointsr`   
class_namemoduleemessages	    `       rH   rW   zMarkdown.build_extension   s   & w--\\\\T%B%D%D\\\ 	"q/&&((C3>>>>! :=x~~c1555xY[n*	,X66FLL>I     	 	 	6AGZ!&*,AF	
  	.76:..99999+v+66g666!   &)(0'';!afQRRj0s0   61B( (
C2CC1C> >
D;.D66D;	extensionc                :    | j                             |           | S )a  
        Register an extension as having a resettable state.

        Arguments:
            extension: An instance of the extension to register.

        This should get called once by an extension during setup. A "registered" extension's
        `reset` method is called by [`Markdown.reset()`][markdown.Markdown.reset]. Not all extensions have or need a
        resettable state, and so it should not be assumed that all extensions are "registered."

        )r<   appendrF   r{   s     rH   registerExtensionzMarkdown.registerExtension   s      	!((333rJ   c                    | j                                          | j                                         | j        D ]&}t          |d          r|                                 '| S )z
        Resets all state variables to prepare the parser instance for new input.

        Called once upon creation of a class instance. Should be called manually between calls
        to [`Markdown.convert`][markdown.Markdown.convert].
        rE   )rB   rE   r@   clearr<   hasattrr~   s     rH   rE   zMarkdown.reset  sf     	2 	" 	"Iy'** "!!!rJ   r_   c                   |                                                     d          | _        	 | j        | j                 | _        n# t
          $ r}t          | j                                                  }|                                 d| j        ddd	                    |          z   dz   d}|f|j
        dd         z   |_
         d}~ww xY w| S )	z
        Set the output format for the class instance.

        Arguments:
            format: Must be a known value in `Markdown.output_formats`.

        145zInvalid Output Format: "z". Use one of "z", "r1   r   N)lowerrstripr7   r   
serializerKeyErrorlistkeyssortjoinrr   )rF   r_   ry   valid_formatsrz   s        rH   rD   zMarkdown.set_output_format  s     $\\^^22599		"1$2DEDOO 	 	 	 !4!9!9!;!;<<M    %%%]333c999;G Z!&*,AF	 s   A 
CA:C

Ctagr   boolc                    t          |t                    r.|                                                    d          | j        v S dS )z
        Check if the given `tag` is a block level HTML tag.

        Returns `True` for any string listed in `Markdown.block_level_elements`. A `tag` which is
        not a string always returns `False`.

        /F)rU   rV   r   r   r;   )rF   r   s     rH   is_block_levelzMarkdown.is_block_level.  s@     c3 	H99;;%%c**d.GGGurJ   sourcec                   |                                 sdS 	 t          |          }n## t          $ r}|xj        dz  c_         d}~ww xY w|                    d          | _        | j        D ]!}|                    | j                  | _        "| j        	                    | j                  
                                }| j        D ]}|                    |          }||}|                     |          }| j        r	 |                    d| j        z            t!          | j                  z   dz   }|                    d| j        z            }	|||	                                          }nn# t$          $ ra}|                                                     d| j        z            rd}n%t%          d	|                                 z            |Y d}~nd}~ww xY w| j        D ]}
|
                    |          }|                                 S )
a*  
        Convert a Markdown string to a string in the specified output format.

        Arguments:
            source: Markdown formatted text as Unicode or ASCII string.

        Returns:
            A string in the specified output format.

        Markdown parsing takes place in five steps:

        1. A bunch of [`preprocessors`][markdown.preprocessors] munge the input text.
        2. A [`BlockParser`][markdown.blockparser.BlockParser] parses the high-level structural elements of the
           pre-processed text into an [`ElementTree`][xml.etree.ElementTree.ElementTree] object.
        3. A bunch of [`treeprocessors`][markdown.treeprocessors] are run against the
           [`ElementTree`][xml.etree.ElementTree.ElementTree] object. One such `treeprocessor`
           ([`markdown.treeprocessors.InlineProcessor`][]) runs [`inlinepatterns`][markdown.inlinepatterns]
           against the [`ElementTree`][xml.etree.ElementTree.ElementTree] object, parsing inline markup.
        4. Some [`postprocessors`][markdown.postprocessors] are run against the text after the
           [`ElementTree`][xml.etree.ElementTree.ElementTree] object has been serialized into text.
        5. The output is returned as a string.

        !!! warning
            The Python-Markdown library does ***not*** sanitize its HTML output.
            If you are processing Markdown input from an untrusted source, it is your
            responsibility to ensure that it is properly sanitized. For more
            information see [Sanitizing HTML Output](../../sanitization.md).

        r3   z/. -- Note: Markdown only accepts Unicode input!N
z<%s>   z</%s>z<%s />z4Markdown failed to strip top-level tags. Document=%r)striprV   UnicodeDecodeErrorreasonrn   linesrM   runrN   parseDocumentgetrootrP   r   r>   indexdoc_taglenrindex
ValueErrorendswithrQ   )rF   r   ry   preproottreeprocessornewRootoutputstartendpps              rH   convertzMarkdown.convert;  sF   @ ||~~ 	2	[[FF! 	 	 	HHIIHH	 \\$''
& 	. 	.D$*--DJJ {((44<<>> "0 	 	M#''--G" &&! 	RRT\)+ +-0->->?ABCmmGdl$:;;c	*0022 R R R<<>>**8dl+BCC RFF % &9;A<<>>&J K KPQR FFFFR % 	$ 	$BVVF^^FF||~~s.   ( 
AAA?A.E. .
G8AGGNinputstr | BinaryIO | Noner   encoding
str | Nonec                   |pd}|rnt          |t                    rt          |d|          }n t          j        |          |          }|                                }|                                 nt          j                                        }|	                    d          }| 
                    |          }|rt          |t                    rBt          j        |d|d          }|                    |           |                                 nqt          j        |          } ||d          }|                    |           n:|                    |d          }t          j        j                            |           | S )	a  
        Read Markdown text from a file or stream and write HTML output to a file or stream.

        Decodes the input file using the provided encoding (defaults to `utf-8`),
        passes the file content to markdown, and outputs the HTML to either
        the provided stream or the file with provided name, using the same
        encoding as the source file. The
        [`xmlcharrefreplace`](https://docs.python.org/3/library/codecs.html#error-handlers)
        error handler is used when encoding the output.

        **Note:** This is the only place that decoding and encoding of Unicode
        takes place in Python-Markdown.  (All other code is Unicode-in /
        Unicode-out.)

        Arguments:
            input: File object or path. Reads from `stdin` if `None`.
            output: File object or path. Writes to `stdout` if `None`.
            encoding: Encoding of input and output files. Defaults to `utf-8`.

        !!! warning
            The Python-Markdown library does ***not*** sanitize its HTML output.
            As `Markdown.convertFile` writes directly to the file system, there is no
            easy way to sanitize the output from Python code. Therefore, it is
            recommended that the `Markdown.convertFile` method not be used on input
            from an untrusted source.  For more information see [Sanitizing HTML
            Output](../../sanitization.md).

        zutf-8r)moder   u   ﻿wxmlcharrefreplace)r   errors)r   )rU   rV   opencodecs	getreaderreadclosesysstdinlstripr   write	getwriterencodestdoutbuffer)	rF   r   r   r   
input_filetextr   output_filewriters	            rH   convertFilezMarkdown.convertFile  s   F &w  	$%%% ?!%cHEEE

7V-h77>>
??$$D9>>##D{{8$$ ||D!!  	*&#&& 	($k&#3;1DF F F !!$'''!!####)(33$fV4GHHH!!$'''' ;;x)<==DJ##D)))rJ   )rK   r   )r4   rR   r6   rS   rK   r   )ra   rV   r6   rb   rK   r   )r{   r   rK   r   )r_   rV   rK   r   )r   r   rK   r   )r   rV   rK   rV   )NNN)r   r   r   r   r   r   rK   r   )r]   r\   __qualname____doc__r   r   r   r   __annotations__rI   r?   rC   rW   r   rE   rD   r   r   r   re   rJ   rH   r   r   .   s8         < G !!E EN    
) ) )V   8" " " "H3 3 3 3j          .   M M M Mb (,(,#	G G G G G G GrJ   r   r   rV   rG   r   rK   c                D    t          di |}|                    |           S )aw  
    Convert a markdown string to HTML and return HTML as a Unicode string.

    This is a shortcut function for [`Markdown`][markdown.Markdown] class to cover the most
    basic use case.  It initializes an instance of [`Markdown`][markdown.Markdown], loads the
    necessary extensions and runs the parser on the given text.

    Arguments:
        text: Markdown formatted text as Unicode or ASCII string.

    Keyword arguments:
        **kwargs: Any arguments accepted by the Markdown class.

    Returns:
        A string in the specified output format.

    !!! warning
        The Python-Markdown library does ***not*** sanitize its HTML output.
        If you are processing Markdown input from an untrusted source, it is your
        responsibility to ensure that it is properly sanitized. For more
        information see [Sanitizing HTML Output](../../sanitization.md).

    re   )r   r   )r   rG   mds      rH   r   r     s)    0 
		F		B::drJ   c                     t          di | }|                    |                     dd          |                     dd          |                     dd                     dS )a  
    Read Markdown text from a file or stream and write HTML output to a file or stream.

    This is a shortcut function which initializes an instance of [`Markdown`][markdown.Markdown],
    and calls the [`convertFile`][markdown.Markdown.convertFile] method rather than
    [`convert`][markdown.Markdown.convert].

    Keyword arguments:
        input (str | BinaryIO): A file name or readable object.
        output (str | BinaryIO): A file name or writable object.
        encoding (str): Encoding of input and output.
        **kwargs: Any arguments accepted by the `Markdown` class.

    !!! warning
        The Python-Markdown library does ***not*** sanitize its HTML output.
        As `markdown.markdownFromFile` writes directly to the file system, there is no
        easy way to sanitize the output from Python code. Therefore, it is
        recommended that the `markdown.markdownFromFile` function not be used on input
        from an untrusted source.  For more information see [Sanitizing HTML
        Output](../../sanitization.md).

    r   Nr   r   re   )r   r   r8   )rG   r   s     rH   r   r     si    . 
		F		BNN6::gt,,::h--::j$//1 1 1 1 1rJ   )r   rV   rG   r   rK   rV   )rG   r   )(
__future__r   r   r   loggingro   typingr   r   r   r   r   r	   r
   r3   r   rM   r   blockprocessorsr   rP   r   inlinepatternsr   rQ   r   r4   r   serializersr   r   r   xml.etree.ElementTreer   __all__	getLoggerrY   r   r   r   re   rJ   rH   <module>r      s  ( # " " " " "  



      V V V V V V V V V V V V V V V V V V       . . . . . . / / / / / / 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ! ! ! ! ! ! 8 8 8 8 8 8 8 8 & & & & & & .------
6
6
6 
	:	&	&c c c c c c c cL   81 1 1 1 1 1rJ   