xhdlddlmZddlZddlmZmZmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZdd lmZmZmZmZdd lmZdd lmZddlmZddlmZddl m!Z!ddl"m#Z#m$Z$ddl%m&Z&ddl'm(Z(m)Z)GddZ*Gdde*Z+Gdde*Z,Gdde,Z-Gdde,Z.Gdde,Z/Gd d!e,Z0Gd"d#e*Z1Gd$d%e*Z2Gd&d'e*Z3Gd(d)e*Z4Gd*d+e*Z5Gd,d-e*Z6Gd.d/e*Z7Gd0d1e,Z8Gd2d3e,Z9Gd4d5e,Z:Gd6d7Z;Gd8d9eZZ>ddlZe>j?d;<Z@e@Ad=d>d?@e@AdAdBdCdDddEFe@AdGdHdIdJdKLe@AdMdNdOddOLe@AdPdQdRdDdSTe@AdUdVeBdWddXYe@AdZd[d\dDd]Te@Ad^d_d`dDdaTe@CZDddblEmZeDjFdckrejGHZIn>eJeDjFdde5ZKeKHZIdddn #1swxYwYes tr*consoleroptionsrrcdS)Nr%r%r0r>r?s r(__rich_console__z MarkdownElement.__rich_console__Ls rr*Nr r!r"rr#rr+r,r#r-r+r,r3rr#r-r+r,r9rr#r:r>rr?rr#r) __name__ __module__ __qualname__r__annotations__ classmethodr)r1r6r8r=rBr%r*r(rrs#H####   [                 r*rceZdZdZdS)UnknownElementzAn unknown element. Hopefully there will be no unknown elements, and we will have a MarkdownElement for everything in the document. N)rHrIrJ__doc__r%r*r(rNrNRsr*rNc.eZdZdZdZd dZdd Zd d Zd S) TextElementz)Base class for elements that render text.noner+r,r#r-cn||j|_td|_dSNleftjustify) enter_style style_namestylerr3r/s r(r1zTextElement.on_enter`s/((99 ((( r*r3rct|j|t|tr|jnddSN)r3append isinstancestr current_styler5s r(r6zTextElement.on_textds8  48M8MWw44SWXXXXXr*c.|dSr\) leave_styler/s r(r8zTextElement.on_leavegsr*NrDrE)rHrIrJrOrYr1r6r8r%r*r(rQrQ[s_33J))))YYYYr*rQcJeZdZUdZdZded<edd Zdd ZddZ dS) Paragraphz A Paragraph.zmarkdown.paragraphrrWr r!r"rr#c(||jpdSrTrVr&s r(r)zParagraph.createqss8+5v6666r*r-c||_dSr\rVr0rWs r(__init__zParagraph.__init__us  r*r>rr?rrc#>K|j|j_|jVdSr\)rWr3rAs r(rBzParagraph.__rich_console__xs#!L ir*N)r r!r"rr#rdrWrr#r-rG) rHrIrJrOrYrKrLr)rhrBr%r*r(rdrdkss%J777[7r*rdcLeZdZdZeddZdd Zdfd ZddZxZ S)Headingz A heading.r r!r"rr#c"||jSr\)tagr&s r(r)zHeading.createss59~~r*r+r,r-c`t|_||jdSr\)rr3rXrYr/s r(r1zHeading.on_enters)FF DO,,,,,r*rnr_cj||_d||_tdS)N markdown.)rnrYsuperrh)r0rn __class__s r(rhzHeading.__init__s5+c++ r*r>rr?rrc#K|j}d|_|jdkr t|tjdVdS|jdkrt dV|VdS)Ncenterh1zmarkdown.h1.border)r rZh2)r3rWrnrr HEAVYr)r0r>r?r3s r(rBzHeading.__rich_console__sy 8t  I*     x42hhJJJJJr*)r r!r"rr#rlrD)rnr_r#r-rG) rHrIrJrOrLr)r1rhrB __classcell__rss@r(rlrls[---- r*rlc>eZdZdZdZeddZdd ZddZdS) CodeBlockz&A code block with syntax highlighting.zmarkdown.code_blockr r!r"rr#cp|jpd}|dd}||pd|jS)Nrx rr3)info partition code_theme)r'r r" node_info lexer_names r(r)zCodeBlock.creates@J$" ((--a0 s:')<===r*rr_themer-c"||_||_dSr\)rr)r0rrs r(rhzCodeBlock.__init__s$ r*r>rr?rrc#Kt|j}t||j|jdd}|VdS)NTr )r word_wrappadding)r_r3rstriprrr)r0r>r?codesyntaxs r(rBzCodeBlock.__rich_console__sS49~~$$&& $/tQ    r*N)r r!r"rr#r})rr_rr_r#r-rG) rHrIrJrOrYrLr)rhrBr%r*r(r}r}se00&J>>>[> r*r}c.eZdZdZdZddZdd ZddZdS) BlockQuotezA block quote.zmarkdown.block_quoter#r-c,t|_dSr\relementsr0s r(rhzBlockQuote.__init__%0]] r*r+r,r9rr:c:|j|dSNFrr]r<s r(r=zBlockQuote.on_child_close U###ur*r>rr?rrc#K||jdz }||j||j}|j}t d}t d|}|D]}|V|Ed{V|VdS)NwidthrZ u▌ )update max_width render_linesrrZr) r0r>r?render_optionslinesrZrrlines r(rBzBlockQuote.__rich_console__s!g.?!.CDD$$T]N$*$UU 4==&%((  DMMMOOOOOOONNNN  r*Nr#r-rFrG)rHrIrJrOrYrhr=rBr%r*r(rrs['J3333      r*rceZdZdZdZd d Zd S) HorizontalRulez%A horizontal rule to divide sections.Fr>rr?rr#rc#\K|dd}t|VdS)Nz markdown.hrrRdefaultr) get_styler)r0r>r?rZs r(rBzHorizontalRule.__rich_console__s=!!-!@@r*NrG)rHrIrJrOrrBr%r*r(rrs3//H      r*rc*eZdZdZddZdd ZddZdS) TableElementz.MarkdownElement corresponding to `table_open`.r#r-c"d|_d|_dSr\)headerbodyrs r(rhzTableElement.__init__s15 -1 r*r+r,r9rr:ct|tr||_n,t|tr||_nt ddS)Nz Couldn't process markdown table.F)r^TableHeaderElementrTableBodyElementr RuntimeErrorr<s r(r=zTableElement.on_child_closesP e/ 0 0 CDKK / 0 0 CDIIABB Bur*r>rr?rrc#*Kttj}|j:|jj.|jjjD]}||j|j*|jj D]}d|jD}|j ||VdS)Nr cg|] }|j Sr%)content).0elements r( z1TableElement.__rich_console__..sHHH7wHHHr*) r r SIMPLE_HEAVYrrowcells add_columnrrrowsadd_row)r0r>r?tablecolumnr row_contents r(rBzTableElement.__rich_console__s#*+++ ; "t{'B+// 1 1  0000 9 y~ , ,HHciHHH  {+++ r*NrrFrG)rHrIrJrOrhr=rBr%r*r(rrsV882222r*rc"eZdZdZd dZd d Zd S)rz@MarkdownElement corresponding to `thead_open` and `thead_close`.r#r-cd|_dSr\)rrs r(rhzTableHeaderElement.__init__s +/r*r+r,r9rr:cBt|tsJ||_dSr)r^TableRowElementrr<s r(r=z!TableHeaderElement.on_child_closes$%11111ur*NrrFrHrIrJrOrhr=r%r*r(rrsBJJ0000r*rc"eZdZdZd dZd d Zd S)rz@MarkdownElement corresponding to `tbody_open` and `tbody_close`.r#r-cg|_dSr\)rrs r(rhzTableBodyElement.__init__s +- r*r+r,r9rr:cht|tsJ|j|dSr)r^rrr]r<s r(r=zTableBodyElement.on_child_closes3%11111 ur*NrrFrr%r*r(rrsBJJ....r*rc"eZdZdZd dZd d Zd S)rz:MarkdownElement corresponding to `tr_open` and `tr_close`.r#r-cg|_dSr\)rrs r(rhzTableRowElement.__init__s -/ r*r+r,r9rr:cht|tsJ|j|dSr)r^TableDataElementrr]r<s r(r=zTableRowElement.on_child_close s4%!122222 %   ur*NrrFrr%r*r(rrsBDD0000r*rc:eZdZdZeddZdd ZddZdS)rz[MarkdownElement corresponding to `td_open` and `td_close` and `th_open` and `th_close`.r r!r"rr#rct|jdpd}d|vrd}nd|vrd}n d|vrd}nd }|ttvsJ|| S) NrZrxztext-align:rightrightztext-align:centerruztext-align:leftrUrrV)r_attrsgetrr)r'r r"rZrWs r(r)zTableDataElement.create*sEKOOG,,--3  & &GG E ) )GG % ' 'GGG(=111111s7####r*rWrr-c@td||_||_dS)NrxrV)rrrWrgs r(rhzTableDataElement.__init__;s!!"g666  r*r+r,r3rct|trt|n|}||j|j|dSr\)r^r_rstylizer`r append_textr5s r(r6zTableDataElement.on_text?sQ'c22#>??@@@r* list_typer_ list_start int | Noner-c0g|_||_||_dSr\)itemsrr)r0rrs r(rhzListElement.__init__Ls%' "$r*r+r,r9rr:cht|tsJ|j|dSr)r^ListItemrr]r<s r(r=zListElement.on_child_closeQs3%***** %   ur*r>rr?rrc#4K|jdkr(|jD]}|||Ed{VdS|jdn|j}|t |jz}t |jD]&\}}|||||z|Ed{V'dS)Nbullet_list_openr )rr render_bulletrlen enumerate render_number)r0r>r?itemnumber last_numberindexs r(rBzListElement.__rich_console__Vs >/ / /  @ @--gw?????????? @ @/1QQtF 3tz??2K(44   t--Wfunk  r*N)r r!r"rr#r)rr_rrr#r-rFrG) rHrIrJrOrLr)rhr=rBr%r*r(rrEsxAAA[A%%%%       r*rc6eZdZdZdZddZdd ZddZddZdS)rzAn item in a list.z markdown.itemr#r-c,t|_dSr\rrs r(rhzListItem.__init__jrr*r+r,r9rr:c:|j|dSrrr<s r(r=zListItem.on_child_closemrr*r>rr?rrc#hK||jdz }||j||j}|dd}t d|}t d|}t d }t|D]\} } | r|n|V| Ed{V|VdS) Nrrzmarkdown.item.bulletrRru • z r)rrrrrZrrr ) r0r>r?rr bullet_stylebulletrrfirstrs r(rzListItem.render_bulletqs g.?!.CDD$$T]N$*$UU(()?(PP ,//'<004==%e,,  KE4!.&&w . . .OOOOOOONNNN  r*rrrc#Ktt|dz}||j|z }||j||j}|dd}td} td|z|} t| |d z dz|} t|D]\} } | r| n| V| Ed{V| VdS) Nrrzmarkdown.item.numberrRrrrr ) rr_rrrrrZrrrjustr )r0r>r?rr number_widthrr number_stylerrnumeralrrs r(rzListItem.render_number~s3{++,,q0  g.?,.NOO$$T]N$*$UU(()?(PP 4==# ,l;;V+++L1,<==C\RR%e,,  KE4"/'' / / /OOOOOOONNNN  r*NrrFrG) r>rr?rrrrrr#r) rHrIrJrOrYrhr=rrr%r*r(rreso J3333    r*rc.eZdZed dZdd Zd S)Linkr r!r"rr#rct|jdd}||jt|S)Nhref#)rrrr_)r'r r"urls r(r)z Link.creates1koofc**s5=#c((+++r*r3r_rc<t||_||_dSr\)rr3r)r0r3rs r(rhz Link.__init__sJJ  r*NrC)r3r_rr_)rHrIrJrLr)rhr%r*r(rrsF,,,[,r*rcTeZdZdZdZedd Zdfd Zdfd ZddZ xZ S) ImageItemz#Renders a placeholder for an image.Fr r!r"rr#rcp|t|jdd|jS)zFactory to create markdown element, Args: markdown (Markdown): The parent Markdown object. token (Any): A token from markdown-it. Returns: MarkdownElement: A new markdown element srcrx)r_rr hyperlinksr&s r(r)zImageItem.creates1s3u{ub1122H4GHHHr* destinationr_rr:r-cr||_||_d|_tdSr\)r rlinkrrrh)r0r rrss r(rhzImageItem.__init__s5&$ $  r*r+r,c|jj|_td|_t |dSrT)r`r rr3rrr1)r0r+rss r(r1zImageItem.on_enters@). (((  !!!!!r*r>rr?rrc#FKt|jp|jpd}|jp@t |jdddd}|jr||t j d|dd}|VdS) Nr /r u🌆 rrx)end) rr r r3rstriprsplitrrassemble)r0r>r? link_styletitler3s r(rBzImageItem.__rich_console__s ET-= EFFF  QT$"2"8"8"="="D"DS!"L"LR"PQQ ? & MM* % % %}WeSb999 r*rC)r r_rr:r#r-rDrG) rHrIrJrOrrLr)rhr1rBrzr{s@r(rrs--H I I I[ I """""" r*rcPeZdZdZ dddZeddZddZddZddZ dS)r,z!Manages the console render state.Nmonokair>rr?rrZrinline_code_lexer str | Noneinline_code_themer_r#r-c||_||_t||_t |_d|_|td|||_dSdS)Nrx)r)r>r?r style_stackrstack_syntaxr)r0r>r?rZrrs r(rhzMarkdownContext.__init__s^  '1%'8'8-2WW &*  (!"&7?PQQQDLLL ) (r*c|jjS)z>Current style which is the product of all styles on the stack.)rcurrentrs r(r`zMarkdownContext.current_styles''r*r3 node_typec8|dvru|jn|j|}||jj|t j||jj dS|jj||dS)z#Called when the parser visits text.>fence code_inlineNr) r highlightrrtopr6rrrr!)r0r3r"highlight_texts r(r6zMarkdownContext.on_texts 0 0 0T\5M!\33D99N  ! ! # # # JN " "dmN$:J:RSSS      JN " "4 . . . . .r*rY str | Stylec||j|d}|j||jS)zEnter a style context.rRr)r>rrpushr`)r0rYrZs r(rXzMarkdownContext.enter_styles< &&z6&BB e$$$!!r*c8|j}|S)zLeave a style context.)rpop)r0rZs r(rbzMarkdownContext.leave_styles $$&& r*)Nr) r>rr?rrZrrrrr_r#r-)r#r)r3r_r"r_r#r-)rYr)r#r) rHrIrJrOrhpropertyr`r6rXrbr%r*r(r,r,s++)-!* RRRRR"(((X( / / / /"""" r*r,ceZdZUdZidedededededede d e d e d e d e d e dedededeZded<hdZ d1d2d'Zd3d*Zd4d0ZdS)5r!aA Markdown renderable. Args: markup (str): A string containing markdown. code_theme (str, optional): Pygments theme for code blocks. Defaults to "monokai". See https://pygments.org/styles/ for code themes. 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 None. inline_code_theme: (Optional[str], optional): Pygments theme for inline code highlighting, or None for no highlighting. Defaults to None. paragraph_open heading_openr$ code_blockblockquote_openhrrordered_list_openlist_item_openimage table_open tbody_open thead_opentr_opentd_openth_openz*ClassVar[dict[str, type[MarkdownElement]]]r>semrstrongrNrRTmarkupr_rrWJustifyMethod | NonerZr)rr:rrrr#r-ctdd}||_|||_||_||_||_||_||_ |p||_ dS)N strikethroughr) renablerAparseparsedrrWrZrrr) r0rArrWrZrrrparsers r(rhzMarkdown.__init__sz$$_55<rr?rrc#h K||jd}|d}t||||j|j}|j}|j}d}tj }| |D]} | j } | j } | j dk} | j dk} | j d k}| d kr|| j| T| d kr|d | q| d kr|d| | dkrt!| jdd}|jrA|dd}|t)|z }|||jt0|| 9| dkr6|jr|]|j}t9|t0sJ|dd}||||jj| ||d| |dd}||||j| ||d| v| |vr| dkr| dkr| r|d| | r||d| | jr|| j| | |j | j ptB}||| }| s|r/|j||"|| rr|j}|j p&|jo|jj#$||}|r'|r|V|%||j&Ed{Vn|r|j| j}|||||j p&|jo|jj#$||}|r-|r | dkr|V|%||j&Ed{V| s|r|'||j(}dS)zRender markdown to the console.rRrN)height)rrFr rrr3 hardbreakr softbreakr link_openrrxzmarkdown.link_urlr link_closez markdown.linkz ()r$r2rqinline))rrZrr,rrrGinlinesrrrNrrnnestingr6rr_rrrrrXrr+rr)rbr-r^r3plainrrrNr1r'r=renderr?r8r)r0r>r?rZr+rIinline_style_tagsr_new_line_segmentr"r"rnenteringexiting self_closingrrrlink_url_style element_class should_renderr3s r(rBzMarkdown.__rich_console__4s!!$*f!==...--!   "4"4     L#LNN))&11c 0c 0E I)C})Hmr)G =A-LF"" y9999k))i0000k))Y////k))5;??626677?A!(!2!23FPV!2!W!WJ%T"2"2"22J'' 3333M&&t{{4'?'?@@@@l**?4''))))%m//11G%gt44444!(!2!2?F!2!S!SJ'' 333OOGL$6 BBB'')))OOD)444%,%6%6+V&7&&N''777OOGL)<<<'')))OOC3333(((((-- *''(9C(9(9:::: *''))))''(9C(9(9:::}B yAAA''))))!% 1 1%* = = O '..tU;;.|.M&&w///$$W---L%m//11G(/ $5% O#M-<>'7?#K#KKKKKKKK!LM%%''' =D'666$M)O"=O#M-<>'7?#K#KKKKKKKK0l0$$W---&/HGc 0c 0r*)rNrRTNN)rAr_rr_rWrBrZr)rr:rrrrr#r-)rIrJr#rJrG)rHrIrJrOrdrlr}rrrrrrrrrrrrKrYrhrNrBr%r*r(r!r!sh  <)<< < i < : < n < K< [< (< < l< &< (< ?< #< #!<H&,++G $(,#(,(,AAAAA(u0u0u0u0u0u0r*r!__main__z(Render Markdown to the console with Rich) descriptionpathPATHz%path to markdown file, or - for stdin)metavarhelpz-cz --force-color force_color store_truezforce color for non-terminals)destactionrrjz-tz --code-themerrzpygments code theme)rmrrjz-iz--inline-code-lexerrz-yz --hyperlinksrzenable hyperlinks)rmrnrjz-wz--widthrz*width of output (default will auto-detect))rrmrrjz-jz --justifyrWzenable full text justifyz-pz--pagepagezuse pager to scroll output)r-zutf-8)encodingfullrU)rWrrr)fileforce_terminalrT)rtrrecord)[ __future__rsystypingrrr markdown_itrmarkdown_it.tokenr rich.tabler rxr _loopr _stackrr>rrrr containersrjupyterrpanelrrulersegmentrrZrrrrr3rrrrNrQrdrlr}rrrrrrrrrrrr,r!rHargparseArgumentParserrH add_argumentr parse_argsargs rich.consolergstdinread markdown_bodyopen markdown_filerWrrrr roiopydocStringIOfileiorkrprintpagergetvaluer%r*r(rs"""""" //////////""""""######IIIIIIIIIIII######!!!!!!$$$$$$$$ 66666666r_     /     (     k   F 24      _    ?B                     o   ''''''''>/@'''''{'''T;%%%%% %%%P--------`w0w0w0w0w0|w0w0w0t zOOOJJJ $X $>F  4     ,     "              9     '     )      D$$$$$$ yC (( T$)g . . . 1-)..00M 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1x,2F??0 H y   '(8      h FOO%%&&&&&'+4:d     hsK##K'*K'