
    h                         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 j"                  d
      Z e j"                  d      Z G d de      Z G d d	e      Zy)    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                   p    e Zd ZdZedee   defd       Zedee   defd       Zedee   defd       Z	y)FencedParserfenced_directivemreturnc                 $    | j                  d      S )Ntypegroupr   s    Y/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/mistune/directives/_fenced.py
parse_typezFencedParser.parse_type       wwv    c                 $    | j                  d      S )Ntitler   r   s    r   parse_titlezFencedParser.parse_title   s    wwwr   c                 $    | j                  d      S )Ntextr   r   s    r   parse_contentzFencedParser.parse_content"   r   r   N)
__name__
__module____qualname__namestaticmethodr   strr   r    r#    r   r   r   r      sw    DeCj S    uSz  c     s   r   r   c                        e Zd ZdZeZddee   deddf fdZ	ddd	ed
e
dddee
   f
dZdddee   dddee
   fdZdddee   dddee
   fdZd fdZ x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                     t         t        |   |       || _        dj	                  d |D              }d|z   dz   | _        y )N|c              3   F   K   | ]  }t        j                  |        y w)N)reescape).0cs     r   	<genexpr>z+FencedDirective.__init__.<locals>.<genexpr>d   s     "AA299Q<s   !z^(?P<fenced_directive_mark>(?:z){3,})\{[a-zA-Z0-9_-]+\})superr   __init__r-   joindirective_pattern)selfr,   r-   _marker_pattern	__class__s       r   r7   zFencedDirective.__init__a   sH    ot-g6(("A"AA-? C" " 	r   blockr   markerstartstater   c                    t        |      }|t        |      z   }d|d   z   dz   t        |      z   dz   }t        j                  |t        j                        }|j                  |j                  |      }	|	r.|j                  ||	j                          }
|	j                         }n|j                  |d  }
|j                  }t        j                  |
      }|sy | j                  |||       |S )Nz^ {0,3}r   {z,}[ \t]*(?:\n|$))lenr)   r1   compileMsearchsrcr?   end
cursor_max_directive_rematchparse_method)r:   r=   r>   r?   r@   mlencursor_start_end_pattern_end_re_end_mr"   end_posr   s                r   _process_directivez"FencedDirective._process_directivej   s    6{s6{* "S(3t94 8  	 **\2440		<899\FLLN;DjjlG99\]+D&&G%%E*r   r   c                 h    |j                  d      }| j                  |||j                         |      S )Nfenced_directive_mark)r   rS   r?   )r:   r=   r   r@   r>   s        r   parse_directivezFencedDirective.parse_directive   s.    01&&ufaggiGGr   c                 :   |j                  d      }|rt        j                  |      s|j                  ||      S |j	                         |j
                  k\  r|j                  ||      S |j                  d      }| j                  |||j                         |      S )Nfenced_3fenced_2)r   _type_rerK   parse_fenced_codedepthmax_nested_levelrS   r?   )r:   r=   r   r@   infor>   s         r   r[   z!FencedDirective.parse_fenced_code   s    wwz"8>>$/**1e44;;=E222**1e44$&&ufaggiGGr   c                     t         t        |   |       | j                  dk(  r(|j                  j                  dd | j                         y | j                  |d       y )N`~fenced_code)r6   r   __call__r-   r=   registerr[   register_block_parser)r:   mdr<   s     r   rb   zFencedDirective.__call__   sJ    ot-b1<<4HHmT43I3IJ&&r=9r   )r`   )re   r   r   N)r$   r%   r&   __doc__r   parserr   r
   r)   r7   intr   rS   r   rV   r[   rb   __classcell__)r<   s   @r   r   r   '   s    5n F
_ 5 
 
t 
 s 3 Wc hpqthu 2H] HuSz H, H[cdg[h H	H} 	Hs 	HL 	H]efi]j 	H: :r   )r1   typingr   r   r   r   _baser   r	   r
   block_parserr   corer   markdownr   __all__rD   rZ   rJ   r   r   r*   r   r   <module>rp      sl    	 7 7 B B*!# 
 2::./

#?  p:m p:r   