o
    MEgNP                     @   s^  d dl mZmZmZmZ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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%m&Z& G dd dZ'G dd de'Z(G dd de'Z)G dd de)Z*G dd de)Z+G dd de)Z,G dd de)Z-G dd de'Z.G d d! d!e'Z/G d"d# d#e)Z0G d$d% d%e)Z1G d&d' d'Z2G d(d) d)eZ3e4d*krd d+l5Z5d d+l6Z6e5j7d,d-Z8e8j9d.d/d0d1 e8j9d2d3d4d5d+d6d7 e8j9d8d9d:d;d<d= e8j9d>d?d@d+d@d= e8j9dAdBdCd5dDdE e8j9dFdGe:dHd+dIdJ e8j9dKdLdMd5dNdE e8j9dOdPdQd5dRdE e8; Z<d dSl=mZ e<j>dTkrIe6j?@ ZAneBe<j>dUdVdWZCeC@ ZAW d+   n	1 saw   Y  e3eAe<jDrndXndYe<jEe<jFe<jGdZZHe<jIrd d+lJZJd d+lKZKeeKL e<jMe<jNd[ZeOeH eJPejQR  d+S ee<jMe<jNd\ZeOeH d+S d+S )]    )AnyClassVarDictListOptionalTypeUnion)Parser   )box)
loop_first)Stack)ConsoleConsoleOptionsJustifyMethodRenderResult)Renderables)JupyterMixin)Panel)Rule)Segment)Style
StyleStack)Syntax)TextTextTypec                   @   s   e Zd ZU dZee ed< edddedd fddZ	dddZ
d	d
deddfddZdddZd	d
dd defddZ						dddZdS )MarkdownElementTnew_linemarkdownMarkdownnodereturnc                 C   s   |  S zFactory to create markdown element,

        Args:
            markdown (Markdown): THe parent Markdown object.
            node (Any): A node from Pygments.

        Returns:
            MarkdownElement: A new markdown element
         clsr   r    r#   r#   g/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/markdown.pycreate   s   zMarkdownElement.createcontextMarkdownContextNc                 C      dS )zuCalled when the node is entered.

        Args:
            context (MarkdownContext): The markdown context.
        Nr#   selfr(   r#   r#   r&   on_enter$       zMarkdownElement.on_entertextc                 C   r*   )zpCalled when text is parsed.

        Args:
            context (MarkdownContext): The markdown context.
        Nr#   r,   r(   r/   r#   r#   r&   on_text+   r.   zMarkdownElement.on_textc                 C   r*   )zwCalled when the parser leaves the element.

        Args:
            context (MarkdownContext): [description]
        Nr#   r+   r#   r#   r&   on_leave2   r.   zMarkdownElement.on_leavechildc                 C   r*   )a|  Called when a child element is closed.

        This method allows a parent element to take over rendering of its children.

        Args:
            context (MarkdownContext): The markdown context.
            child (MarkdownElement): The child markdown element.

        Returns:
            bool: Return True to render the element, or False to not render the element.
        Tr#   r,   r(   r3   r#   r#   r&   on_child_close9   s   zMarkdownElement.on_child_closeconsoler   optionsr   r   c                 C   r*   )Nr#   r#   r,   r6   r7   r#   r#   r&   __rich_console__I   s   z MarkdownElement.__rich_console__r(   r)   r!   N)r6   r   r7   r   r!   r   )__name__
__module____qualname__r   r   bool__annotations__classmethodr   r'   r-   r   r1   r2   r5   r9   r#   r#   r#   r&   r      s*   
 


r   c                   @   s   e Zd ZdZdS )UnknownElementzAn unknown element.

    Hopefully there will be no unknown elements, and we will have a MarkdownElement for
    everything in the document.

    N)r;   r<   r=   __doc__r#   r#   r#   r&   rA   O   s    rA   c                   @   s>   e Zd ZdZdZdddZddd	eddfd
