o ]Lbt @sdZddlmZddlZddlmZddlmZmZm Z ddl m Z dd Z d d Z d d ZddZddZddZedZddZddZedZedZedZedZedZddZd Zd!d"Zd#d$Zd%d&Zed'Z d(d)Z!d*d+Z"d,d-Z#d.d/Z$d0d1Z%d2d3Z&dWd4d5Z'ed6ed7ed8ed9ed:ed;ed<ed=ed>d? Z(d@dAZ)dBdCZ*dDdEZ+dXdFdGZ,dHdIZ-dJdKZ.dYdNdOZ/dPdQZ0dRdSZ1dZdUdVZ2dS)[asimplified reStructuredText parser. This parser knows just enough about reStructuredText to parse the Mercurial docstrings. It cheats in a major way: nested blocks are not really nested. They are just indented blocks that look like they are nested. This relies on the user to keep the right indentation for the blocks. Remember to update https://mercurial-scm.org/wiki/HelpStyleGuide when adding support for new constructs. )absolute_importN)_)encodingpycompaturl) stringutilcCd|dt|fS)N%s %s "rcolwidthsr3/usr/lib/python3/dist-packages/mercurial/minirst.pysection"rcCr )Nr =r rrrr subsection&rrcCr )Nr -r rrrr subsubsection*rrcCr )Nr .r rrrrsubsubsubsection.rrcCr )Nr 'r rrrrsubsubsubsubsection2rrcCsJ|ttj}|D]\}}||d|d}q |ttjS)aB Apply a list of (find, replace) pairs to a text. >>> replace(b"foo bar", [(b'f', b'F'), (b'b', b'B')]) 'Foo Bar' >>> encoding.encoding = b'latin1' >>> replace(b'\x81\\', [(b'\\', b'/')]) '\x81/' >>> encoding.encoding = b'shiftjis' >>> replace(b'\x81\\', [(b'\\', b'/')]) '\x81\\' ascii)decodersysstrrreplaceencode)textsubstsutextftrrrr6s rs \n(?:\s*\n)+csbg}t|dD]"}|}|r.tdd|Dfdd|D}||dq |S)zFind continuous blocks of lines in text. Returns a list of dictionaries representing the blocks. Each block has an 'indent' field and a 'lines' field.  css$|] }t|t|VqdSN)lenlstrip.0lrrr [s"zfindblocks..csg|]}|dqSr'rr*indentrr \szfindblocks..)indentlines)_blockresplitr)rstrip splitlinesminappend)r!blocksblinesrr.r findblocksQsr<cCsd}|t|krd||d<||dddr|dt|kr||d}||dd|}||ddgkrB||=|d8}n[||ddd r`||ddd d ||dd<n=t||ddkr||ddd d r||ddd ddkr|d7}q||ddd d||dd<t||dd}|r||7}||8}|dt|kr||dd|krd||dd<||dd|8<|d7}|dt|kr||dd|ks|d7}|t|ks|S)zFinds literal blocks and adds a 'type' field to the blocks. Literal blocks are given the type 'literal', all other blocks are given type the 'paragraph'. r paragraphtyper2s::rr1s ::N .. literal)r(endswithr) startswithfind _bulletrematchend)r9ir/ adjustmentmrrrfindliteralblocksas<  &  &$  $$ 2rNs+(\*|-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)|\|) s0^(-([a-zA-Z0-9]), )?(--[a-z0-9-]+)((.*) +)(.*)$s&:(?![: ])((?:\:|[^:])*)(?\}}}||d ||rlg}t|D]!\} } ||| ||rX||g||d d|dd | q@||||d<nq.|d7}|t|ks|S)zSplit paragraphs into lists.bulletToptionfield definitionFcSsf||}|dt|kr||dpd}||sdS|r.|dkp-|dddkp-||S|dS)zDoes itemre match an item at line i? A list item can be followed by an indented line or another list item (but only if singleline is True). rFrrA)r(rIrF)r;rKitemre singlelineline1line2rrrrIs  " zsplitparagraphs..matchrr>r=r2r1)r>r2r1r?r)rH _optionre_fieldre _definitionrer( enumerater8) r9 listtypesrIrKr;typerTrUitemsjlinerrrsplitparagraphss8    racCsd}|t|krd||ddkr|d7}q|}|t|krZ||ddkrZt||dd}|\}}|||dd<|dd||d<|d7}|t|krZ||ddks%|d}|t|ks|S) zFind key for field lists.rr>rQrr2s\::key)r(rYrIgroupsr)r9rKr_rMkeyrestrrrupdatefieldlistss    rhc Cs\d}|t|kr||ddkr|d7}qd}|}|t|kr||ddkrt||dd}|d}|d}|dd}|d}|d } |||dd<d } |s`d } d |rgd |phd d| || ff} | } | ||d<t|t| }|d7}|t|kr||ddks'|||D]} || d<q|d}|t|ks|S)Nrr>rPrr2rCrS s%s%ss-%s s %s--%s %soptstr optstrwidth) r(rXrIgroupstripr5maxrr ) r9rK optstrwidthr_rM shortoptiongroup3 longoptiondesc longoptionarg noshortopoptblockrrrupdateoptionlistss@       !r{c Cs6g}d}|dt|kr||ddkr||dddr||d}||dd|}||dddd }d }|D] }||d vrJd }q?|rR||||=|} |d8}| t|kr|| d|kr|ro|| =n|| d|8<| d7} | t|kr|| d|ksi|d7}|dt|ks ||fS) zPrune unwanted containers. The blocks must have a 'type' field, i.e., they should have been run through findliteralblocks first. rrr>r=r2s.. container::r1NTrF)r(rFr4r8) r9keepprunedrKr/rL containertypeprunecr_rrrprunecontainerss<   rs^([-=`:.'"~^_*+#])\1+$c sH|D]}|ddkrt|ddkrt|ddr|dd|ddkrd|d<d|d <|ddfd d ttD}g}|dd dD]O}|krXd |d <qMg}|d}t|D]4\}}|d t|kr||d |} t||| } |t| 7}|| qb|||d qb||qM||d<q|S)zNFind simple tables Only simple one-line table elements are supported r>r=r2rirr?tableFheadercs@g|]}||ddkr|dks|d|dkr|qS)rrrrAr)r+xdivrrr0_s 0zfindtables..rTN) r(_tablererIrxranger[rgetcolsr8rp) r9rzcolumnsrowsr,rowposnstartwidthvrrr findtablesDs<      rcCs|D]?}|ddkrAt|ddkrAt|ddt|ddkrAt|ddrA|dddd|d<d|d<|dd=q|S) zFinds sections. The blocks must have a 'type' field, i.e., they should have been run through findliteralblocks first. r>r=r2rirr underlinesection)r(rr _sectionrerI)r9rzrrr findsections|s " rcs:dg|D]}|ddvrfdd|dD|d<q|S)N)s``r r>r=rcg|]}t|qSrrr*r"rrr0z"inlineliterals..r2rr9r:rrrinlineliteralss  rcs<ddg|D]}|ddvrfdd|dD|d<q|S)N)s:hg:`s'hg )`rr>rcrrrr*rrrr0rzhgrole..r2rrrrrhgroles  rcCsd}|t|krE||d||ddkr#||ddvr#|d7}n||dds0|d7}n||dgddd|d 7}|t|ks|S) zAdds empty blocks for vertical spacing. This groups bullets, options, and definitions together with no vertical space between them, and adds an empty block between all other blocks. rr>)rOrPrQr2rSrmargin)r2r1r>ri)r(insert)r9rKrrr addmarginss  "   rcCsd}|t|krA||}|ddkr7|ddds"|ddgkr7||=|t|kr6||ddkr6||=n|d7}|t|ks|S) zRemove comments.rr>r=r2rBs..rr)r(rF)r9rKr:rrr prunecommentss    rcCs|pt}tjddt|tjd}d}|t|krw|||dd}|rmd||d<||ddd| d  }||dd| d d }|r`||d d d ||||d <||dd=|d }|t|ks|S)zj Makes the type of the block an admonition block if the first line is an admonition directive s \.\. (%s)::|)flagsrr2 admonitionr>rCrirNrladmonitiontitle) _admonitiontitleskeysrecompilejoinsorted IGNORECASEr(rIrJlowerr)r9 admonitions admonitionrerKrMadmonitiontitle firstlinerrrfindadmonitionss$   $    rs Attention:sCaution:s!Danger!sError:sHint:s Important:sNote:sTip:sWarning!) s attentionscautionsdangerserrorshints importantsnotestipswarningcCstdttj|d}t|d}|d}|d}d|dd||f}dt|d}dtj||||dS) NrAr2rmrrns%s%s s %s ) initindent hangindent)rmapbytesrprr rwrap)rzrrvr usablewidthhangingrrrrr formatoptionsrcCs|dkrd}d|d}|ddkrTt|d}|ds"||d St|dd t|dd }||d}dttj|d}d ||tj||||d fS|dd kr\d S|ddkrs|d7}|d ||dd S|ddkrt |dd|d}d||dd||fS|ddkr|d}ddt |D} d}t | t| d} d|d}dt|| | d }|D]J} g} t | | D]\} }d| t |}| ||qd| } tj| |||d } |s |dr | d |dt|| d }q|| d 7}q|S|ddkrV||dd}t|dd t|dd }||d}dttj|ddd}d |tj||||d fS|}|ddkr|dddrx|dddd|dd<nPt|dd}||d}n>|ddkr|d }|td}t|dtkr||}n|t}||dd|dd<n |dd!krt||Sdttj|d}tj||||d d S)"z"Format a block according to width.rNrAr1r>rrr2r&r?s%s %s )rrrrrDs rrs %s%s %s%s rcSg|] }tdd|DqS)cSsg|]}t|qSrr r+errrr0)rz*formatblock...rqr+rrrrr0)zformatblock..rSrrrrRNrOs| rirQrdrP)rr(r)rrrrprrrr zipsumr8r7rFrHrIrJ _fieldwidthljustr)rzrr/ admonitionhang defindentr! underlinetablewidthsspanrr,wrpadterm subindentrMrfrrr formatblock s    $       $     rc sgd}gdd}fdd}dd|D}t|D]\}}|d}|d }|d }|d krO|t|d } |d ttj|} d| | fn6|dkrad|d|n$|dkrgn|dkryd|d|n |dkr|d} | |vr|| 7}|| d}d|||d|fn|dkr|d} d| D]+} d| D]}d||ddqdqd n|d!kr |d"|||d}|d ttj|dd#} d$|| fn||d%kr<|d d d\}}|d&vr#|d'|n|d(|d)|d |g|dd#nI|d*kra|d"|||d+}|d ttj|} d$|| fn$|d,kr|d"|||d-}|d ttj|}d$||frɈd.\}}|t |dkrd/|q||d}|d }||ks||kr|dd0vrɈd/|qdS)1zFormat RST blocks as HTMLrScSs t|dS)NT)rescaperrrrrns zformathtml..escapecs8r dd|kr||fd|dSdS)Nr?rs<%s> )r8)rlevellistnestoutrropenlistqszformathtml..openlistcSsg|] }|ddkr|qS)r>rrr+r:rrrr0vzformathtml..r>r1r2rrrAs

