
    iKj
I              	          d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
Z
ddlmZmZmZmZ ddlmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ d	Z ej@                  d
ejB                        Z" ej@                  d      Z# ej@                  dejB                        Z$ ej@                  d      Z%ddjM                  e      z   dz   djM                  e      z   dz   Z' ej@                  edz         Z( ej@                  d      Z) ej@                  d      Z* G d dee         Z+dede,de-de-fdZ.dedee,   de-fdZ/de,dee,   fdZ0dede-d ee,   dee-   fd!Z1d"e,d#e-de-fd$Z2d%e,de-de3fd&Z4y)'    )bisect_leftN)OptionalListTupleMatchPatternSet   )unikey
escape_url
expand_tabexpand_leading_tab)Parser
BlockState)
LINK_LABELHTML_TAGNAMEHTML_ATTRIBUTES
BLOCK_TAGSPRE_TAGSunescape_charparse_link_hrefparse_link_title)
parse_listLIST_PATTERN   z^ {1,4})flagsz(\s+|^)#+\s*$z^ ?z[ \t]*\n(|)z[ \t]*>[ \t]*(?:\n|$)z^ {0,3}>([^\n]*(?:\n|$))c                       e Zd ZeZ ej                  dej                        Zde	z   dz   Z
dez   dz   Zddddd	d
dez   dz   deee
dZdZddefdeee      deee      def fdZdee   dedefdZdee   dedefdZdee   dedefdZdee   dedee   fdZdee   dedefdZdee   dedee   fdZdee   dedee   fdZdee   dedeeee   e e   f   fdZ!dee   dedefd Z"dee   dedefd!Z#dee   dedee   fd"Z$dee   dedee   fd#Z%d(ded$eee      ddfd%Z&ded&e'e   de(fd'Z) xZ*S ))BlockParserz(^[ \t\v\f]*\n)+z^ {0,3}(</?z|<!--|<\?|<![A-Z]|<!\[CDATA\[)z^ {0,3}(?:(?:</?z.(?:[ \t]+|\n|$))|<!--|<\?|<![A-Z]|<!\[CDATA\[)z:^ {0,3}(?P<atx_1>#{1,6})(?!#+)(?P<atx_2>[ \t]*|[ \t]+.*?)$z#^ {0,3}(?P<setext_1>=|-){1,}[ \t]*$zF^(?P<fenced_1> {0,3})(?P<fenced_2>`{3,}|~{3,})[ \t]*(?P<fenced_3>.*?)$zC^(?: {4}| *\t)[^\n]+(?:\n+|$)((?:(?: {4}| *\t)[^\n]+(?:\n+|$))|\s)*z:^ {0,3}((?:-[ \t]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})$z^ {0,3}\[(?P<reflink_1>z)\]:z^ {0,3}>(?P<quote_1>.*?)$)
blank_lineatx_headingsetex_headingfenced_codeindent_codethematic_breakref_linkblock_quotelist
block_htmlraw_html)
r%   r&   r#   r$   r'   r)   r*   r(   r,   r"   Nblock_quote_rules
list_rulesmax_nested_levelc           	         t         t        |           |t        | j                        }|t        | j                        }|| _        || _        || _        | j                  D ci c]  }|t        | d|z          c}| _
        y c c}w )Nparse_)superr!   __init__r*   DEFAULT_RULESr-   r.   r/   SPECIFICATIONgetattr_methods)selfr-   r.   r/   name	__class__s        e/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/mistune/block_parser.pyr3   zBlockParser.__init__^   s     	k4)+$ $T%7%7 8d001J!2$ 0JNJ\J\]J\$wtX_==J\]]s   %Bmstatereturnc                 H    |j                  ddi       |j                         S )zParse token for blank lines.typer"   append_tokenendr8   r<   r=   s      r;   parse_blank_linezBlockParser.parse_blank_liner   s     FL12uuw    c                 N    |j                  ddi       |j                         dz   S )z:Parse token for thematic break, e.g. ``<hr>`` tag in HTML.r@   r'   r
   rA   rD   s      r;   parse_thematic_breakz BlockParser.parse_thematic_breakw   s&    F$456uuw{rF   c                 Z   |j                         }|r|S |j                  d      }t        ||j                               }||j                         k7  r|j	                  |      }t        |      }t        j                  d|      }|j                  d      }|j                  d|dd       |S )z9Parse token for code block which is indented by 4 spaces.r    

