
    AHj                    L   U d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dl m!Z!m"Z"m#Z# d dl$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z, d dl-m.Z. d dl/m0Z0 d dl1m2Z2 d dl3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z< d dl=m>Z> dZ?de@d<   	  G d d       ZA G d! d"      ZB G d# d$e;      ZC G d% d&eC      ZD G d' d(eD      ZE G d) d*eD      ZF G d+ d,eD      ZG G d- d.eD      ZH G d/ d0eD      ZI G d1 d2eD      ZJ G d3 d4eC      ZK G d5 d6eC      ZL G d7 d8eC      ZM G d9 d:eC      ZN G d; d<eN      ZO G d= d>eN      ZP G d? d@e;      ZQ G dA dBe9      ZR G dC dDeC      ZS G dE dFeC      ZT G dG dHeC      ZU G dI dJeC      ZV G dK dLeC      ZW G dM dNeC      ZX G dO dPe9      ZY G dQ dReC      ZZ G dS dTeZ      Z[ G dU dVeZ      Z\ G dW dXeC      Z]dYZ^ G dZ d[e9      Z_ G d\ d]e9d^_      Z` G d` dae#dbd^c      Zay)d    )annotationsN)suppress)partial)PathPurePath)CallableIterableOptional)unquote)
MarkdownIt)Token)Text)	TypeAlias)TrackedSlugsslug_for_tcss_id)ComposeResult)AwaitComplete)
HorizontalVerticalVerticalScroll)ContentSpan)	NoMatches)Mount	highlight)Layout)
GridLayout)Message)reactivevar)Style)Widget)StaticTree)Labelz!list[tuple[int, str, str | None]]r   TableOfContentsTypec                  8    e Zd ZdZddZd	dZd	dZd
dZd	dZy)MarkdownStreamzAn object to manager streaming markdown.

    This will accumulate markdown fragments if they can't be rendered fast enough.

    This object is typically created by the [Markdown.get_stream][textual.widgets.Markdown.get_stream] method.

    c                n    || _         d| _        t        j                         | _        g | _        d| _        y)zO
        Args:
            markdown_widget: Markdown widget to update.
        NF)markdown_widget_taskasyncioEvent_new_markup_pending_stopped)selfr+   s     Q/root/tools/cai/cai_env/lib/python3.12/site-packages/textual/widgets/_markdown.py__init__zMarkdownStream.__init__1   s/    
  /*.
"==?#%    c                n    | j                   )t        j                  | j                               | _         yy)zStart the updater running in the background.

        No need to call this, if the object was created by [Markdown.get_stream][textual.widgets.Markdown.get_stream].

        N)r,   r-   create_task_runr2   s    r3   startzMarkdownStream.start<   s+     :: ,,TYY[9DJ r5   c                   K   | j                   =| j                   j                          | j                    d{    d| _         d| _        yy7 w)z%Stop the stream and await its finish.NT)r,   cancelr1   r9   s    r3   stopzMarkdownStream.stopE   sC     ::!JJ**DJ DM	 "s   6AAAc                   K   | j                   rt        d      |sy| j                  j                  |       | j                  j                          t        j                  d       d{    y7 w)zAppend or enqueue a markdown fragment.

        Args:
            markdown_fragment: A string to append at the end of the document.
        z/Can't write to the stream after it has stopped.Nr   )r1   RuntimeErrorr0   appendr/   setr-   sleep)r2   markdown_fragments     r3   writezMarkdownStream.writeM   sX      ==PQQ ./mmAs   A(A2*A0+A2c                f  K   	 | j                   j                          d{   rdj                  | j                        }| j                  j	                          | j                   j	                          t        j                  | j                  j                  |             d{    | j                   j                          d{   rdj                  | j                        }|r$| j                  j                  |       d{    yy7 7 k7 K# t
        j                  $ r Y ]w xY w7 %w)z7Run a task to append markdown fragments when available.N )
r/   waitjoinr0   clearr-   shieldr+   r@   CancelledError)r2   new_markdowns     r3   r8   zMarkdownStream._run^   s     	((--///!wwt}}5##%  &&(nnT%9%9%@%@%NOOO	 ((--/// wwt}}-&&--l;;;  0 P	 0
 %% 		 <si   D1D DBD &D'!D D	D ;D1	D/
D1D D D D,)D1+D,,D1N)r+   MarkdownreturnNonerN   rO   )rC   strrN   rO   )	__name__
__module____qualname____doc__r4   r:   r=   rD   r8    r5   r3   r)   r)   (   s     	:!"<r5   r)   c                  f    e Zd ZdZd
dZedd       Zedd       Zedd       ZddZ	ddZ
ddZy	)	Navigatorz(Manages a stack of paths like a browser.c                     g | _         d| _        y )Nr   )stackindexr9   s    r3   r4   zNavigator.__init__r   s    !#

r5   c                b    | j                   st        d      S | j                   | j                     S )z]The current location.

        Returns:
            A path for the current document.
        .)rZ   r   r[   r9   s    r3   locationzNavigator.locationv   s(     zz9zz$**%%r5   c                     | j                   dk(  S )z2Is the current location at the start of the stack?r   r[   r9   s    r3   r:   zNavigator.start   s     zzQr5   c                L    | j                   t        | j                        dz
  k\  S )z0Is the current location at the end of the stack?   r[   lenrZ   r9   s    r3   endzNavigator.end   s      zzS_q000r5   c                   t         j                  t        |            \  }}|t        d      k(  r4|r2t         j                  t        | j                              \  }}| d| }| j                  j
                  t        |      z  }| j                  d| j                  dz    | _        |j                         }| j                  j                  |       t        | j                        dz
  | _        |S )zGo to a new document.

        Args:
            path: Path to new document.

        Returns:
            New location.
        r]   #Nrb   )rM   sanitize_locationrQ   r   r^   parentrZ   r[   absoluter@   rd   )r2   pathr^   anchorcurrent_file_new_paths          r3   gozNavigator.go   s     $55c$i@&tCy V&88T]]9KLOL!"^1VH-D==''$t*4ZZ 0$**q.1
$$&

(#_q(
r5   c                H    | j                   r| xj                   dz  c_         yy)zkGo back in the stack.

        Returns:
            True if the location changed, otherwise False.
        rb   TFr`   r9   s    r3   backzNavigator.back   s     ::JJ!OJr5   c                z    | j                   t        | j                        dz
  k  r| xj                   dz  c_         yy)znGo forward in the stack.

        Returns:
            True if the location changed, otherwise False.
        rb   TFrc   r9   s    r3   forwardzNavigator.forward   s0     ::DJJ!++JJ!OJr5   NrP   )rN   r   rN   bool)rk   str | PurePathrN   r   )rR   rS   rT   rU   r4   propertyr^   r:   re   rp   rr   rt   rV   r5   r3   rX   rX   o   sU    2 & &   1 1(		r5   rX   c                       e Zd ZdZh dZ	 dZ	 d	 	 	 	 	 	 	 d fdZedd       Zedd       Z	ddZ
ddZdd	Zdd
ZddZddZddZ xZS )MarkdownBlockz&The base class for a Markdown Element.>   semstrongcode_inlinezI
    MarkdownBlock {
        width: 1fr;
        height: auto;
    }
    c                   || _         	 t               | _        || _        g | _        d | _        |xs, |j                  |j                  d   |j                  d   fnd| _        t        | $  ||j                  d|j                   d| y )Nr   rb   )r   r   zlevel-)nameclasses)	_markdownr   _content_token_blocks_inline_tokenmapsource_rangesuperr4   typelevel)r2   markdowntokenr   argskwargs	__class__s         r3   r4   zMarkdownBlock.__init__   s     $,L!(",.+/-9 .
,1II,AUYYq\599Q<(v 	 	

fU[[M,B	
FL	
r5   c                ,    | j                  t              S N)query_ancestorrM   r9   s    r3   select_containerzMarkdownBlock.select_container   s    ""8,,r5   c                    | j                   y| j                   \  }}dj                  | j                  j                  j	                  d      ||       S )z4The source of this block if known, otherwise `None`.NrF   Tkeepends)r   rH   r   source
splitlines)r2   r:   re   s      r3   r   zMarkdownBlock.source   sP     $&&
swwt~~,,777FuSQRRr5   c                &    |j                   | _         y)z$Copy the context from another block.N)r   r2   blocks     r3   _copy_contextzMarkdownBlock._copy_context   s    llr5   c              #  l   K   | j                   E d {    | j                   j                          y 7 wr   )r   rI   r9   s    r3   composezMarkdownBlock.compose   s(     << 	 s   42 4c                4    || _         | j                  |       y r   )r   updater2   contents     r3   set_contentzMarkdownBlock.set_content   s    Gr5   c                   K   | j                          d {    | j                  j                  |       d {    y 7 (7 wr   )remover   mountr   s     r3   _update_from_blockz MarkdownBlock._update_from_block   s6     kkmnn""5))) 	)s   A?"AAAAc                j   K   | j                  t        j                  | j                  |             yw)zCalled on link click.N)post_messagerM   LinkClickedr   r2   hrefs     r3   action_linkzMarkdownBlock.action_link   s%     (..t~~tDE   13c                V    || _         | j                  |      }| j                  |       y)zBuild inline block content from its source token.

        Args:
            token: The token from which this block is built.
        N)r   _token_to_contentr   )r2   r   r   s      r3   build_from_tokenzMarkdownBlock.build_from_token  s*     #((/!r5   c                <   |j                   t        d      S g g g dd fd}d!fd}dd"fd}|j                   D ]  }|j                  }|dk(  r' |t        j                  dd|j
                               |d	k(  r |d
       |dk(  r	 |d       W|dk(  r" |d        ||j
                          |        ~|dk(  r	 |d       |dk(  r	 |d       |dk(  r	 |d       |dk(  r@|j                  j                  dd      }d|d} |t        j                  d|i             |dk(  r|j                  j                  dd      }|j                  j                  dd      }	d|d} |t        j                  d|i              |d       |	r |d|	 d       |j                   #|j                   D ]  }
 ||
j
                           |        |j                  d      s |         t        dj                              }|S )#zConvert an inline token to Textual Content.

        Args:
            token: A markdown token.

        Returns:
            Content instance.
        rF   r   textc                D    j                  |        t        |       z  y)zzAdd text to the tokens list, and advance the position.

            Args:
                text: Text to add.

            N)r@   rd   )r   positiontokenss    r3   add_contentz4MarkdownBlock._token_to_content.<locals>.add_content  s     MM$D	!Hr5   c                ,    j                  | f       y)zuAdd a style to the stack.

            Args:
                style: A style as Style instance or string.
            N)r@   )styler   style_stacks    r3   	add_stylez2MarkdownBlock._token_to_content.<locals>.add_style*  s     x01r5   c                 d    j                         \  } }j                  t        ||              y r   )popr@   r   )r   r:   r   spansr   s     r3   	close_tagz2MarkdownBlock._token_to_content.<locals>.close_tag4  s(    &??,LE5LLeXu56r5   z\s+ 	hardbreak
	softbreakr~   z.code_inlineem_openz.emstrong_openz.strongs_openz.s	link_openr   zlink()z@clickimagesrcaltu   🖼  (_close)r   )r   rQ   rN   rO   )r   zStyle | strrN   rO   rP   )childrenr   r   resubr   attrsgetr"   	from_metaendswithrH   )r2   r   r   r   r   child
child_typer   actionr   
grandchildr   r   r   r   r   s               @@@@r3   r   zMarkdownBlock._token_to_content  s    >>!2;57		"	2 	7 ^^ $	EJV#BFF63>?[(D![(C },.)EMM*y(% },)$x'${*{{vr2 *%//8V*<=>w&{{ub1kkooeR0 *%//8V*<=>H%!C5
+>>-&+nn 8
#J$6$678$$X.I$	L "''&/7r5   r   )r   rM   r   r   r   ztuple[int, int] | NonerN   rO   )rN   r#   )rN   
str | Noner   rz   rN   rO   rN   r   r   r   rN   rO   r   rQ   rN   rO   )r   r   rN   rO   )r   r   rN   r   )rR   rS   rT   rU   COMPONENT_CLASSESDEFAULT_CSSr4   rx   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r3   rz   rz      s    0<
K 04	

 
 -	
 

, - - S S#*F"Rr5   rz   c                      e Zd ZdZdZdZy)MarkdownHeaderz!Base class for a Markdown header.r   zP
    MarkdownHeader {
        color: $text;
        margin: 2 0 1 0;

    }
    NrR   rS   rT   rU   LEVELr   rV   r5   r3   r   r   b  s    +EKr5   r   c                      e Zd ZdZdZdZy)
MarkdownH1zAn H1 Markdown header.rb   z
    MarkdownH1 {
        content-align: center middle;
        color: $markdown-h1-color;
        background: $markdown-h1-background;
        text-style: $markdown-h1-text-style;
    }
    Nr   rV   r5   r3   r   r   p       EKr5   r   c                      e Zd ZdZdZdZy)
MarkdownH2zAn H2 Markdown header.   z
    MarkdownH2 {
        color: $markdown-h2-color;
        background: $markdown-h2-background;
        text-style: $markdown-h2-text-style;
    }
    Nr   rV   r5   r3   r   r     s     EKr5   r   c                      e Zd ZdZdZdZy)
MarkdownH3zAn H3 Markdown header.   z
    MarkdownH3 {
        color: $markdown-h3-color;
        background: $markdown-h3-background;
        text-style: $markdown-h3-text-style;
        margin: 1 0;
        width: auto;
    }
    Nr   rV   r5   r3   r   r     s     EKr5   r   c                      e Zd ZdZdZdZy)
MarkdownH4zAn H4 Markdown header.   z
    MarkdownH4 {
        color: $markdown-h4-color;
        background: $markdown-h4-background;
        text-style: $markdown-h4-text-style;
        margin: 1 0;
    }
    Nr   rV   r5   r3   r   r     r   r5   r   c                      e Zd ZdZdZdZy)
MarkdownH5zAn H5 Markdown header.   z
    MarkdownH5 {
        color: $markdown-h5-color;
        background: $markdown-h5-background;
        text-style: $markdown-h5-text-style;
        margin: 1 0;
    }
    Nr   rV   r5   r3   r   r     r   r5   r   c                      e Zd ZdZdZdZy)
MarkdownH6zAn H6 Markdown header.   z
    MarkdownH6 {
        color: $markdown-h6-color;
        background: $markdown-h6-background;
        text-style: $markdown-h6-text-style;
        margin: 1 0;
    }
    Nr   rV   r5   r3   r   r     r   r5   r   c                      e Zd ZdZdZy)MarkdownHorizontalRulezA horizontal rule.z
    MarkdownHorizontalRule {
        border-bottom: solid $secondary;
        height: 1;
        padding-top: 1;
        margin-bottom: 1;
    }
    NrR   rS   rT   rU   r   rV   r5   r3   r   r     s    Kr5   r   c                  ,     e Zd ZdZdZdZd fdZ xZS )MarkdownParagraphzA paragraph Markdown block.FzH
    Markdown > MarkdownParagraph {
         margin: 0 0 1 0;
    }
    c                   K   t        |t              r-| j                  |j                         | j	                  |       y t
        |   |       d {    y 7 wr   )
isinstancer   r   r   r   r   r   r2   r   r   s     r3   r   z$MarkdownParagraph._update_from_block  sE     e./U^^,u%',U333   AAAAr   rz   )rR   rS   rT   rU   
SCOPED_CSSr   r   r   r   s   @r3   r   r     s    %JK4 4r5   r   c                      e Zd ZdZdZy)MarkdownBlockQuotezA block quote Markdown block.aE  
    MarkdownBlockQuote {
        background: $boost;
        border-left: outer $text-primary 50%;
        margin: 1 0;
        padding: 0 1;
    }
    MarkdownBlockQuote:light {
        border-left: outer $text-secondary;
    }
    MarkdownBlockQuote > BlockQuote {
        margin-left: 2;
        margin-top: 1;
    }
    Nr   rV   r5   r3   r  r    s    'Kr5   r  c                      e Zd ZdZy)MarkdownListz

    MarkdownList {
        width: 1fr;
    }

    MarkdownList MarkdownList {
        margin: 0;
        padding-top: 0;
    }
    N)rR   rS   rT   r   rV   r5   r3   r  r    s	    
Kr5   r  c                      e Zd ZdZdZddZy)MarkdownBulletListzA Bullet list Markdown block.z
    MarkdownBulletList {
        margin: 0 0 1 0;
        padding: 0 0;
    }

    MarkdownBulletList Horizontal {
        height: auto;
        width: 1fr;
    }

    MarkdownBulletList Vertical {
        height: auto;
        width: 1fr;
    }
    c              #     K   | j                   D ]L  }t        |t              st               }|j                  |_        t        |t        |j                           N | j                   j                          y wr   )	r   r   MarkdownListItemMarkdownBulletbulletsymbolr   r   rI   )r2   r   r  s      r3   r   zMarkdownBulletList.compose  sa     \\ 	CE%!12') % 5==)ABB		C
 	s    A9AA9Nr   rR   rS   rT   rU   r   r   rV   r5   r3   r	  r	    s    'K"r5   r	  c                      e Zd ZdZdZddZy)MarkdownOrderedListzAn ordered list Markdown block.z
    MarkdownOrderedList {
        margin: 0 0 1 0;
        padding: 0 0;
    }

    MarkdownOrderedList Horizontal {
        height: auto;
        width: 1fr;
    }

    MarkdownOrderedList Vertical {
        height: auto;
        width: 1fr;
    }
    c              #  8  K   dd}| j                   r@t        | j                   d   t              r#	 t        | j                   d   j                        }t        fdt        | j                   |      D              }t        | j                   |      D ][  \  }}t        |t              st               }|  j                  |dz         |_        t        |t        |j                           ] | j                   j                          y # t
        $ r Y w xY ww)Nz. rb   r   c              3  b   K   | ]&  \  }}t        |t              rt        |         ( y wr   )r   r  rd   ).0numberr   suffixs      r3   	<genexpr>z.MarkdownOrderedList.compose.<locals>.<genexpr>D  s6      
%!12 6(6(#$
s   ,/)r   r   MarkdownOrderedListItemintr  
ValueErrormax	enumerater  r  rjustr  r   r   rI   )r2   r:   symbol_sizer  r   r  r  s         @r3   r   zMarkdownOrderedList.compose<  s     <<Jt||A8OPDLLO223  
!*4<<!?
 

 't||U; 	CMFE%!12')#)(6( 3 9 9+/ J 5==)ABB		C 	  s0   .D"D AD)A"D	DDDDNr   r  rV   r5   r3   r  r  (  s    )K"r5   r  c                      e Zd ZdZddZy)MarkdownTableCellContentszOWidget for table cells.

    A shim over a Static which responds to links.
    c                |   K   | j                  t        j                  | j                  t              |             ywz2Pass a link action on to the MarkdownTable parent.N)r   rM   r   r   r   s     r3   r   z%MarkdownTableCellContents.action_linkX  s,     (..t/B/B8/LdSTs   :<Nr   )rR   rS   rT   rU   r   rV   r5   r3   r   r   R  s    
Ur5   r   c                  `     e Zd ZdZdZddhZd fdZddZddZddZ	dd	Z
dd
ZddZ xZS )MarkdownTableContentzRenders a Markdown table.a  
    MarkdownTableContent {
        width: 1fr;
        height: auto;
        layout: grid;
        grid-columns: auto;
        grid-rows: auto;
        grid-gutter: 1 1;

        & > .cell {
            margin: 0 0;
            height: auto;
            padding: 0 1;
            text-overflow: ellipsis;
        }
        & > .header {
            height: auto;
            margin: 0 0;
            padding: 0 1;
            color: $primary;
            text-overflow: ellipsis;
            content-align: left bottom;
        }
        keyline: thin $foreground 20%;
    }
    MarkdownTableContent > .markdown-table--header {
        text-style: bold;
    }
    zmarkdown-table--headerzmarkdown-table--linesc                    |j                         | _        	 |j                         | _        	 t        |           d| _        d| _        y )NTr   )copyheadersrowsr   r4   shrinklast_row)r2   r'  r(  r   s      r3   r4   zMarkdownTableContent.__init__  s<    ||~"IIK	r5   c                    t        |t              sJ d|_        | j                  t              j
                  j                   |_        d|_        d|_	        y NT)
r   r   auto_minimumr   MarkdownTablestylesis_auto_widthexpandr)  stretch_height)r2   layouts     r3   
pre_layoutzMarkdownTableContent.pre_layout  sJ    &*---" //>EESSS $r5   c              #    K   | j                   D ]   }t        |d      j                  |       " t        | j                  d      D ]?  \  }}|D ].  }t        |d| d      j                  |j
                         0 || _        A y w)Nheaderr   rb   row cell)r'  r   with_tooltipr  r(  plainr*  )r2   r6  	row_indexr8  cells        r3   r   zMarkdownTableContent.compose  s     ll 	F+FHERR 	 (		15 	&NIs +/C	{%"8,tzz*++ &DM	&s   B	Bc                   || _         || _        g | j                   | j                  D cg c]  }|D ]  }|  c}}}t        | j                  t              |      D ]  \  }}|j                  |d        yc c}}w )zUpdate cell contents.F)r3  N)r'  r(  zipqueryr   r   )r2   r'  r(  r8  r=  cellsr   updated_cells           r3   _update_contentz$MarkdownTableContent._update_content  s    	 
\\ 
"ii8sC8Dd8d8 
 $'tzz2K'Le#T 	5E<LLeL4	5 9s   A;c           
       K   t        | j                        | j                  _        | j	                  d| j
                         j                          d {    g }t        || j
                        D ];  \  }}|D ]1  }|j                  t        |d| d      j                  |             3 = | _        | j                  |       d {    y 7 {7 w)Nz	.cell.rowr8  r9  r7  )rd   r'  r/  grid_size_columnsquery_childrenr*  r   r  r@   r$   r:  	mount_all)r2   updated_rows	new_cellsr<  r8  r=  s         r3   _update_rowsz!MarkdownTableContent._update_rows  s     (+DLL(9%!!Idmm_"=>EEGGG"$	'dmmD 	NIs   43yk)?@MMdS	
 "nnY''' 	H 	(s%   ACCA5CCCCc                L    t        | j                        | j                  _        y r   )rd   r'  r/  rE  r9   s    r3   on_mountzMarkdownTableContent.on_mount  s    (+DLL(9%r5   c                   K   t        | j                  t              r$| j                  j                  |       d{    yy7 wr"  )r   ri   r.  r   r   s     r3   r   z MarkdownTableContent.action_link  s4     dkk=1++))$/// 2/s   9AAA)r'  zlist[Content]r(  list[list[Content]])r3  r   rN   rO   r   )rH  rN  rN   rO   rP   r   )rR   rS   rT   rU   r   r   r4   r4  r   rC  rJ  rL  r   r   r   s   @r3   r$  r$  ]  s>    #K< 23JK%
&	5
(:0r5   r$  c                  D     e Zd ZdZdZd fdZddZd	dZd
 fdZ xZ	S )r.  zA Table markdown Block.z
    MarkdownTable {
        width: 1fr;
        margin-bottom: 1;
        &:light {
            background: white 30%;
        }
    }
    c                J    t        |   ||g|i | g | _        g | _        y r   )r   r4   _headers_rows)r2   r   r   r   r   r   s        r3   r4   zMarkdownTable.__init__  s*    5:4:6:')*,
r5   c              #  j   K   | j                         \  }}|| _        || _        t        ||       y wr   )_get_headers_and_rowsrQ  rR  r$  )r2   r'  r(  s      r3   r   zMarkdownTable.compose  s3     224
"7D11r   c                b   dfdg }g } |       D ]  }t        |t              r|j                  |j                         /t        |t              r|j                  g        Qt        |t
              sb|d   j                  |j                          |r|d   s|j                          ||fS )zGet list of headers, and list of rows.

        Returns:
            A tuple containing a list of headers, and a list of rows.
        c              3  t   K   | j                   D ]"  } | j                   r |       E d {    |  $ y 7 wr   )r   )r   flattens    r3   rW  z4MarkdownTable._get_headers_and_rows.<locals>.flatten  s6      ==&u~---s   '868)r   rz   rN   Iterable[MarkdownBlock])r   
MarkdownTHr@   r   
MarkdownTR
MarkdownTDr   )r2   r'  r(  r   rW  s       @r3   rT  z#MarkdownTable._get_headers_and_rows  s    	 "$$&T] 	0E%,u~~.E:.BE:.R/	0 RHHJ}r5   c                h  K   t        |t              rs	 | j                  t              }|j                  rQ| j
                  }|j                         \  }}|t        |      dz
  d }|| _        |j                  |       d{    yt        | -  |       d{    y7 # t        $ r Y %w xY w7 w)zlSpecial case to update a Markdown table.

        Args:
            block: Existing markdown block.
        rb   N)r   r.  	query_oner$  r(  rR  rT  rd   rJ  r   r   r   )r2   r   table_contentcurrent_rows_new_headersnew_rowsrH  r   s          r3   r   z MarkdownTable._update_from_block  s      e]+ $/C D !%%#'::L-2-H-H-J*L(#+C,=,A,C#DL!)DJ'44\BBBg(/// C   	0s@   B2B! AB2BB2B0B2!	B-*B2,B--B2)r   rM   r   r   rN   rO   r   )rN   z)tuple[list[Content], list[list[Content]]]r   )
rR   rS   rT   rU   r   r4   r   rT  r   r   r   s   @r3   r.  r.    s&    !K-
240 0r5   r.  c                      e Zd ZdZy)MarkdownTBodyzA table body Markdown block.NrR   rS   rT   rU   rV   r5   r3   rd  rd        &r5   rd  c                      e Zd ZdZy)MarkdownTHeadzA table head Markdown block.Nre  rV   r5   r3   rh  rh    rf  r5   rh  c                      e Zd ZdZy)r[  zA table row Markdown block.Nre  rV   r5   r3   r[  r[    s    %r5   r[  c                      e Zd ZdZy)rZ  zA table header Markdown block.Nre  rV   r5   r3   rZ  rZ    s    (r5   rZ  c                      e Zd ZdZy)r\  zA table data Markdown block.Nre  rV   r5   r3   r\  r\    rf  r5   r\  c                  6    e Zd ZdZdZ ed      Z	 ddZddZy)	r  zA bullet widget.z
    MarkdownBullet {
        width: auto;
        color: $text-primary;
        &:light {
            color: $text-secondary;
        }
    }
    u   ●c                    | j                   dfS )Nr   )r  )r2   
_selections     r3   get_selectionzMarkdownBullet.get_selection(  s    {{Cr5   c                ,    t        | j                        S r   )r   r  r9   s    r3   renderzMarkdownBullet.render+  s    t{{##r5   N)rN   ztuple[str, str] | None)rN   r   )	rR   rS   rT   rU   r   r    r  ro  rq  rV   r5   r3   r  r    s%    K hF$ $r5   r  c                  (     e Zd ZdZdZd fdZ xZS )r  zA list item Markdown block.z
    MarkdownListItem {
        layout: horizontal;
        margin-right: 1;
        height: auto;
    }

    MarkdownListItem > Vertical {
        width: 1fr;
        height: auto;
    }
    c                4    || _         t        | 	  ||       y r   )r  r   r4   )r2   r   r   r  r   s       r3   r4   zMarkdownListItem.__init__?  s    5)r5   )r   rM   r   r   r  rQ   rN   rO   )rR   rS   rT   rU   r   r4   r   r   s   @r3   r  r  /  s    %K* *r5   r  c                      e Zd Zy)r  NrR   rS   rT   rV   r5   r3   r  r  D      r5   r  c                      e Zd Zy)MarkdownUnorderedListItemNru  rV   r5   r3   rx  rx  H  rv  r5   rx  c                  f     e Zd ZdZdZd
 fdZddZedd       ZddZ	d fdZ
ddZdd	Z xZS )MarkdownFencezA fence Markdown block.a  
    MarkdownFence {
        padding: 0;
        margin: 1 0;
        overflow: scroll hidden;
        scrollbar-size-horizontal: 0;
        scrollbar-size-vertical: 0;
        width: 1fr;
        height: auto;
        color: rgb(210,210,210);
        background: black 10%;
        &:light {
            background: white 30%;
        }
        & > Label {
            padding: 1 2;
        }
    }
    c                    t         |   ||       || _        |j                  | _        | j                  | j                  | j                        | _        y r   )r   r4   codeinfolexerr   _highlighted_code)r2   r   r   r|  r   s       r3   r4   zMarkdownFence.__init__c  s?    5)	ZZ
!%		4::!Fr5   c                     yr,  rV   r9   s    r3   allow_horizontal_scrollz%MarkdownFence.allow_horizontal_scrolli  s    r5   c                    t        ||      S )N)languager   )clsr|  r  s      r3   r   zMarkdownFence.highlightl  s    11r5   c                j    t        |t              r#|j                  | _        |j                  | _        y y r   )r   rz  r~  r   r   s     r3   r   zMarkdownFence._copy_contextp  s'    e]+DJ,,DK ,r5   c                   K   t        |t              r-| j                  |j                         | j	                  |       y t
        |   |       d {    y 7 wr   )r   rz  r   r  r   r   r   r   s     r3   r   z MarkdownFence._update_from_blocku  sF     e]+U445u%',U333r  c                    || _         t        t              5  | j                  dt              j                  |       d d d        y # 1 sw Y   y xY w)Nz#code-content)r   r   r   r^  r&   r   r   s     r3   r   zMarkdownFence.set_content|  sC    i  	CNN?E299'B	C 	C 	Cs   &AAc              #  >   K   t        | j                  d       y w)Nzcode-content)id)r&   r  r9   s    r3   r   zMarkdownFence.compose  s     D**~>>s   )r   rM   r   r   r|  rQ   rN   rO   ru   )r|  rQ   r  rQ   rN   r   r   r  r   r   )rR   rS   rT   rU   r   r4   r  classmethodr   r   r   r   r   r   r   s   @r3   rz  rz  L  sA    !K(G 2 2'
4C
?r5   rz  u    ⅠⅡⅢⅣⅤⅥc                      e Zd ZU dZg dZ	 i dedededede	de
d	ed
edededededededededeeeeedZded<   	 	 d/dddddd	 	 	 	 	 	 	 	 	 	 	 d0 fdZed1d       Z G d de      Z G d de      Z  G d  d!e      Z!ed2d"       Z"d3d#Z#d4d$Z$e%d5d%       Z&d6d&Z'e(d7d'       Z)d8d(Z*d9d)Z+d:d*Z,d;d+Z-d<d,Z.d=d-Z/d=d.Z0 xZ1S )>rM   a  
    Markdown {
        height: auto;
        padding: 0 2 0 2;
        layout: vertical;
        color: $foreground;
        overflow-y: hidden;

        MarkdownBlock {
            &:dark > .code_inline {
                background: $warning 10%;
                color: $text-warning 95%;
            }
            &:light > .code_inline {
                background: $error 5%;
                color: $text-error 95%;
            }
            & > .em {
                text-style: italic;
            }
            & > .strong {
                text-style: bold;
            }
            & > .s {
                text-style: strike;
            }
        }
    }
    )u   • u   ▪ u   ‣ u   ⭑ u   ◦ h1h2h3h4h5h6hrparagraph_openblockquote_openbullet_list_openordered_list_openlist_item_ordered_openlist_item_unordered_open
table_open
tbody_open
thead_opentr_open)th_opentd_openfence
code_blockzdict[str, type[MarkdownBlock]]BLOCKSNT)r   r  r   parser_factory
open_linksc                   t         |   |||       || _        d| _        || _        d| _        || _        d| _        d| _        y)a5  A Markdown widget.

        Args:
            markdown: String containing Markdown or None to leave blank for now.
            name: The name of the widget.
            id: The ID of the widget in the DOM.
            classes: The CSS classes of the widget.
            parser_factory: A factory function to return a configured MarkdownIt instance. If `None`, a "gfm-like" parser is used.
            open_links: Open links automatically. If you set this to `False`, you can handle the [`LinkClicked`][textual.widgets.markdown.Markdown.LinkClicked] events.
        r   r  r   rF   Nr   )	r   r4   _initial_markdownr   _parser_factory_table_of_contents_open_links_last_parsed_line_theme)r2   r   r   r  r   r  r  r   s          r3   r4   zMarkdown.__init__  sO    ( 	dr7;-5->B%!"r5   c                    | j                   Z| j                  D cg c]?  }t        |t              r-|j                  |j
                  j                  |j                  fA c}| _         | j                   S c c}w )z!The document's table of contents.)r  r   r   r   r   r   r;  r  )r2   r6  s     r3   table_of_contentszMarkdown.table_of_contents  sg     ""* #mm'fn5 v44fii@'D#
 &&&'s   AA3c                  B     e Zd ZdZ	 	 	 	 	 	 d fdZedd       Z xZS )Markdown.TableOfContentsUpdatedz"The table of contents was updated.c                @    t         |           || _        	 || _        y r   )r   r4   r   r  )r2   r   r  r   s      r3   r4   z(Markdown.TableOfContentsUpdated.__init__  s%     G&.DMN:KD"$r5   c                    | j                   S )a  The `Markdown` widget associated with the table of contents.

            This is an alias for [`TableOfContentsUpdated.markdown`][textual.widgets.Markdown.TableOfContentsSelected.markdown]
            and is used by the [`on`][textual.on] decorator.
            r   r9   s    r3   controlz'Markdown.TableOfContentsUpdated.control       == r5   )r   rM   r  r'   rN   rO   rN   rM   rR   rS   rT   rU   r4   rx   r  r   r   s   @r3   TableOfContentsUpdatedr    s7    0	%$	%9L	%	% 
	! 
	!r5   r  c                  6     e Zd ZdZd fdZedd       Z xZS ) Markdown.TableOfContentsSelectedz An item in the TOC was selected.c                @    t         |           || _        	 || _        y r   )r   r4   r   block_id)r2   r   r  r   s      r3   r4   z)Markdown.TableOfContentsSelected.__init__  s"    G&.DMC!)DM4r5   c                    | j                   S )zThe `Markdown` widget where the selected item is.

            This is an alias for [`TableOfContentsSelected.markdown`][textual.widgets.Markdown.TableOfContentsSelected.markdown]
            and is used by the [`on`][textual.on] decorator.
            r  r9   s    r3   r  z(Markdown.TableOfContentsSelected.control
  r  r5   )r   rM   r  rQ   rN   rO   r  r  r   s   @r3   TableOfContentsSelectedr     s    .	5 
	! 
	!r5   r  c                  6     e Zd ZdZd fdZedd       Z xZS )Markdown.LinkClickedz#A link in the document was clicked.c                R    t         |           || _        	 t        |      | _        y r   )r   r4   r   r   r   )r2   r   r   r   s      r3   r4   zMarkdown.LinkClicked.__init__  s&    G&.DMD$T]DI-r5   c                    | j                   S )zThe `Markdown` widget containing the link clicked.

            This is an alias for [`LinkClicked.markdown`][textual.widgets.Markdown.LinkClicked.markdown]
            and is used by the [`on`][textual.on] decorator.
            r  r9   s    r3   r  zMarkdown.LinkClicked.control  r  r5   )r   rM   r   rQ   rN   rO   r  r  r   s   @r3   r   r    s    1	. 
	! 
	!r5   r   c                "    | j                   xs dS )zThe markdown source.rF   )r   r9   s    r3   r   zMarkdown.source&  s     ~~##r5   c                     | j                   |   S )zGet the block widget class.

        Args:
            block_name: Name of the block.

        Returns:
            A MarkdownBlock class
        )r  )r2   
block_names     r3   get_block_classzMarkdown.get_block_class+  s     {{:&&r5   c                   K   | j                   }d | _         | j                  |xs d       d {    |?| j                  t        j	                  | | j
                        j                  |              y y 7 FwNrF   )r  r   r   rM   r  r  
set_sender)r2   rn   initial_markdowns      r3   	_on_mountzMarkdown._on_mount6  ss     11!%kk*0b111#//$11*T" $ 	2s   ,A7A5AA7c                <    t        |      }|j                          |S )aN  Get a [MarkdownStream][textual.widgets.markdown.MarkdownStream] instance to stream Markdown in the background.

        If you append to the Markdown document many times a second, it is possible the widget won't
        be able to update as fast as you write (occurs around 20 appends per second). It will still
        work, but the user will have to wait for the UI to catch up after the document has be retrieved.

        Using a [MarkdownStream][textual.widgets.markdown.MarkdownStream] will combine several updates in to one
        as necessary to keep up with the incoming data.

        example:
        ```python
        # self.get_chunk is a hypothetical method that retrieves a
        # markdown fragment from the network
        @work
        async def stream_markdown(self) -> None:
            markdown_widget = self.query_one(Markdown)
            container = self.query_one(VerticalScroll)
            container.anchor()

            stream = Markdown.get_stream(markdown_widget)
            try:
                while (chunk:= await self.get_chunk()) is not None:
                    await stream.write(chunk)
            finally:
                await stream.stop()
        ```


        Args:
            markdown: A [Markdown][textual.widgets.Markdown] widget instance.

        Returns:
            The Markdown stream object.
        )r)   r:   )r  r   updaters      r3   
get_streamzMarkdown.get_streamB  s    H !*r5   c                h    | j                   r&| j                  j                  |j                         y y r   )r  appopen_urlr   )r2   events     r3   on_markdown_link_clickedz!Markdown.on_markdown_link_clickedj  s&    HHejj) r5   c                F    | j                  d      \  } }}t        |       |fS )a  Given a location, break out the path and any anchor.

        Args:
            location: The location to sanitize.

        Returns:
            A tuple of the path to the location cleaned of any anchor, plus
            the anchor (or an empty string if none was found).
        rg   )	partitionr   )r^   rn   rl   s      r3   rh   zMarkdown.sanitize_locationn  s*     '005!VH~v%%r5   c                   | j                   rt        | j                  t              syt	               }| j                   D ]@  \  }}}|j                  |      |k(  s| j                  d|       j                  d        y y)a  Try and find the given anchor in the current document.

        Args:
            anchor: The anchor to try and find.

        Note:
            The anchor is found by looking at all of the headings in the
            document and finding the first one whose slug matches the
            anchor.

            Note that the slugging method used is similar to that found on
            GitHub.

        Returns:
            True when the anchor was found in the current document, False otherwise.
        Frg   Ttop)r  r   ri   r#   r   slugr^  scroll_visible)r2   rl   uniquern   title	header_ids         r3   goto_anchorzMarkdown.goto_anchor|  sy    " &&jf.M#'#:#: 	Aui{{5!V+9+/>>4>H	 r5   c                *  K   | j                  t        |            \  }}t        j                         j	                  dt        |j                  d             d{   }| j                  |       d{    |r| j                  |       yy7 27 w)al  Load a new Markdown document.

        Args:
            path: Path to the document.

        Raises:
            OSError: If there was some form of error loading the document.

        Note:
            The exceptions that can be raised by this method are all of
            those that can be raised by calling [`Path.read_text`][pathlib.Path.read_text].
        Nzutf-8)encoding)	rh   rQ   r-   get_running_looprun_in_executorr   	read_textr   r  )r2   rk   rl   datas       r3   loadzMarkdown.load  s      --c$i8f--/??'$..7;
 
 kk$V$ 	
 	 s$   ABBB5B6BBc                     y)zProcess an unhandled token.

        Args:
            token: The MarkdownIt token to handle.

        Returns:
            Either a widget to be added to the output, or `None`.
        NrV   )r2   r   s     r3   unhandled_tokenzMarkdown.unhandled_token  s     r5   c              #    K   g }|j                   }| j                  }|D ]'  }|j                  }|dk(  r  |  ||j                        | |             5|dk(  r  |d      | |       L|dk(  r |  |d      | |             g|dk(  r |  |d      | |             |dk(  r |  |d      | |             |dk(  r |  |d      | |             |dk(  rz|j                  r! |  |d      | ||j                               t        d	 |D              } |  |d
      | || j                  |t        | j                        z                  7|dk(  r |  |d      | |             S|dk(  r |  |d      | |             o|dk(  r |  |d      | |             |dk(  r |  |d      | |             |dk(  r |  |d      | |             |dk(  r |  |d      | |             |j                  d      rz|j                         }|j                  dk(  r3dt        |j                  j                         dt        |       |_        |r |d   j                  j                  |       d| j|dk(  r|d   j                  |       |dv rd ||      }	t!        |	t"              sJ  |	| ||j$                  j'                               }
|r |d   j                  j                  |
       |
 | j)                  |      }||r |d   j                  j                  |       $| * yw)zCreate a stream of MarkdownBlock widgets from markdown.

        Args:
            tokens: List of tokens.

        Yields:
            Widgets for mounting.
        heading_openr  r  r  r  r  list_item_openr  c              3  @   K   | ]  }t        |t              rd   yw)rb   N)r   rx  r  r   s     r3   r  z+Markdown._parse_markdown.<locals>.<genexpr>  s#      %!%e-FG %s   r  r  r  r  r  r  r  r   heading_closezheading--rX  inline)r  r  N)r@   r  r   tagr}  sumBULLETSrd   r   r   r   r   r;  r  r   r   
issubclassrz  r   rstripr  )r2   r   rZ   stack_appendr  r   
token_type
item_countr   fence_classr  externals               r3   _parse_markdownzMarkdown._parse_markdown  sb     &(||.. H	'EJ^+7_UYY7eDEt#+od+D%88//>_-=>tUKL00?_->?eLM11@_-?@uMN22A_-@A$NO//:: A(@A % "% %%*% "J
 !C(BC ! LLc$,,6G)GH |+:_\:4GH|+:_\:4GH|+:_\:4GHy(7_Y7eDEy(7_Y7eDEy(7_Y7eDE$$X.		::0"#3ENN4H4H#I"J!BuI;W H "I%%,,U3Kx'b	**5166-j9!+}===#D%1E1E1GH"I%%,,U3K//6'b	))00:&QH	's   LM!*Mc                    | j                   t        d      n| j                         }|j                  |      }t        | j	                  |            S )zBuild blocks from markdown source.

        Args:
            markdown: A Markdown document, or partial document.

        Returns:
            A list of MarkdownBlock instances.
        gfm-like)r  r   parselistr  )r2   r   parserr   s       r3   _build_from_sourcezMarkdown._build_from_source  sQ     ##+ z"%%' 	
 h'D((011r5   c                      j                   j                   _         j                  t	        d      n j                          j                  d       _        d _        d fd}t         |             S )zUpdate the document with new Markdown.

        Args:
            markdown: A string containing Markdown.

        Returns:
            An optionally awaitable object. Await this to ensure that all children have been mounted.
        Nr  rz   c                   K   d} g }
j                   4 d{    t        j                         j                  d	j                         d{   }dd
fd}
j                  |      D ]B  }|j                  |       t        |      | k(  s# ||       d{    |j                          D |r ||       d{    sj                          d{    ddd      d{    j                         }t        |      |r|d   rdndz
  
_        
j                  t        j                  

j                        j!                  
             y7 C7 7 7 7 7 {# 1 d{  7  sw Y   xY ww)	zUpdate in batches.   NFc                  K   rj                  |        d{    yj                  j                         5  j                          d{    j                  |        d{    ddd       dy7 \7 *7 # 1 sw Y   xY ww)zMount a single match of blocks.

                    Args:
                        batch: A list of blocks to mount.
                    NT)rG  r  batch_updater   )batchmarkdown_blockremovedr2   s    r3   mount_batchz:Markdown.update.<locals>.await_update.<locals>.mount_batchB  s}      "nnU333!XX224 8"0"7"7"999"&.."7778 #' 4 :78 8sJ   B	A7B	A=A9A='A;(A=,B	9A=;A==BB	rX  rb   r   )r  list[MarkdownBlock]rN   rO   )lockr-   r  r  r  r  r@   rd   rI   r   r   r  r   rM   r  r  r  )
BATCH_SIZEr  r   r  r   linesr  r   r  r  r2   s         @r3   await_updatez%Markdown.update.<locals>.await_update4  sf    J)+E yy 2 2&779II&,,  
 !&' "11&9 &ELL'5zZ/)%000	&
 %e,,,(//111?2 2B '')E%(Z%)1QR%SD"//$00*T"G22 1 -1?2 2 2 2s   E?EE?3E*EAE*E*E"#E*E$E*E&E*!E?,E(-A0E?E*"E*$E*&E*(E?*E<0E31E<8E?rP   )	r  themer  r  r   r@  r   r  r   )r2   r   r  r  r  s   `` @@r3   r   zMarkdown.update   sv     hhnn ##+ z"%%' 	 O4!"&-	 -	^ \^,,r5   c                     j                   t        d      n j                          j                  |z    _        dj	                   j                  j                  d       j                  d       d fd}t         |             S )zAppend to markdown.

        Args:
            markdown: A fragment of markdown to be appended.

        Returns:
            An optionally awaitable object. Await this to ensure that the markdown has been append by the next line.
        Nr  rF   Tr   c                 ,  K   j                   4 d{    j                        } j                  D cg c]  }t        |t              s| }}j
                  }t        |       D ]B  }|j                  |j                  dk(  s xj
                  |j                  d   z  c_         n t        j                  |             }t        d |D              }|D ]   }|j                  \  }}	||z   |	|z   f|_        " j                  j                         5  |r=|r;|d   }
|d   j                  |
_        	 |
j                  |d          d{    |dd }|rj#                  |       d{    ddd       |rEd_        j'                  t(        j+                  j,                        j/                               ddd      d{    y7 c c}w 7 # t         $ r Y w xY w7 }# 1 sw Y   |xY w7 ,# 1 d{  7  sw Y   yxY ww)zAppend new markdown widgets.Nr   c              3  <   K   | ]  }t        |t                y wr   )r   r   r  s     r3   r  z8Markdown.append.<locals>.await_append.<locals>.<genexpr>  s      ":?Jun5"s   rX  rb   )r
  r  r   r   rz   r  reversedr   r   r  r  anyr   r  r  r   
IndexErrorrG  r  r   rM   r  r  r  )r   r   existing_blocks
start_liner   
new_blocksany_headersr   r:   re   
last_blockr  r2   updated_sources              r3   await_appendz%Markdown.append.<locals>.await_appendy  s    yy * *n5'+}}##
5-8XE# # "33
%f- Eyy,1A..%))A,>.
 "$"6"6v">?
! "CM"  ( E!&!3!3JE3
*j(*E& XX**, 9&:%4R%8
2<Q-2L2L
/8","?"?
1"NNN *4ABJ!"nnZ8889 .2D+%% 77 $"8"8$*T*M* * *#2 O) ! ! 99 9-* * * *s   HGH G?GG(G?;G?BG?G1:G GG G11G/2G16AG?HG=HG?G  	G,)G1+G,,G11G:	6G?=H?HHHHrP   )r  r   r   r   rH   r   r  r   )r2   r   r  r  r  s   `  @@r3   r@   zMarkdown.appende  s     ##+ z"%%' 	 x/NN%%t%4T5K5K5MN
,	\ \^,,r5   r   )r   r   r   r   r  r   r   r   r  Callable[[], MarkdownIt] | Noner  rv   )rN   r'   )rN   rQ   )r  rQ   rN   ztype[MarkdownBlock]rn   r   rN   rO   )r   rM   rN   r)   )r  r   rN   rO   )r^   rQ   rN   ztuple[Path, str])rl   rQ   rN   rv   )rk   r   rN   rO   )r   r   rN   zMarkdownBlock | None)r   zIterable[Token]rN   rY  )r   rQ   rN   r	  )r   rQ   rN   r   )2rR   rS   rT   r   r  r   r   r   r   r   r   r   r   r  r	  r  r  rx  r.  rd  rh  r[  rZ  r\  rz  r  __annotations__r4   rx   r  r   r  r  r   r   r  r  r  r  r  staticmethodrh   r  r  r  r  r  r   r@   r   r   s   @r3   rM   rM     s   K< 7G3.j.j. 	j. 	j	.
 	j. 	j. 	$. 	+. 	-. 	.. 	0. 	!"9. 	#$=. 	m. 	m.  	m!." 	:#.$ #+.F* . 7  $  ":> 	
   8 : ' '! !*!' !&!g !& $ $	'
 % %N* & &4%*	W'r2"C-JB-r5   rM   c                       e Zd ZdZdZ eee      dd      Z	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d fdZ	ddZ
ddZdd	Zdd
Z xZS )MarkdownTableOfContentsz5Displays a table of contents for a markdown document.aD  
    MarkdownTableOfContents {
        width: auto;
        height: 1fr;
        background: $panel;
        &:focus-within {
            background-tint: $foreground 5%;
        }
    }
    MarkdownTableOfContents > Tree {
        padding: 1;
        width: auto;
        height: 1fr;
        background: $panel;
    }
    NF)initc                <    || _         	 t        | 	  ||||       y)aX  Initialize a table of contents.

        Args:
            markdown: The Markdown document associated with this table of contents.
            name: The name of the widget.
            id: The ID of the widget in the DOM.
            classes: The CSS classes for the widget.
            disabled: Whether the widget is disabled or not.
        )r   r  r   disabledN)r   r   r4   )r2   r   r   r  r   r$  r   s         r3   r4   z MarkdownTableOfContents.__init__  s%    " #+Kdr7XNr5   c              #  b   K   t        d      }d|_        d|_        d|_        d|_        | y w)NTOCFTr   )r%   	show_rootshow_guidesguide_depthauto_expand)r2   trees     r3   r   zMarkdownTableOfContents.compose  s3     %[ 
s   -/c                &    | j                  |       y)z-Triggered when the table of contents changes.N)rebuild_table_of_contents)r2   r  s     r3   watch_table_of_contentsz/MarkdownTableOfContents.watch_table_of_contents  s    &&'89r5   c                   | j                  t              }|j                          |j                  }|D ]  \  }}}|}t	        |dz
        D ]O  }|j
                  r'|j
                  d   }|j                          d|_        6|j                  t        |   d      }Q t        j                  t        |    ddf|      }	|j                  |	d|i        y)	zRebuilds the tree representation of the table of contents data.

        Args:
            table_of_contents: Table of contents.
        rb   rX  T)r1  r   dimr  N)r^  r%   rI   rootrange	_childrenr1  allow_expandaddNUMERALSr   assembleadd_leaf)
r2   r  r+  r1  r   r   r  nodern   
node_labels
             r3   r-  z1MarkdownTableOfContents.rebuild_table_of_contents  s     ~~d#

yy%6 
	>!E4D519% B>>>>"-DKKM(,D%88HUOD8ADB 8E?*;1(=u'EtLJMM*z8&<=
	>r5   c                   K   |j                   j                  }|:| j                  t        j	                  | j
                  |d                d {    |j                          y 7 w)Nr  )r9  r  _post_messagerM   r  r   r=   )r2   message	node_datas      r3   _on_tree_node_selectedz.MarkdownTableOfContents._on_tree_node_selected  s\     LL%%	 $$00	*@UV   	s   AA(A&A()NNNF)r   rM   r   r   r  r   r   r   r$  rv   rN   rO   r   )r  r'   rN   rO   )r=  zTree.NodeSelectedrN   rO   )rR   rS   rT   rU   r   r    r
   r'   r  r4   r   r.  r-  r?  r   r   s   @r3   r!  r!    s    ?K" @*=!>?5QC
  "OO O 	O
 O O 
O*:>*r5   r!  T)can_focus_childrenc                  *    e Zd ZU dZdZdZ ed      Z	  ed      Z e	e
      Zded<    G d d	e      Z	 ddd
d
d
d
dd	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZedd       Zedd       ZddZddZddZddZddZd dZd!dZ	 	 	 	 d"dZ	 	 	 	 d#dZ xZS )$MarkdownViewerzA Markdown viewer widget.Fa=  
    MarkdownViewer {
        height: 1fr;
        scrollbar-gutter: stable;
        background: $surface;
        & > MarkdownTableOfContents {
            display: none;
            dock:left;
        }
    }

    MarkdownViewer.-show-table-of-contents > MarkdownTableOfContents {
        display: block;
    }
    TrF   zvar[Navigator]	navigatorc                      e Zd ZdZy)MarkdownViewer.NavigatorUpdatedz.Navigator has been changed (clicked link etc).Nre  rV   r5   r3   NavigatorUpdatedrE    s    <r5   rF  N)show_table_of_contentsr   r  r   r  r  c               b    t         |   |||       || _        || _        || _        || _        y)a  Create a Markdown Viewer object.

        Args:
            markdown: String containing Markdown, or None to leave blank.
            show_table_of_contents: Show a table of contents in a sidebar.
            name: The name of the widget.
            id: The ID of the widget in the DOM.
            classes: The CSS classes of the widget.
            parser_factory: A factory function to return a configured MarkdownIt instance. If `None`, a "gfm-like" parser is used.
            open_links: Open links automatically. If you set this to `False`, you can handle the [`LinkClicked`][textual.widgets.markdown.Markdown.LinkClicked] events.
        r  N)r   r4   rG  r   r  r  )	r2   r   rG  r   r  r   r  r  r   s	           r3   r4   zMarkdownViewer.__init__  s8    , 	dr7;&<#!-%r5   c                ,    | j                  t              S )z;The [`Markdown`][textual.widgets.Markdown] document widget.)r^  rM   r9   s    r3   documentzMarkdownViewer.document:  s     ~~h''r5   c                ,    | j                  t              S )zQThe [table of contents][textual.widgets.markdown.MarkdownTableOfContents] widget.)r^  r!  r9   s    r3   r  z MarkdownViewer.table_of_contents?  s     ~~566r5   c                r   K   | j                   j                  | j                  xs d       d {    y 7 wr  )rJ  r   r   )r2   rn   s     r3   r  zMarkdownViewer._on_mountD  s'     mm""4>>#7R888s   -757c                l  K   | j                   j                  t        |            \  }}|t        d      k(  r|r| j                   j	                  |       y| j                   j                  | j                  j                  |             d{    | j                  | j                                y7 $w)z Navigate to a new document path.r]   N)
rJ  rh   rQ   r   r  r  rC  rp   r   rF  )r2   r^   rk   rl   s       r3   rp   zMarkdownViewer.goG  s     }}66s8}Ef49MM%%f- --$$T^^%6%6x%@AAAd3356 Bs   BB4B2%B4c                   K   | j                   j                         rW| j                  j                  | j                   j                         d{    | j                  | j                                yy7 %w)z!Go back one level in the history.N)rC  rr   rJ  r  r^   r   rF  r9   s    r3   rr   zMarkdownViewer.backR  sW     >> --$$T^^%<%<===d3356 !=   AA7A5&A7c                   K   | j                   j                         rW| j                  j                  | j                   j                         d{    | j                  | j                                yy7 %w)z$Go forward one level in the history.N)rC  rt   rJ  r  r^   r   rF  r9   s    r3   rt   zMarkdownViewer.forwardX  sW     >>!!#--$$T^^%<%<===d3356 $=rO  c                v   K   |j                          | j                  |j                         d {    y 7 wr   )r=   rp   r   r2   r=  s     r3   _on_markdown_link_clickedz(MarkdownViewer._on_markdown_link_clicked^  s%     gggll###s   /979c                (    | j                  |d       y )Nz-show-table-of-contents)	set_class)r2   rG  s     r3   watch_show_table_of_contentsz+MarkdownViewer.watch_show_table_of_contentsb  s    -/HIr5   c              #  ~   K   t        | j                  | j                        }d|_        | t	        |       y w)N)r  r  T)rM   r  r  	can_focusr!  )r2   r   s     r3   r   zMarkdownViewer.composee  s;     //D<L<L
 "%h//s   ;=c                l    |j                   | j                  t              _         |j                          y r   )r  r^  r!  r=   rR  s     r3   &_on_markdown_table_of_contents_updatedz5MarkdownViewer._on_markdown_table_of_contents_updatedm  s*     %% 	./A 	r5   c                    d|j                    }| j                  |t              }| j                  |d       |j	                          y )Nrg   Tr  )r  r^  rz   scroll_to_widgetr=   )r2   r=  block_selectorr   s       r3   '_on_markdown_table_of_contents_selectedz6MarkdownViewer._on_markdown_table_of_contents_selectedu  sC     W--./~}=e.r5   r   )r   r   rG  rv   r   r   r  r   r   r   r  r  r  rv   r  )rN   r!  r  )r^   rw   rN   rO   rP   )r=  r  rN   rO   )rG  rv   rN   rO   r   )r=  r  rN   rO   )r=  r  rN   rO   )rR   rS   rT   rU   r  r   r    rG  	top_blockr!   rX   rC  r  r   rF  r4   rx   rJ  r  r  rp   rr   rt   rS  rV  r   rZ  r^  r   r   s   @r3   rB  rB     s!   #JK  &d^%I #II~.=7 =
  $& (,":>&& !%	&
 & & & 8& &8 ( ( 7 79	777$J06	7	r5   rB  F)rX  r@  )b
__future__r   r-   r   
contextlibr   	functoolsr   pathlibr   r   typingr   r	   r
   urllib.parser   markdown_itr   markdown_it.tokenr   	rich.textr   typing_extensionsr   textual._slugr   r   textual.appr   textual.await_completer   textual.containersr   r   r   textual.contentr   r   textual.css.queryr   textual.eventsr   textual.highlightr   textual.layoutr   textual.layouts.gridr   textual.messager   textual.reactiver    r!   textual.styler"   textual.widgetr#   textual.widgetsr$   r%   textual.widgets._labelr&   r'   r  r)   rX   rz   r   r   r   r   r   r   r   r   r   r  r  r	  r  r   r$  r.  rd  rh  r[  rZ  r\  r  r  r  rx  rz  r6  rM   r!  rB  rV   r5   r3   <module>rz     s   "  	   " / /   " #  ' 8 % 0 C C ) '   ' ! + # *  ! ( (!D Y DD< D<ND DNiF iX]        
] 
4 4$ (=  :', 'TU U\06 \0~E0M E0P'M ''M '& &) )' '$V $.*} **	. 		 0 	6?M 6?r !_-v _-DSf Sl{^u {r5   