a
    
d9                     @   s   d Z ddlZddlZddlZddl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 g dZedZG dd dZdd Zdd ZdS )a  
Python Markdown

A Python implementation of John Gruber's Markdown.

Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).
    N   )util)build_preprocessors)build_block_parser)build_treeprocessors)build_inlinepatterns)build_postprocessors)	Extension)to_html_stringto_xhtml_string)BLOCK_LEVEL_ELEMENTS)MarkdownmarkdownmarkdownFromFileZMARKDOWNc                   @   sp   e Zd ZdZdZee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 ZdddZdS )r   zConvert Markdown to HTML.div)htmlxhtmlc                 K   s   | dd| _g d| _t | _g | _d| _d| _| 	  i | _
t | _| j| dg | di d | | d	d
 |   dS )ay  
        Creates a new Markdown instance.

        Keyword arguments:

        * `extensions`: A list of extensions.
            If an item is an instance of a subclass of `markdown.extension.Extension`, the  instance will be used
            as-is. If an item is of type string, first an entry point will be loaded. If that fails, the string is
            assumed to use Python dot notation (`path.to.module:ClassName`) to load a `markdown.Extension` subclass. If
            no class is specified, then a `makeExtension` function is called within the specified module.
        * `extension_configs`: Configuration settings for extensions.
        * `output_format`: Format of output. Supported formats are:
            * `xhtml`: Outputs XHTML style tags. Default.
            * `html`: Outputs HTML style tags.
        * `tab_length`: Length of tabs in the source. Default: 4

        
tab_length   )\`*_{}[]()>#+-.! T
extensionsZextension_configs)r&   configsoutput_formatr   N)getr   ZESCAPED_CHARSr   copyblock_level_elementsregisteredExtensionsZdocTypestripTopLevelTagsbuild_parser
referencesr   Z	HtmlStash	htmlStashregisterExtensionsset_output_formatreset)selfkwargs r6   V/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/markdown/core.py__init__4   s    



zMarkdown.__init__c                 C   s6   t | | _t| | _t| | _t| | _t| | _	| S )z* Build the parser from the various parts. )
r   preprocessorsr   parserr   ZinlinePatternsr   treeprocessorsr   postprocessors)r4   r6   r6   r7   r.   [   s    




zMarkdown.build_parserc                 C   s   |D ]z}t |tr&| |||i }t |trV||  td|jj	|jj
f  q|durtd|jj	|jj
tj	tj
q| S )a  
        Register extensions with this instance of Markdown.

        Keyword arguments:

        * `extensions`: A list of extensions, which can either
           be strings or objects.
        * `configs`: A dictionary mapping extension names to `configs` options.

        z&Successfully loaded extension "%s.%s".Nz*Extension "{}.{}" must be of type: "{}.{}")
isinstancestrbuild_extensionr)   r	   ZextendMarkdownloggerdebug	__class__
__module____name__	TypeErrorformat)r4   r&   r'   extr6   r6   r7   r1   d   s$    


zMarkdown.registerExtensionsc           	   
      sB  t |} fddt D }|r<|d  }|f i |S d v rP ddn df\ }zt }td   W nD t	y } z,d  }|f|j
dd	  |_
 W Y d	}~n
d	}~0 0 |rt||f i |S z|jf i |W S  ty< } z:|j
d }d
 |f }|f|j
dd	  |_
 W Y d	}~n
d	}~0 0 d	S )a  
        Build extension from a string name, then return an instance.

        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 `Extension` instance returned by that function.
        c                    s   g | ]}|j  kr|qS r6   )name).0epext_namer6   r7   
<listcomp>       z,Markdown.build_extension.<locals>.<listcomp>r   :r   r%   z,Successfully imported extension module "%s".zFailed loading extension "%s".Nz%Failed to initiate extension '%s': %s)dictr   Zget_installed_extensionsloadsplit	importlibimport_moduler@   rA   ImportErrorargsgetattrZmakeExtensionAttributeError)	r4   rL   r'   Zentry_pointsrG   
class_namemoduleemessager6   rK   r7   r?      s4     

zMarkdown.build_extensionc                 C   s   | j | | S )z# This gets called by the extension )r,   appendr4   	extensionr6   r6   r7   registerExtension   s    zMarkdown.registerExtensionc                 C   s6   | j   | j  | jD ]}t|dr|  q| S )zR
        Resets all state variables so that we can start with a new text.
        r3   )r0   r3   r/   clearr,   hasattrr^   r6   r6   r7   r3      s    




zMarkdown.resetc              
   C   s   |  d| _z| j| j | _W nn ty } zVt| j }|  d| jdd	| d f }|f|j
dd  |_
 W Y d}~n
d}~0 0 | S )z/ Set the output format for the class instance. Z145z+Invalid Output Format: "%s". Use one of %s."z", "r   N)lowerrstripr(   output_formats
serializerKeyErrorlistkeyssortjoinrV   )r4   rF   r[   Zvalid_formatsr\   r6   r6   r7   r2      s    zMarkdown.set_output_formatc                 C   s"   t |tr| d| jv S dS )z+Check if the tag is a block level HTML tag./F)r=   r>   rd   re   r+   )r4   tagr6   r6   r7   is_block_level   s    
zMarkdown.is_block_levelc              
   C   sr  |  sdS zt|}W n4 tyL } z| jd7  _ W Y d}~n
d}~0 0 |d| _| jD ]}|| j| _q`| j	| j
 }| jD ]}||}|dur|}q| |}| jrRzB|d| j t| j d }|d| j }	|||	   }W nT tyP } z:|  d| j r*d}ntd	|   |W Y d}~n
