3 \7@sdZddlmZddlmZddlmZddlmZddlZdd ZGd d d ej Z Gd d d e Z Gddde Z Gddde Z dS)z PRE-PROCESSORS ============================================================================= Preprocessors work on source text before we start doing anything too complicated. )absolute_import)unicode_literals)util)odictNcKs:tj}t||d<|jdkr*t||d<t||d<|S)z: Build the default set of preprocessors used by Markdown. Znormalize_whitespaceescapeZ html_block reference)r OrderedDictNormalizeWhitespacesafeModeHtmlBlockPreprocessorReferencePreprocessor)Z md_instancekwargs preprocessorsr#build/lib/markdown/preprocessors.pybuild_preprocessorss     rc@seZdZdZddZdS) PreprocessoraF Preprocessors are run after the text is broken into lines. Each preprocessor implements a "run" method that takes a pointer to a list of lines of the document, modifies it as necessary and returns either the same pointer or a pointer to a new list. Preprocessors must extend markdown.Preprocessor. cCsdS)z Each subclass of Preprocessor should override the `run` method, which takes the document as a list of strings split by newlines and returns the (possibly modified) list of lines. Nr)selflinesrrrrun%szPreprocessor.runN)__name__ __module__ __qualname____doc__rrrrrrs rc@seZdZdZddZdS)r z. Normalize whitespace for consistant parsing. cCs`dj|}|jtjdjtjd}|jddjddd}|j|jj}tj dd|}|j dS)N z  z z (?<=\n) +\n) joinreplacerSTXETX expandtabsmarkdown tab_lengthresubsplit)rrsourcerrrr2s  zNormalizeWhitespace.runN)rrrrrrrrrr /sr c@seZdZdZddgZdZdeZejeej Z ejeej Z dZ ddZ d d Zd d Zd dZddZddZddZddZdS)r zDRemove html blocks from the text and store them for later retrieval.zz%s>a \s+(?P[^>"'/= ]+)=(?P['"])(?P.*?)(?P=q) # attr="value" | # OR \s+(?P[^>"'/= ]+)=(?P[^> ]+) # attr=value | # OR \s+(?P[^>"'/= ]+) # attr z+^\<(?P[^> ]+)(?P(%s)*)\s*\/?\>?FcCs(|jj|}|r|jd}|jd}i}|rx|jj|D]}|jdr|jdrl|jd||jdj<qd||jdj<q:|jdr|jdr|jd||jdj<qd||jdj<q:|jdr:d||jdj<q:W|t|jd |fS|d djd d d j}|t|d ifSdS) NtagattrsattrvaluerZattr1Zvalue1Zattr2rr>) left_tag_rematchgroupattrs_refinditerstriplenr'lower)rblockmr)Z raw_attrsr*Zmarrr _get_left_tagKs(        z#HtmlBlockPreprocessor._get_left_tagcCsvxp|j||}|dkrdS|j||}||ks6|dkrB|t|S|jd|}|j|||d|}|dkrdSqWdS)Nrr-r:r:r:r:)findr5_recursive_tagfind)rltagZrtagZ start_indexr7ijrrrr<ds    z(HtmlBlockPreprocessor._recursive_tagfindcCsfxF|jD]<}||}|jd||||}|dkr|jdjd|fSqW|j| djt|fS)Nz<%sr.rrr_get_right_tagvs  z$HtmlBlockPreprocessor._get_right_tagcCs\|dd krdSd||kr dS|dkr4|dkr4dS||ddkrT|ddkrTdSd SdS) Nr?@%T/z--rF)rHrIrJr)rrD right_tagrrr _equal_tags~s   z!HtmlBlockPreprocessor._equal_tagscCs|dkS)Nhrhr/)rNrOr)rr)rrr _is_onelinersz"HtmlBlockPreprocessor._is_onelinercCs@|jdd\}}x$||kr6|t||7}|d7}qW|dS)z Same effect as concatenating the strings in items, finding the character to which stringindex refers in that string, and returning the index of the item in which that character resides. Zdummyrr)rr)appendr5)rZ stringindexitemsr>countrrr_stringindex_to_listindexs    z/HtmlBlockPreprocessor._stringindex_to_listindexc CsTxLt|D]>\}}|jj|r |jdj||d\}}}|j||dj||d\}}|j|||d|} d|jkr|||d||<|jj j |||d| d} |j || t || dkr| d8} || dt | d|| <q t || dkr| d8} |jj j dj||| d} ||| d=|j || q W|S)z@Find and process html child elements of the given element block.rNr#rr.z ) enumerater/r0r9rrGrTkeysr# htmlStash store_taginsertr5store) rrRr>itemrDrEr*rL data_indexZright_listindex placeholderrrr_nested_markdown_in_htmls,    z.HtmlBlockPreprocessor._nested_markdown_in_htmlcCsdj|}g}|jd}g}d}d}d}x|r|d}|jdrP|dd}|dd}|jdrr|dd}|sJ|jdot|jdkr>|ddd krd d i}} } n|j|\}} } |j|| |\}} | t|kotj|s|d kr|j d|| d|d| }tj|p.|ddks>|j |q,|j |rZ|j |jq,|j j dr|j||r|jrd| jkr|| t| d }|j |jjj|| dd |j|gq,|j |jjj|jq,nT|j|| r"tj|s|d kr"|j |jd}q,|j |jjj|jq,n |j |q,|j ||j|d|\}} |j||r,| t|kr|d| |d<|j d|| dd}|jrd| jkr|d| d|d<|ddt| d |d<|t|drt|d} n t|d } |j |jjj|| d| t|jjj} |j|j|t|jjj| }|jjjd|d|d 7<n|j |jjjdj|g}q,W|r|jrd| jkr|d| d|d<|ddt| d |d<|t|drt|d} n t|d } |j |jjj|| d| t|jjj} |j|j|t|jjj| }|jjjd|d|d 7<n|j |jjjdj||j ddj|}|jdS)Nrz rFrrr@z!--z--r.!rHrIrJr-r#T right_index)r`rHrIrJr:r:r:r:r:r:r:)rrsplit startswithr5r4r9rGr isBlockLevelrYrQrPrCendswithrMmarkdown_in_rawrVr#rWrXextendrZtag_datar^r')rrtextZ new_blocksrRrDrLZin_tagr7rEr*r\rbZplaceholderslenZnestsnew_textrrrrs                           zHtmlBlockPreprocessor.runN)rrrrrAZ attrs_patternZleft_tag_patternr%compileVERBOSEr2r/rgr9r<rGrMrPrTr^rrrrrr ;s  r c@s<eZdZdZdZejdeejZejdeZ ddZ dS)r zA Remove reference definitions from text and store for later use. z$[ ]*(\"(.*)\"|\'(.*)\'|\((.*)\))[ ]*z*^[ ]{0,3}\[([^\]]*)\]:\s*([^ ]*)[ ]*(%s)?$z^%s$c Csg}x|r|jd}|jj|}|r|jdjj}|jdjdjd}|jdph|jdph|jd}|s|jj|d}|r|jd|jdp|jd p|jd }||f|j j |<q|j |qW|S) Nrrr.r@r-rar_) popREr0r1r4r6rBrCTITLE_REr# referencesrQ) rrrkliner8idlinktZtmrrrrBs    zReferencePreprocessor.runN) rrrrZTITLEr%rlDOTALLrrrsrrrrrr ;s r )r __future__rrrrrr%r Processorrr r r rrrrs