o
    ^i                     @   s   d dl Z d dlmZmZmZmZ ddlmZmZm	Z	 er.ddl
mZ ddlmZ ddl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)TYPE_CHECKINGListMatchOptional   )BaseDirectiveDirectiveParserDirectivePlugin   )BlockParser)
BlockState)Markdown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                   @   s^   e Zd ZdZedee defddZedee defddZedee defdd	Z	d
S )FencedParserfenced_directivemreturnc                 C   
   |  dS )Ntypegroupr    r   U/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/mistune/directives/_fenced.py
parse_type      
zFencedParser.parse_typec                 C   r   )Ntitler   r   r   r   r   parse_title   r   zFencedParser.parse_titlec                 C   r   )Ntextr   r   r   r   r   parse_content"   r   zFencedParser.parse_contentN)
__name__
__module____qualname__namestaticmethodr   strr   r   r   r   r   r   r   r      s    r   c                       s   e Zd ZdZeZddee deddf fddZ	d	d
dede
dddee
 f
ddZd	d
dee dddee
 fddZd	d
dee dddee
 fddZd 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
    `~pluginsmarkersr   Nc                    s<   t t| | || _ddd |D }d| d | _d S )N|c                 s   s    | ]}t |V  qd S )N)reescape).0cr   r   r   	<genexpr>d   s    z+FencedDirective.__init__.<locals>.<genexpr>z^(?P<fenced_directive_mark>(?:z){3,})\{[a-zA-Z0-9_-]+\})superr   __init__r(   joindirective_pattern)selfr'   r(   _marker_pattern	__class__r   r   r0   a   s
   
zFencedDirective.__init__blockr   markerstartstater   c                 C   s   t |}|t | }d|d  d t| d }t|tj}||j|}	|	r6|j||	  }
|	 }n
|j|d  }
|j	}t
|
}|sId S | ||| |S )Nz^ {0,3}r   {z,}[ \t]*(?:\n|$))lenr%   r*   compileMsearchsrcr9   end
cursor_max_directive_rematchparse_method)r3   r7   r8   r9   r:   mlencursor_start_end_pattern_end_re_end_mr   end_posr   r   r   r   _process_directivej   s    

z"FencedDirective._process_directiver   c                 C   s   | d}| ||| |S )Nfenced_directive_mark)r   rL   r9   )r3   r7   r   r:   r8   r   r   r   parse_directive   s   
zFencedDirective.parse_directivec                 C   s\   | d}|rt|s|||S | |jkr|||S | d}| ||| |S )Nfenced_3fenced_2)r   _type_rerD   parse_fenced_codedepthmax_nested_levelrL   r9   )r3   r7   r   r:   infor8   r   r   r   rR      s   

z!FencedDirective.parse_fenced_codemdr   c                    s@   t t| | | jdkr|jdd | j d S | |d d S )Nr&   fenced_code)r/   r   __call__r(   r7   registerrR   register_block_parser)r3   rV   r5   r   r   rX      s   
zFencedDirective.__call__)r&   )rV   r   r   N)r    r!   r"   __doc__r   parserr   r	   r%   r0   intr   rL   r   rN   rR   rX   __classcell__r   r   r5   r   r   '   s    7 "	"")r*   typingr   r   r   r   _baser   r   r	   block_parserr   corer   markdownr   __all__r=   rQ   rC   r   r   r   r   r   r   <module>   s    