block_codeindent)r@   rawstyle)
append_paragraphgroup_trim_partial_next_line_indentrC   get_textr   _INDENT_CODE_TRIMsubstriprB   )r8   r<   r=   end_poscodes        r;   parse_indent_codezBlockParser.parse_indent_code}   s     ((*Nwwqz0quuw?aeeg>>'*D!$' $$R.zz$LQRrF   c                 >   |j                  d      }|j                  d      }|j                  d      }|d   }|r|dk(  r|j                  |      dk7  ryt        j                  d|z   d	z   t	        t        |            z   d
z   t        j                        }|j                         dz   }|j                  |j                  |      }	|	r.|j                  ||	j                          }
|	j                         }n|j                  |d }
|j                  }|rP|
rNt        j                  dt	        t        |            z   dz   t        j                        }|j                  d|
      }
d|
d|d}|r t        |      }d|j                         i|d<   |j                  |       |S )a9  Parse token for fenced code block. A fenced code block is started with
        3 or more backtick(`) or tilde(~).

        An example of a fenced code block:

        .. code-block:: markdown

            ```python
            def markdown(text):
                return mistune.html(text)
            ```
        fenced_1fenced_2fenced_3r   `Nz^ {0,3}{z,}[ \t]*(?:\n|$)r
   z^ {0,}rJ   rL   fenced)r@   rN   rO   markerinfoattrs)rQ   findrecompilestrlenMrC   searchsrcstart
cursor_maxrU   r   rV   rB   )r8   r<   r=   spacesrc   rd   c_endcursor_startm2rX   rW   _trim_patterntokens                 r;   parse_fenced_codezBlockParser.parse_fenced_code   sl    $$wwz"1IAH yy|r!zz*q.3.S[1AADWWY[Y]Y]^uuw{[[L199\BHHJ7DffhG99\]+D&&GdJJwS[1A'AC'GNM $$R.D%dXQWX &D$djjl3E'N5!rF   c                    t        |j                  d            }|j                  d      j                  t        j                        }|rt
        j                  d|      }d|d|idd}|j                  |       |j                         dz   S )	z[Parse token for ATX heading. An ATX heading is started with 1 to 6
        symbol of ``#``.atx_1atx_2rJ   headinglevelatx)r@   textre   rO   r
   )	rj   rQ   rV   string
whitespace_ATX_HEADING_TRIMrU   rB   rC   )r8   r<   r=   r|   r~   rv   s         r;   parse_atx_headingzBlockParser.parse_atx_heading   s}     AGGG$%www%%f&7&78$((T2D"DGU;KV[\5!uuw{rF   c                 |   |j                   |j                  v ry|j                         }|rD|d   dk(  r<|j                  d      dk(  rdnd}d|d<   d	|d
<   d|i|d<   |j	                         dz   S | j                  ddg      }|j                  |j                  |j                         }|r| j                  ||      S y)zParse token for setex style heading. A setex heading syntax looks like:

        .. code-block:: markdown

            H1 title
            ========
        Nr@   	paragraphsetext_1=r
      r{   setextrO   r|   re   r'   r*   )	cursorlazy_line_starts
last_tokenrQ   rC   
compile_scmatchrm   parse_method)r8   r<   r=   r   r|   scrt   s          r;   parse_setex_headingzBlockParser.parse_setex_heading   s     <<5111%%'
*V,;,3AE!*Jv"*Jw#*E"2Jw557Q;__.78XXeii.$$R//rF   c                    |j                         }|r|S |j                  d      }t        |      }|syt        |j                  |j                         d      \  }}|y|J t        ||| j                        }||j                  }	n|}	t        |j                  ||	      \  }
}|r7t        j                  |j                  |      }|r|j                         }nd}d}
|7t        j                  |j                  |      }|r|j                         }nd}d}|xs |}|sy||j                  d   vr6|J t        |      }t        |      |d}|
r|
|d<   ||j                  d   |<   |S )a  Parse link references and save the link information into ``state.env``.

        Here is an example of a link reference:

        .. code-block:: markdown

            a [link][example]

            [example]: https://example.com "Optional title"

        This method will save the link reference into ``state.env`` as::

            state.env['ref_links']['example'] = {
                'url': 'https://example.com',
                'title': "Optional title",
            }
        	reflink_1NT)block	ref_links)urllabeltitle)rP   rQ   r   r   rm   rC   _find_next_blank_line