dZdddZdS )TextElementz)Base class for elements that render text.noner(   r)   r!   Nc                 C   s   | | j| _tdd| _d S Nleftjustify)enter_style
style_namestyler   r/   r+   r#   r#   r&   r-   ]   s   zTextElement.on_enterr/   c                 C   s(   | j |t|tr|j d S d  d S N)r/   append
isinstancestrcurrent_styler0   r#   r#   r&   r1   a   s   (zTextElement.on_textc                 C   s   |   d S rL   )leave_styler+   r#   r#   r&   r2   d      zTextElement.on_leaver:   )	r;   r<   r=   rB   rJ   r-   r   r1   r2   r#   r#   r#   r&   rC   X   s    
rC   c                   @   s`   e Zd ZU dZdZeed< edddedd fdd	Z	dedd
fddZ
dededefddZd
S )	ParagraphzA Paragraph.zmarkdown.paragraphrH   r   r   r    r!   c                 C   s   | |j pddS rE   rG   r$   r#   r#   r&   r'   n   s   zParagraph.createNc                 C   s
   || _ d S rL   rG   )r,   rH   r#   r#   r&   __init__r   s   
zParagraph.__init__r6   r7   c                 c   s    | j | j_ | jV  d S rL   )rH   r/   r8   r#   r#   r&   r9   u   s   
zParagraph.__rich_console__)r;   r<   r=   rB   rJ   r   r?   r@   r   r'   rT   r   r   r   r9   r#   r#   r#   r&   rS   h   s   
 rS   c                       sd   e Zd ZdZedddedd fddZdddZdedd
f fddZ	de
dedefddZ  ZS )Headingz
A heading.r   r   r    r!   c                 C   s   | |j }|S rL   )level)r%   r   r    headingr#   r#   r&   r'         
zHeading.creater(   r)   Nc                 C   s   t  | _|| j d S rL   )r   r/   rI   rJ   r+   r#   r#   r&   r-      s   zHeading.on_enterrV   c                    s    || _ d| | _t   d S )Nz
markdown.h)rV   rJ   superrT   )r,   rV   	__class__r#   r&   rT      s   zHeading.__init__r6   r7   c                 c   sL    | j }d|_| jdkrt|tjddV  d S | jdkr!tdV  |V  d S )Ncenterr
   zmarkdown.h1.border)r   rK       )r/   rH   rV   r   r   DOUBLEr   )r,   r6   r7   r/   r#   r#   r&   r9      s   



zHeading.__rich_console__r:   )r;   r<   r=   rB   r@   r   r'   r-   intrT   r   r   r   r9   __classcell__r#   r#   rZ   r&   rU   |   s    
rU   c                   @   sZ   e Zd ZdZdZedddedd fddZd	ed
eddfddZ	de
dedefddZdS )	CodeBlockz&A code block with syntax highlighting.zmarkdown.code_blockr   r   r    r!   c                 C   s(   |j pd}|dd }| |pd|jS )Nr^    r   default)info	partition
code_theme)r%   r   r    	node_info
lexer_namer#   r#   r&   r'      s   
zCodeBlock.createri   themeNc                 C   s   || _ || _d S rL   )ri   rj   )r,   ri   rj   r#   r#   r&   rT      s   
zCodeBlock.__init__r6   r7   c                 c   s8    t | j }tt|| j| jddtjd}|V  d S )Nrj   dim)border_styler   )	rO   r/   rstripr   r   ri   rj   r   SQUARE)r,   r6   r7   codesyntaxr#   r#   r&   r9      s   
zCodeBlock.__rich_console__)r;   r<   r=   rB   rJ   r@   r   r'   rO   rT   r   r   r   r9   r#   r#   r#   r&   rb      s    rb   c                   @   sJ   e Zd ZdZdZdddZddd	d
defddZdede	de
fddZdS )
BlockQuotezA block quote.zmarkdown.block_quoter!   Nc                 C      t  | _d S rL   r   elementsr,   r#   r#   r&   rT      rR   zBlockQuote.__init__r(   r)   r3   r   c                 C      | j | dS NFru   rM   r4   r#   r#   r&   r5         zBlockQuote.on_child_closer6   r7   c           	      c   sd    |j |jd d}|j| j|| jd}| j}td}td|}|D ]}|V  |E d H  |V  q"d S )N   widthrK   
u   ▌ )update	max_widthrender_linesru   rK   r   )	r,   r6   r7   render_optionslinesrK   r   paddingliner#   r#   r&   r9      s   

zBlockQuote.__rich_console__r!   N)r;   r<   r=   rB   rJ   rT   r>   r5   r   r   r   r9   r#   r#   r#   r&   rr      s$    

rr   c                   @   s*   e Zd ZdZdZdededefddZdS )	HorizontalRulez%A horizontal rule to divide sections.Fr6   r7   r!   c                 c   s     |j ddd}t|dV  d S )Nzmarkdown.hrrD   rd   r~   )	get_styler   )r,   r6   r7   rK   r#   r#   r&   r9      s   zHorizontalRule.__rich_console__N)	r;   r<   r=   rB   r   r   r   r   r9   r#   r#   r#   r&   r      s    r   c                   @   sp   e Zd ZdZedddedd fddZded	ee	 dd
fddZ
dddddefddZdededefddZd
S )ListElementzA list element.r   r   r    r!   c                 C   s   |j }| |d |d S )Ntypestart)	list_data)r%   r   r    r   r#   r#   r&   r'      s   zListElement.create	list_type
list_startNc                 C   s   g | _ || _|| _d S rL   )itemsr   r   )r,   r   r   r#   r#   r&   rT      s   
zListElement.__init__r(   r)   r3   r   c                 C   s   t |tsJ | j| dS rx   )rN   ListItemr   rM   r4   r#   r#   r&   r5      s   zListElement.on_child_closer6   r7   c                 c   s~    | j dkr| jD ]}|||E d H  q	d S | jd u rdn| j}|t| j }| jD ]}|||||E d H  |d7 }q+d S )Nbulletr
   )r   r   render_bulletr   lenrender_number)r,   r6   r7   itemnumberlast_numberr#   r#   r&   r9      s   



zListElement.__rich_console__)r;   r<   r=   rB   r@   r   r'   rO   r   r`   rT   r>   r5   r   r   r   r9   r#   r#   r#   r&   r      s&    
r   c                
   @   sh   e Zd ZdZdZdddZddd	d
