U
    muf                     @   sB   d dl mZ ddlmZ ddlmZ ddlmZ G dd dZdS )	    )Optional   )
BlockState)BlockParser)InlineParserc                   @   sv   e Zd ZdZdee ee dddZdd Ze	dd	d
Z
dd Zdeee	 dddZdddZedddZdS )Markdowna  Markdown instance to convert markdown text into HTML or other formats.
    Here is an example with the HTMLRenderer::

        from mistune import HTMLRenderer

        md = Markdown(renderer=HTMLRenderer(escape=False))
        md('hello **world**')

    :param renderer: a renderer to convert parsed tokens
    :param block: block level syntax parser
    :param inline: inline level syntax parser
    :param plugins: mistune plugins to use
    N)blockinlinec                 C   sZ   |d krt  }|d krt }|| _|| _|| _g | _g | _g | _|rV|D ]}||  qHd S N)r   r   rendererr   r	   before_parse_hooksbefore_render_hooksafter_render_hooks)selfr   r   r	   Zpluginsplugin r   4/tmp/pip-unpacked-wheel-8p1ovdot/mistune/markdown.py__init__   s    zMarkdown.__init__c                 C   s   ||  d S r
   r   )r   r   r   r   r   use*   s    zMarkdown.use)statec                 C   s(   |  |j|}| jr | ||S t|S r
   )_iter_rendertokensr   list)r   r   datar   r   r   render_state-   s    zMarkdown.render_statec                 c   sd   |D ]Z}d|kr.|  |d |}t||d< n*d|krX|d}| |d|j|d< |V  qd S )Nchildrentextz 
	)r   r   popr	   stripenv)r   r   r   tokr   r   r   r   r   r   3   s    
zMarkdown._iter_render)sr   c                 C   s   |dkr| j  }|dd}|dd}|ds<|d7 }|| | jD ]}|| | qL| j | | jD ]}|| | qn| |}| j	D ]}|| ||}q||fS )zParse and convert the given markdown string. If renderer is None,
        the returned **result** will be parsed markdown tokens.

        :param s: markdown string
        :param state: instance of BlockState
        :returns: result, state
        Nz

)
r   	state_clsreplaceendswithprocessr   parser   r   r   )r   r!   r   hookresultr   r   r   r(   ?   s     






zMarkdown.parseutf-8c              	   C   sP   |d kr| j  }||jd< t|d}| }W 5 Q R X ||}| ||S )N__file__rb)r   r$   r   openreaddecoder(   )r   filepathencodingr   fr!   r   r   r   r/   `   s    


zMarkdown.read)r!   c                 C   s   |d krd}|  |d S )Nr"   r   )r(   )r   r!   r   r   r   __call__k   s    zMarkdown.__call__)NNNN)N)r+   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   strr(   r/   r4   r   r   r   r   r      s       !
r   N)	typingr   corer   Zblock_parserr   Zinline_parserr   r   r   r   r   r   <module>   s   