d}~0 0 | jD ]}
|
|}qX|  S )
a  
        Convert markdown to serialized XHTML or HTML.

        Keyword arguments:

        * `source`: Source text as a Unicode string.

        Markdown processing takes place in five steps:

        1. A bunch of `preprocessors` munge the input text.
        2. `BlockParser()` parses the high-level structural elements of the
           pre-processed text into an `ElementTree`.
        3. A bunch of `treeprocessors` are run against the `ElementTree`. One
           such `treeprocessor` runs `InlinePatterns` against the `ElementTree`,
           detecting inline markup.
        4. Some post-processors are run against the text after the `ElementTree`
           has been serialized into text.
        5. The output is written to a string.

        r%   z/. -- Note: Markdown only accepts Unicode input!N
z<%s>   z</%s>z<%s />z4Markdown failed to strip top-level tags. Document=%r)stripr>   UnicodeDecodeErrorreasonrR   linesr9   runr:   ZparseDocumentgetrootr;   rg   r-   indexdoc_taglenrindex
ValueErrorendswithr<   )r4   sourcer[   preprootZtreeprocessorZnewRootoutputstartendppr6   r6   r7   convert   sL    




zMarkdown.convertNc           	      C   s  |pd}|rHt |tr(tj|d|d}nt||}| }|  ntj }t |tsf|	|}|
d}| |}|rt |trtj|d|dd}|| |  n t|}||dd}|| n@||d}ztjj| W n  ty   tj| Y n0 | S )	aM  Converts a markdown file and returns the HTML as a Unicode string.

        Decodes the 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` 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.)

        Keyword 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`.

        zutf-8r)modeencodingu   ﻿wxmlcharrefreplace)r   errors)r   )r=   r>   codecsopen	getreaderreadclosesysstdindecodelstripr   write	getwriterencodestdoutbufferrX   )	r4   inputr   r   Z
input_filetextr   Zoutput_filewriterr6   r6   r7   convertFile  s:    










zMarkdown.convertFile)NNN)rD   rC   __qualname____doc__ry   r
   r   rf   r8   r.   r1   r?   r`   r3   r2   ro   r   r   r6   r6   r6   r7   r   *   s   '	.Fr   c                 K   s   t f i |}|| S )a  Convert a markdown string to HTML and return HTML as a Unicode string.

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

    Keyword arguments:

    * `text`: Markdown formatted text as Unicode or ASCII string.
    * Any arguments accepted by the Markdown class.

    Returns: An HTML document as a string.

    )r   r   )r   r5   mdr6   r6   r7   r   i  s    r   c                  K   s8   t f i | }|| dd| dd| dd dS )a  Read markdown code from a file and write it to a file or a stream.

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

    Keyword arguments:

    * `input`: a file name or readable object.
    * `output`: a file name or writable object.
    * `encoding`: Encoding of input and output.
    * Any arguments accepted by the `Markdown` class.

    r   Nr   r   )r   r   r)   )r5   r   r6   r6   r7   r   |  s
    

r   )r   r   r   loggingrS   r%   r   r9   r   Zblockprocessorsr   r;   r   Zinlinepatternsr   r<   r   r&   r	   Zserializersr
   r   r   __all__	getLoggerr@   r   r   r   r6   r6   r6   r7   <module>   s(   
  A