%s %s

r=s

%s

r&rrDs
%s
rrrs%s rrs sss s
s
rRsdlNs
%s
%s rO)*rsulsols
  • %s rQrdrPrmr?s sdefinition bullet field option) r[rrrrrpr8indexpopr4r()r9 headernestrrrr:btyperr;rr!rKrrrrbulletheadrfryrvrnbnirrr formathtmlgs              (            rcCst|}|D] }|d|7<qt|}t|}t||pg\}}t|}t|}t|}t|}t|}t |}t ||d}t |}t |}||fS)z Parse text into a list of blocksr1)r) r<rNrrrrrrarhr{rrr)r!r/r}rr9r:r~rrrparses  rcsdfdd|D}|S)NrSc3|]}t|VqdSr'rrrrrr-zformatblocks..r)r9rr!rrr formatblockssrcsdfdd|DS)z"Format parsed blocks as plain textrSc3rr'rrrrrr-rzformatplain..r)r9rrrr formatplainsrPplaincCs>t|||pg\}}|rt||}|dkrt|St||dS)z-Parse and format the text according to width.shtmlr)rfiltersectionsrr)r!rr/r}stylerr9r~rrrformats   rcs g}t}gd}g}g}d}|t|kr||\}} } || d=||||ks2|d|r||krt|} t|} | rF| | krFd}g} td| dD]+}||}|| ks`|||kr{| t||d}|d|d qP| r|| |dd}| |dt|kr||dd| kr|d7}||d|dt|kr||dd| ks|d7}|t|ks|r||D]0} fd d | D}| d d}|d }d d||dg dd|d<| d|=q҈S)zSelect parsed blocks under the specified section The section name is separated by a dot, and matches the suffix of the full section path. rTNrFrCrrir?csg|] }|ddqS)r2rr)r+synr9rrr0rz"filtersections..r2s"%s"r rS) _getsectionsr(r8rErrextendreverserr)r9rparentssectionsrK lastparents syntheticcollapsepathnestr:llenplenrr_parentsecrealreallinerrrrs^        $$    rcCsd}d}g}dd}|D]8}|ddkrF|d}||vr!||7}||d}|d |}|d |||f}|d |||gfq |dd vrd }||vrV||7}||d}|d |}tdt|dD]8}|| }|d|kr{n*d d|dD} | r| dd} |d} | | kr|d7}n | | kr|d}nql|d |||f}|d |||gfq |sddgfg}|ddkrrQrdr2rr ()rrrpr4)r:rrrrgetname+s   z_getsections..getnamer>rrrNr)rRrQrAcSsg|] }|ddkr|qS)r>rRr)r+arrrr0Hrz _getsections..rir?r1rrr2)rr8rranger(r))r9rnamessecsrr:rKrrsiblings siblingindentr/pointerbindentrsindentblevelrrrr%sx          rFc Csddt|D}d|}|ddd|Dd}|g}|D];}g}t||D]%\}} d| vr>ddd| dD} d|t| } || | q)||d|dq |rjt|dkrj|d ||||S) zAGenerate an RST table for the given table data as a list of linescSr)css|]}t|VqdSr'r rrrrr-rrz'maketable...rrrrrr0rrzmaketable..rAcss|]}d|VqdS)rNr)r+rrrrr-tzmaketable..r&css|]}|VqdSr')r)rrrrr-}rrri)rrr4rr r8r(r) datar/headerrrrrr,rrrrrr maketableos   rr')rNN)rrNrN)rF)3__doc__ __future__rri18nrrrrutilsrrrrrrrrr3r<rNrHrXrYrZrrarrhr{rrrrrrrrrrrrrrrrrrrrrrrrsn     <   2& +8   \ ]  8J