BLANK_LINEro   r   _BLANK_TO_LINEr   envr   r   )r8   r<   r=   rW   r   keyhrefhref_pos	blank_posmax_posr   	title_posrt   m3datas                  r;   parse_ref_linkzBlockParser.parse_ref_link   su   $ ((*N$Um(AEEG4Hh<###)%4??K	&&GG+EIIxIy%%eii;BFFH	 	%%eii:B668'xeii,,### &D%d+e<D %W*.EIIk"3'rF   c                 N   t        |j                  |j                              }|J t               }| j	                  g d      }t        |j                  |            }|xj                  t        |j                  |j                              z  c_        d}|r|j                  |j                  k  rt        |j                  |j                              }|n||z  }|xj                  t        |j                  |j                              z  c_        |j                  |j                  k  r~ndd}	| j	                  g d      }
|j                  |j                  k  r5t        |j                  |j                              }|R||z  }|xj                  t        |j                  |j                              z  c_        |j                         sd}	nd}	|	rn|
j                  |j                  |j                        }|r| j                  ||      }|rn}|j                  |j                        }|j                  t        |             |t        |d      z  }|xj                  t        |      z  c_        |j                  |j                  k  r5t        |      ||fS )z6Extract text and cursor end position of a block quote.N)r"   r&   r%   F)r"   r'   r%   r*   r+   T   )_parse_block_quote_lineget_liner   setr   boolr   rj   ro   rV   rm   r   addr   r   )r8   r<   r=   r~   r   r   require_markerrW   quoteprev_blank_linebreak_scm4lines                r;   extract_block_quotezBlockParser.extract_block_quote(  s    'u~~ell'CD%(U__IJbhhtn-ENN5<<899!%,,!1!11/u||0LM=ENN5<<$@ AA ,,!1!11 $OH ,,!1!11/u||0LM$EMDLLCu||(D$EEL ;;=*.*/" ^^EIIu||<"//E:G ~~ell3 $$SY/*433D	)7 ,,!1!11> $*:::rF   c                    | j                  ||      \  }}}|j                  ||      }|j                         | j                  dz
  k\  r| j                  D cg c]	  }|dvs| }}n| j                  }| j                  ||       d|j                  d}	|r|j                  |	       |S |j                  |	       |j                  S c c}w )zParse token for block quote. Here is an example of the syntax:

        .. code-block:: markdown

            > a block quote starts
            > with right arrows
        )r   r
   )r)   r*   r)   )r@   children)
