\52@sddlmZddlmZddlmZddlmZddlmZddZd d ZGd d d ej Z Gd dde Z Gddde Z dS))unicode_literals)absolute_import)util)odict)inlinepatternscKs0tj}t||d.linkTextrrFNrD)findr !_InlineProcessor__findPlaceholderr(getrr>r<strip$_InlineProcessor__processElementTextr;appendr"rrr) rr0rErGZ strartIndexr1r+Z phEndIndexr4r;childr/r)rErFrZ__processPlaceholderssD             z%InlineProcessor.__processPlaceholdersc Csu|jj||d}|d|}|sB|ddfS|j|}|dkr|dt||jt|jdfSt|s-t|jt j s-xy|gt |D]a}t|s|jr|j |j|d|_n|j r#|j |j ||_ q#qqWq-n|j||j} d||jd| |jdfddfS)a Check if the line fits the pattern, create the necessary elements, add it to stashed_nodes. Keyword arguments: * data: the text to be processed * pattern: the pattern to be checked * patternIndex: index of current pattern * startIndex: string index, from which we start searching Returns: String with placeholders instead of ElementTree elements. NFrTrz%s%s%s%srD)ZgetCompiledRegExpmatchZ handleMatchr"spangroupsrrr;rrr>_InlineProcessor__handleInliner<_InlineProcessor__stashNoder*r.) rpatternr0r9r:rPZleftDatar4rOr5rrrZ__applyPatterns*  0       zInlineProcessor.__applyPatterncCsti|_|g}x[|ro|j}g}x=|D]5}|jrt|jtj r|j}d|_|j|j||}||7}|j||fn|j rM|j|j }tj j d} |j|| } | jr| j|_ n d|_ t |j |d} | jx!| D]} |j| | q0Wnt|r4|j|q4q4Wx|D]\} }|jjr| jrt| jrtj| j| | _qnd}x|D]} |jjrJ| j rt| j rtj| j | | _ n| jrJt| jrJtj| j| | _qJn| j|| |d7}qWqtWqW|S)a-Apply inline patterns to a parsed Markdown tree. Iterate over ElementTree, find elements with inline tag, apply inline patterns and append newly created Elements to tree. If you don't want to process your data with inline paterns, instead of normal string, use subclass AtomicString: node.text = markdown.AtomicString("This will not be processed.") Arguments: * tree: ElementTree object, representing Markdown tree. Returns: ElementTree object with applied inline patterns. Ndrr)r(popr;rrrr=rSrNr<etreeElementr>r1r@rAr"r%enable_attributesrrZhandleAttributes)rtreestackZ currElementZ insertQueuerOr;lstr<ZdumbyZ tailResultrBrCelementirrrrsV                     zInlineProcessor.runN) rrrrr'r3rJrTrSrMr=r7rrrrrr ,s     ! D,r c@s.eZdZdZddZddZdS)r z& Add linebreaks to the html document. cCsd}tj|jr|jdkr|j sA|jj rot|rotj|djro||_nx0|D](}tj|jrv|j|qvqvW|j s|jj r||_qn|j s|jj r||_ndS)z5 Recursively add linebreaks to ElementTree children.  codeprerN)zcodezpre)r isBlockLeveltagr;rLr"_prettifyETreer<)relemr_errrreHs!"  z$PrettifyTreeprocessor._prettifyETreecCs|j||jd}xD|D]<}|j sC|jj rOd|_q#d|j|_q#W|jd}xN|D]F}t|ry|djdkry|djjd|d_qyqyWdS)z, Add linebreaks to ElementTree root object. brr`z %srbrraN)re getiteratorr<rLr"rdr;rstrip)rrZbrsrhZpresrbrrrrXs    zPrettifyTreeprocessor.runN)rrrrrerrrrrr Es  r N) __future__rrrHrrrrr Processorrr r rrrrs