defddZdede	de
fddZdede	dedede
f
ddZdS )r   zAn item in a list.zmarkdown.itemr!   Nc                 C   rs   rL   rt   rv   r#   r#   r&   rT   
  rR   zListItem.__init__r(   r)   r3   r   c                 C   rw   rx   ry   r4   r#   r#   r&   r5     rz   zListItem.on_child_closer6   r7   c                 c   s    |j |jd d}|j| j|| jd}|jddd}td|}td|}td	}t|D ]\}	}
|	r5|n|V  |
E d H  |V  q-d S )
N   r|   r~   zmarkdown.item.bulletrD   r   u    • z   r   )r   r   r   ru   rK   r   r   r   )r,   r6   r7   r   r   bullet_styler   r   r   firstr   r#   r#   r&   r     s   


zListItem.render_bulletr   r   c                 c   s    t t|d }|j|j| d}|j| j|| jd}|jddd}td}	td| |}
t| 	|d	 d |}t
|D ]\}}|rG|n|
V  |E d H  |	V  q?d S )
Nr]   r|   r~   zmarkdown.item.numberrD   r   r   rc   r
   )r   rO   r   r   r   ru   rK   r   r   rjustr   )r,   r6   r7   r   r   number_widthr   r   number_styler   r   numeralr   r   r#   r#   r&   r      s   
