a
    Sic.<                     @   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 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)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| _g d| _g | _d| _d| _|   i | _t	
 | _| j| dg | di d	 | | d
d |   dS )ao  
        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   )\`*_{}[]()>#+-.!)9addressZarticleZasideZ
blockquotedetailsr   ZdlfieldsetZ
figcaptionZfigurefooterformZh1Zh2Zh3Zh4Zh5Zh6headerZhgrouphrmainZmenuZnavZolppresectiontableulZcanvasZcolgroupddbodydtgroupZiframeZlilegendmathmapZnoscriptoutputobjectoptionprogressscriptstylesummaryZtbodytdZtextareaZtfootthZtheadtrZvideo T
extensionsextension_configs)rC   configsoutput_formatr   N)getr   ZESCAPED_CHARSblock_level_elementsregisteredExtensionsZdocTypestripTopLevelTagsbuild_parser
referencesr   Z	HtmlStash	htmlStashregisterExtensionsset_output_formatreset)selfkwargs rS   I/var/www/html/django/DPS/env/lib/python3.9/site-packages/markdown/core.py__init__3   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)rQ   rS   rS   rT   rK   e   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 config options.

        z&Successfully loaded extension "%s.%s".Nz*Extension "{}.{}" must be of type: "{}.{}")
isinstancestrbuild_extensionrG   r	   ZextendMarkdownloggerdebug	__class__
__module____name__	TypeErrorformat)rQ   rC   rE   extrS   rS   rT   rN   n   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 rS   )name).0epext_namerS   rT   
<listcomp>       z,Markdown.build_extension.<locals>.<listcomp>r   :r   rB   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]   r^   ImportErrorargsgetattrZmakeExtensionAttributeError)	rQ   ri   rE   Zentry_pointsrd   
class_namemoduleemessagerS   rh   rT   r\      s4     

zMarkdown.build_extensionc                 C   s   | j | | S )z# This gets called by the extension )rI   appendrQ   	extensionrS   rS   rT   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.
        rP   )rM   rP   rL   clearrI   hasattrr{   rS   rS   rT   rP      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rstriprF   output_formats
serializerKeyErrorlistkeyssortjoinrs   )rQ   rc   rx   Zvalid_formatsry   rS   rS   rT   rO      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)rZ   r[   r   r   rH   )rQ   tagrS   rS   rT   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.

        rB   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reasonro   linesrV   runrW   ZparseDocumentZgetrootrX   r   rJ   indexdoc_taglenrindex
ValueErrorendswithrY   )rQ   sourcerx   preprootZtreeprocessorZnewRootr8   startendpprS   rS   rT   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 )	a?  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   )rZ   r[   codecsopen	getreaderreadclosesysstdindecodelstripr   write	getwriterencodestdoutbufferru   )	rQ   inputr8   r   Z
input_filetextr   Zoutput_filewriterrS   rS   rT   convertFile'  s:    










zMarkdown.convertFile)NNN)ra   r`   __qualname____doc__r   r
   r   r   rU   rK   rN   r\   r}   rP   rO   r   r   r   rS   rS   rS   rT   r   )   s   2	.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   rR   mdrS   rS   rT   r   s  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   Nr8   r   )r   r   rG   )rR   r   rS   rS   rT   r     s
    

r   )r   r   r   loggingrp   rB   r   rV   r   Zblockprocessorsr   rX   r   Zinlinepatternsr   rY   r   rC   r	   serializersr
   r   __all__	getLoggerr]   r   r   r   rS   rS   rS   rT   <module>   s&   
  L