r   child_statedepthr/   r-   parsetokensprepend_tokenrB   r   )
r8   r<   r=   r~   rW   r   childrulerulesrv   s
             r;   parse_block_quotezBlockParser.parse_block_quoteh  s     +/*B*B1e*L'g'!!$9I!J;;=D11A55 '+&<&<d&<dLc@cT&<Ed**E

5% &ELLA&N5!|| es   	C"Cc                     t        | ||      S )z,Parse tokens for ordered and unordered list.)r   rD   s      r;   r   zBlockParser.parse_list  s    $5))rF   c                 &    | j                  ||      S N)parse_raw_htmlrD   s      r;   parse_block_htmlzBlockParser.parse_block_html  s    ""1e,,rF   c                    |j                  d      j                         }|dk(  rt        |d|j                               S |dk(  rt        |d|j                               S |dk(  rt        |d|j                               S |j	                  d      rt        |d	|j                               S d }d }|j	                  d
      r1|dd  j                         }|t        v rrt        || j                        S |dd  j                         }|t        v r#d
|z   d	z   }t        |||j                               S |t        v rt        || j                        S |j                         }|r|S |j                         }|j                         }|r!t        j                  |j                  ||      s#|r7t        j                  |j                  ||      rt        || j                        S y )Nr   z<!--z-->z<?z?>z	<![CDATA[z]]>z<!>z</r   r
   )rQ   rV   _parse_html_to_endrC   
startswithlowerr   _parse_html_to_newliner   r   rP   find_line_end_OPEN_TAG_ENDr   rm   _CLOSE_TAG_END)	r8   r<   r=   rc   	close_tagopen_tagend_tagrW   	start_poss	            r;   r   zBlockParser.parse_raw_html  s   !!# V%eUAEEG<< T>%eT1557;; [ %eUAEEG<< T"%eS!%%'::	T"qr
((*IJ&-eT__EEabz'')H8#/C/)%!%%'BB:%-eT__EE ((*N EEG	%%',,UYY	7K...uyy)WM)%AArF   r   c                 @   | j                  |      }|j                  |j                  k  r|j                  |j                  |j                        }|s| j                  ||      rU|s&|j                  |j                  |j                        }|sn|j                         }||j                  kD  r)|j                  |      }|j                  |       ||_        | j                  ||      }|r||_        n9|j                         }|j                  |      }|j                  |       ||_        |j                  |j                  k  r|j                  |j                  k  r<|j                  |j                  d  }|j                  |       |j                  |_        y y r   )r   r   ro   r   rm   _parse_plain_paragraphrl   rn   rS   add_paragraphr   r   )	r8   r=   r   r   r<   rW   r~   end_pos2end_pos3s	            r;   r   zBlockParser.parse  sS   __U#llU---ELL1A44UB?IIeii6ggiG%~~g.##D)&((E2H' ..0~~h/##D)'/ llU---2 <<%***99U\\^,D% ++EL +rF   r   c                    t        |j                  |j                        sy|j                  }||j                  k  rl||j                  kD  r|j	                  |j                  |      rn@|j                  |      }|j                         sn|t        |      z  }||j                  k  rl||j                  k  ry|j                  |j                  |             ||_        y)NFT)
_is_plain_paragraph_startrm   r   ro   r   r   rV   rj   r   rS   )r8   r=   r   posr   s        r;   r   z"BlockParser._parse_plain_paragraph  s    (ELLAllE$$$U\\!bhhuyy#&>>>#&D::<3t9C E$$$ %,,ENN3/0rF   r   )+__name__
__module____qualname__r   	state_clsrg   rh   rk   r   r   RAW_HTML_BLOCK_TAGS_PATTERN
BLOCK_HTMLr   r   r5   r4   DEFAULT_MAX_NESTED_LEVELr   r   ri   intr3   r   rE   rH   rY   rw   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   __classcell__)r:   s   @r;   r!   r!   &   s   I/6J		!	 	'	(+	  *T?(6 X.;gE3 #M(M 26*. 8	^#DI.^ T#Y'^ 	^(%* Z C 
eCj   5: j S "-5: -j -Xc] -^5: j S U3Z 
 xPS} 2Cc
 C: C(3- CJ>;U3Z >;
 >;uSRZ[^R_adehaiMiGj >;@5: j S 6*E#J *z *c *-%* -Z -HSM -1c
 1: 1(3- 1f,: ,htCy.A ,T ,BJ GCL T rF   r!   r=   
end_markerr   r>   c                 4   | j                   j                  ||      }|dk(  r&| j                   | j                  d  }| j                  }n<| j	                  |      }|| _        | j                         }|| j	                  |      z  }| j                  d|d       |S )Nr_   r+   r@   rN   )rm   rf   r   ro   rS   r   rB   )r=   r   r   
marker_posr~   rW   s         r;   r   r     s    
I6JRyy(""~~j)!%%'w''	T:;NrF   newlinec                    |j                  | j                  | j                        }|r"|j                         }| j	                  |      }n%| j                  | j                  d  }| j
                  }| j                  d|d       |S )Nr+   r   )rl   rm   r   rn   rS   ro   rB   )r=   r   r<   rW   r~   s        r;   r   r     sm    uyy%,,/A'')~~g&yy(""	T:;NrF   r   c                     t         j                  |       }|sy t        |j                  d      d      }t        j                  d|      S )Nr
   r   rJ   )_BLOCK_QUOTE_LINEr   r   rQ   _BLOCK_QUOTE_TRIMrU   )r   r<   r~   s      r;   r   r     s?    %Aaggaj!,D  T**rF   r   patternc                 V   | j                   j                  d      }||d   | j                  urR| j                  |j                  | j                        D cg c]  }|j	                          c}f}|| j                   d<   |d   }t        ||      }|t        |      k  r||   S y c c}w )N__blank_line_starts__r   r
   )r   getrm   finditerrn   r   rj   )r=   r   r   cacher<   	positionsindexs          r;   r   r     s    IIMM12E}a		10@0@0KL0K1QWWY0KLM-2		)*aI	3'Es9~ Ms   B&r~   rW   c                     | j                  d      dz   }|dk(  r|S | |d  }|r?|j                  d      dk(  r+t        |j                  d            dk  r|t        |      z
  S |S )NrK   r
   r   z 	rJ      )rfindrV   rj   
expandtabs)r~   rW   
line_startsuffixs       r;   rR   rR   &  sk    D!A%JQ*+F&,,u%+F4E4Ea4H0IA0MV$$NrF   rm   c                     |t        |       k\  ry| |   }|j                          xr% |j                          xr |t        j                  vS )NF)rj   isspaceisdigitr   punctuation)rm   r   rq   s      r;   r   r   1  sD    
c#hCAyy{?N199;N1F<N<N3NNrF   )5bisectr   rg   typingr   r   r   r   r   r	   r   utilr   r   r   r   corer   r   helpersr   r   r   r   r   r   r   r   list_parserr   r   r   rh   rk   rT   r   r   r   joinr   r   r   r   r!   ri   r   r   r   r   r   rR   r   r    rF   r;   <module>r     s    	 = =   %	 	 	 2 BJJz6 BJJ/0 BJJvRTT2 K(CHHZ0036(9KKcQ 

?-EEF45BJJ:; L&$ L^j c c c 
* 
ws| 
 
+# +(3- +
 
# 
 
QYZ]Q^ 
 s s O3 OS OT OrF   