zListItem.render_numberr   )r;   r<   r=   rB   rJ   rT   r>   r5   r   r   r   r   r`   r   r#   r#   r#   r&   r     s.    

r   c                       sp   e Zd ZdZdZedddeddfdd	Zd
ede	ddf fddZ
d fddZdededefddZ  ZS )	ImageItemz#Renders a placeholder for an image.Fr   r   r    r!   r   c                 C   s   | |j |jS r"   )destination
hyperlinksr$   r#   r#   r&   r'   6  s   zImageItem.creater   r   Nc                    s    || _ || _d | _t   d S rL   )r   r   linkrY   rT   )r,   r   r   rZ   r#   r&   rT   C  s   zImageItem.__init__r(   r)   c                    s&   |j j| _tdd| _t | d S rE   )rP   r   r   r/   rY   r-   r+   rZ   r#   r&   r-   I  s   
zImageItem.on_enterr6   r7   c                 c   sb    t | jp	| jp	d d}| jpt| jdddd }| jr%|| tj	d|dddV  d S )	Nr   /r
   u   🌆 rc   r^   )end)
r   r   r   r/   r   striprsplitr   stylizeassemble)r,   r6   r7   
link_styletitler#   r#   r&   r9   N  s   "
zImageItem.__rich_console__r:   )r;   r<   r=   rB   r   r@   r   r'   rO   r>   rT   r-   r   r   r   r9   ra   r#   r#   rZ   r&   r   1  s    r   c                   @   s   e Zd ZdZ		ddedededee ded	dfd
dZ	e
d	efddZdeded	dfddZdeeef d	efddZd	efddZdS )r)   z!Manages the console render state.Nmonokair6   r7   rK   inline_code_lexerinline_code_themer!   c                 C   sD   || _ || _t|| _t | _d | _|d ur td||d| _d S d S )Nr^   rk   )r6   r7   r   style_stackr   stack_syntaxr   )r,   r6   r7   rK   r   r   r#   r#   r&   rT   [  s   
zMarkdownContext.__init__c                 C   s   | j jS )z>Current style which is the product of all styles on the stack.)r   currentrv   r#   r#   r&   rP   l  s   zMarkdownContext.current_styler/   	node_typec                 C   s\   |dv r$| j dur$| j |}|  | jj| tj|| jj	d dS | jj| | dS )z#Called when the parser visits text.rp   Nr~   )
r   	highlightrn   r   topr1   r   r   r   r   )r,   r/   r   highlight_textr#   r#   r&   r1   q  s   zMarkdownContext.on_textrJ   c                 C   s"   | j j|dd}| j| | jS )zEnter a style context.rD   r   )r6   r   r   pushrP   )r,   rJ   rK   r#   r#   r&   rI   |  s   zMarkdownContext.enter_stylec                 C   s   | j  }|S )zLeave a style context.)r   pop)r,   rK   r#   r#   r&   rQ     rX   zMarkdownContext.leave_style)Nr   )r;   r<   r=   rB   r   r   r   r   rO   rT   propertyrP   r1   r   rI   rQ   r#   r#   r#   r&   r)   X  s,    
r)   c                   @   s   e Zd ZU dZeeeeee	e
edZeeeee f  ed< h dZ						dd	ed
edee deeef dedee dee ddfddZdededefddZdS )r   a  A Markdown renderable.

    Args:
        markup (str): A string containing markdown.
        code_theme (str, optional): Pygments theme for code blocks. Defaults to "monokai".
        justify (JustifyMethod, optional): Justify value for paragraphs. Defaults to None.
        style (Union[str, Style], optional): Optional style to apply to markdown.
        hyperlinks (bool, optional): Enable hyperlinks. Defaults to ``True``.
        inline_code_lexer: (str, optional): Lexer to use if inline code highlighting is
            enabled. Defaults to "python".
        inline_code_theme: (Optional[str], optional): Pygments theme for inline code
            highlighting, or None for no highlighting. Defaults to None.
    )	paragraphrW   
code_blockblock_quotethematic_breaklistr   imageru   >   rp   emphstrikestrongr   NrD   Tmarkuprg   rH   rK   r   r   r   r!   c           	      C   sD   || _ t }||| _|| _|| _|| _|| _|| _|p|| _	d S rL   )
r   r	   parseparsedrg   rH   rK   r   r   r   )	r,   r   rg   rH   rK   r   r   r   parserr#   r#   r&   rT     s   
zMarkdown.__init__r6   r7   c                 c   s   |j | jdd}t|||| j| jd}| j }| j}d}|D ]^\}}	|j}
|
dv r8|	|j
dd|
 q |
dkrE|	rD|	d|
 q |
d	krR|	rQ|	d|
 q |
d
kr|	rp|j ddd}| jrj|t|jd7 }|| q |  | js|	d|
 tdd|j ddd }|| |	|j|
 |  |	d|
 q |
|v r| r|	r|d|
  q |  q |d|
  |j
r|	|j
|
 |  q | j|
pt}| r3|	r|| |}|j| || q |j }|jr |jj||r|r
tdV  |||jE dH  || n|| n|| |||jE dH  |j }q || |}|j| || |j
rQ|	||j
!  |j  |jj||rw|rgtdV  |||jE dH  || n|| |j }q dS )zRender markdown to the console.rD   r   )r   r   F)html_inline
html_blockr/   r   rc   	linebreak	softbreakr   zmarkdown.linkr   z (T)	underlinezmarkdown.link_url)z	markdown.N)"r   rK   r)   r   r   r   walkerinlinestr1   literalreplacer   r   r   rI   rQ   is_containerru   getrA   r'   r   r   r-   r   r   r5   r   renderr7   r2   r   rn   )r,   r6   r7   rK   r(   nodesr   r   r   enteringr   r   element_classelementr#   r#   r&   r9     s   











zMarkdown.__rich_console__)r   NrD   TNN)r;   r<   r=   rB   rS   rU   rb   rr   r   r   r   r   ru   r   r   rO   r   r   r?   r   r   r   r   r   r>   rT   r   r   r   r9   r#   r#   r#   r&   r     sT   
 

	
r   __main__Nz(Render Markdown to the console with Rich)descriptionpathPATHz%path to markdown file, or - for stdin)metavarhelpz-cz--force-colorforce_color
store_truezforce color for non-terminals)destactionrd   r   z-tz--code-themerg   r   zpygments code theme)r   rd   r   z-iz--inline-code-lexerr   z-yz--hyperlinksr   zenable hyperlinks)r   r   r   z-wz--widthr}   z*width of output (default will auto-detect))r   r   rd   r   z-jz	--justifyrH   zenable full text justifyz-pz--pagepagezuse pager to scroll output)r   -rtzutf-8)encodingfullrF   )rH   rg   r   r   )fileforce_terminalr}   )r   r}   )Stypingr   r   r   r   r   r   r   commonmark.blocksr	   r^   r   _loopr   _stackr   r6   r   r   r   r   
containersr   jupyterr   panelr   ruler   segmentr   rK   r   r   rq   r   r/   r   r   r   rA   rC   rS   rU   rb   rr   r   r   r   r   r)   r   r;   argparsesysArgumentParserr   add_argumentr`   
parse_argsargsrich.consoler   stdinreadmarkdown_bodyopenmarkdown_filerH   rg   r   r   r   r   pydocioStringIOr   r}   printpagerr   getvaluer#   r#   r#   r&   <module>   s   $ <	$",'0 


