U
    muf                     @   sV   d dl Z ddlmZmZ dgZe dZe dZG dd deZG d	d deZ	dS )
    N   )DirectiveParserBaseDirectiveFencedDirectivez^ *\{[a-zA-Z0-9_-]+\}z\{(?P<type>[a-zA-Z0-9_-]+)\} *(?P<title>[^\n]*)(?:\n|$)(?P<options>(?:\:[a-zA-Z0-9_-]+\: *[^\n]*\n+)*)\n*(?P<text>(?:[^\n]*\n+)*)c                   @   sL   e Zd ZdZeejdddZeejdddZeejdddZ	d	S )
FencedParserZfenced_directivemc                 C   s
   |  dS )Ntypegroupr    r   >/tmp/pip-unpacked-wheel-8p1ovdot/mistune/directives/_fenced.py
parse_type   s    zFencedParser.parse_typec                 C   s
   |  dS )Ntitler
   r   r   r   r   parse_title   s    zFencedParser.parse_titlec                 C   s
   |  dS )Ntextr
   r   r   r   r   parse_content   s    zFencedParser.parse_contentN)
__name__
__module____qualname__namestaticmethodreMatchr   r   r   r   r   r   r   r      s   r   c                       sJ   e Zd ZdZeZd fdd	Zdd Zdd Zd	d
 Z	 fddZ
  ZS )r   a  A **fenced** style of directive looks like a fenced code block, it is
    inspired by markdown-it-docutils. The syntax looks like:

    .. code-block:: text

        ```{directive-type} title
        :option-key: option value
        :option-key: option value

        content text here
        ```

    To use ``FencedDirective``, developers can add it into plugin list in
    the :class:`Markdown` instance:

    .. code-block:: python

        import mistune
        from mistune.directives import FencedDirective, Admonition

        md = mistune.create_markdown(plugins=[
            # ...
            FencedDirective([Admonition()]),
        ])

    FencedDirective is using >= 3 backticks or curly-brackets for the fenced
    syntax. Developers can change it to other characters, e.g. colon:

    .. code-block:: python

            directive = FencedDirective([Admonition()], ':')

    And then the directive syntax would look like:

    .. code-block:: text

        ::::{note} Nesting directives
        You can nest directives by ensuring the start and end fence matching
        the length. For instance, in this example, the admonition is started
        with 4 colons, then it should end with 4 colons.

        You can nest another admonition with other length of colons except 4.

        :::{tip} Longer outermost fence
        It would be better that you put longer markers for the outer fence,
        and shorter markers for the inner fence. In this example, we put 4
        colons outsie, and 3 colons inside.
        :::
        ::::

    :param plugins: list of directive plugins
    :param markers: characters to determine the fence, default is backtick
                    and curly-bracket
    `~c                    s<   t t| | || _ddd |D }d| d | _d S )N|c                 s   s   | ]}t |V  qd S )N)r   escape).0cr   r   r   	<genexpr>[   s     z+FencedDirective.__init__.<locals>.<genexpr>z^(?P<fenced_directive_mark>(?:z){3,})\{[a-zA-Z0-9_-]+\})superr   __init__markersjoinZdirective_pattern)selfZpluginsr"   Z_marker_pattern	__class__r   r   r!   X   s
    
zFencedDirective.__init__c                 C   s   t |}|t | }d|d  d t| d }t|tj}||j|}	|	rl|j||	  }
|	 }n|j|d  }
|j	}t
|
}|sd S | ||| |S )Nz^ {0,3}r   {z,}[ \t]*(?:\n|$))lenstrr   compileMsearchsrcstartendZ
cursor_max_directive_rematchZparse_method)r$   blockmarkerr.   statemlenZcursor_startZ_end_patternZ_end_reZ_end_mr   Zend_posr   r   r   r   _process_directivea   s     

z"FencedDirective._process_directivec                 C   s   | d}| ||| |S )NZfenced_directive_mark)r   r6   r.   )r$   r2   r   r4   r3   r   r   r   parse_directivez   s    
zFencedDirective.parse_directivec                 C   s\   | d}|rt|s$|||S | |jkr>|||S | d}| ||| |S )NZfenced_3Zfenced_2)r   _type_rer1   parse_fenced_codedepthZmax_nested_levelr6   r.   )r$   r2   r   r4   infor3   r   r   r   r9   ~   s    

z!FencedDirective.parse_fenced_codec                    s>   t t| | | jdkr.|jdd | j n| |d d S )Nr   Zfenced_code)r    r   __call__r"   r2   registerr9   Zregister_block_parser)r$   Zmdr%   r   r   r<      s    
zFencedDirective.__call__)r   )r   r   r   __doc__r   parserr!   r6   r7   r9   r<   __classcell__r   r   r%   r   r      s   6	)
r   _baser   r   __all__r*   r8   r0   r   r   r   r   r   r   <module>   s   
