Pc@sWddlZddlmZddlmZddlZddgZyddlmZ Wn!e k r{ddl mZ nXy e Z Wnek reZ nXyeWnek reZnXdZedZd Zd Zd Zd Zd ZdZdZdZedZdZddkdYZddldYZde fdYZ!dZ"dZ#dZ$dZ%dZ&dZ'de fd YZ(d!e(fd"YZ)d#e(fd$YZ*e+d%Z,e+d&Z-ej.d'ej/ej0BZ1ej.d(ej/ej0BZ2ej.d)ej/ej0BZ3d*Z4ej.d+Z5d,Z6dmZ7dnZ8doZ9edYZ:dZZ;ej.d[Z<d\Z=d]Z>d^Z?d_Z@d`ZAdaZBedbZCdcZDddZEdeZFdfZGdgejHfdhYZIeJdikrSddjlmKZKeKjLndS(piN(tetree(tfragment_fromstringt html_annotatethtmldiff(tescapecCsdtt|d|fS(Ns%si(t html_escapet_unicode(ttexttversion((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pytdefault_markupscCsg|D]\}}t||^q}|d}x%|dD]}t|||}q=Wt|}t||}dj|jS(s doclist should be ordered from oldest to newest, like:: >>> version1 = 'Hello World' >>> version2 = 'Goodbye World' >>> print(html_annotate([(version1, 'version 1'), ... (version2, 'version 2')])) Goodbye World The documents must be *fragments* (str/UTF8 or unicode), not complete documents The markup argument is a function to markup the spans of words. This function is called like markup('Hello', 'version 2'), and returns HTML. The first argument is text and never includes any markup. The default uses a span with a title: >>> print(default_markup('Some Text', 'by Joe')) Some Text iit(ttokenize_annotatedthtml_annotate_merge_annotationstcompress_tokenstmarkup_serialize_tokenstjointstrip(tdoclisttmarkuptdocRt tokenlistt cur_tokensttokenstresult((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs%    cCs0t|dt}x|D]}||_qW|S(sFTokenize a document and add an annotation attribute to each token t include_hrefs(ttokenizetFalset annotation(RRRttok((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR Gs  c Cs{td|d|}|j}xS|D]K\}}}}}|dkr(|||!} |||!} t| | q(q(WdS(sMerge the annotations from tokens_old into tokens_new, when the tokens in the new document already existed in the old document. tatbtequalN(tInsensitiveSequenceMatchert get_opcodestcopy_annotations( t tokens_oldt tokens_newtstcommandstcommandti1ti2tj1tj2teq_oldteq_new((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR Os    cCsNt|t|kstx)t||D]\}}|j|_q.WdS(sN Copy annotations from the tokens listed in src to the tokens in dest N(tlentAssertionErrortzipR(tsrctdesttsrc_toktdest_tok((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR"\scCsq|dg}x]|dD]Q}|dj r\|j r\|dj|jkr\t||q|j|qW|S(sm Combine adjacent tokens when there is no HTML between the tokens, and they share an annotation iii(t post_tagstpre_tagsRtcompress_merge_backtappend(RRR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR ds  c Cs|d}t|tk s.t|tk r>|j|nit|}|jr`|d7}n||7}t|d|jd|jd|j}|j|_||d tag). Returns HTML with and tags added around the appropriate text. Markup is generally ignored, with the markup from new_html preserved, and possibly some markup from old_html (though it is considered acceptable to lose some of the old markup). Only the words in the HTML are diffed. The exception is tags, which are treated like words, and the href attribute of tags, which are noted inside the tag itself when there are changes. R (Rthtmldiff_tokensRRtfixup_ins_del_tags(told_htmltnew_htmltold_html_tokenstnew_html_tokensR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs   c Cstd|d|}|j}g}x|D]\}}}}} |dkru|jt||| !dtq.n|dks|dkrt||| !} t| |n|dks|dkr.t|||!} t| |q.q.Wt|}|S(s] Does a diff on the tokens themselves, returning a list of text chunks (not tokens). RRRtinserttreplacetdelete(R R!textendt expand_tokenstTruet merge_insertt merge_deletetcleanup_delete( t html1_tokenst html2_tokensR%R&RR'R(R)R*R+t ins_tokenst del_tokens((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRCs    ccsxz|D]r}x|jD] }|VqW| s7|j r`|jrR|jdVq`|jVnx|jD] }|VqjWqWdS(seGiven a list of tokens, return a generator of the chunks of text for the data in the tokens. R9N(R6thide_when_equalR:R?R5(RRR<RARB((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRMs   cCst|\}}}|j||rO|djd rO|dcd7ins_chunks to the end of that. iR9ss N(tsplit_unbalancedRLtendswithR8(t ins_chunksRtunbalanced_starttbalancedtunbalanced_end((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyROs    t DEL_STARTcBseZRS((t__name__t __module__(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR]stDEL_ENDcBseZRS((R^R_(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR`st NoDeletescBseZdZRS(sY Raised when the document no longer contains any pending deletes (DEL_START/DEL_END) (R^R_t__doc__(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRascCs+|jt|j||jtdS(s Adds the text chunks in del_chunks to the document doc (another list of text chunks) with marker to show it is a delete. cleanup_delete later resolves these markers into tags.N(R8R]RLR`(t del_chunksR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRPs  cCsxyt|\}}}Wntk r0PnXt|\}}}t|||t||||}|r|djd r|dcd7. To do this while keeping the document valid, it may need to drop some tags (either start or end tags). It may also move the del into adjacent tags to try to move it to a similar location where it was originally located (e.g., moving a delete into preceding
tag, if the del looks like (DEL_START, 'Text
', DEL_END)iR9ss (t split_deleteRaRWtlocate_unbalanced_starttlocate_unbalanced_endRXR8RL(tchunkst pre_deleteRKt post_deleteRZR[R\R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRQs&       c Csg}g}g}g}xE|D]=}|jdsG|j|qn|ddk}|jdjd}|tkr|j|qn|r3|r|dd|kr|j||j\}}} | ||/iN( t startswithR8tsplitRt empty_tagstpopRLR.tNone( Rgtstarttendt tag_stackR[tchunktendtagtnametposttag((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRW5s:      )#%cCs`y|jt}Wntk r,tnX|jt}|| ||d|!||dfS(s Returns (stuff_before_DEL_START, stuff_inside_DEL_START_END, stuff_after_DEL_END). Returns the first case found (there may be more DEL_STARTs in stuff_after_DEL_END). Raises NoDeletes if there's no DEL_START found. i(tindexR]t ValueErrorRaR`(RgRwtpos2((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRd]s   cCsx|s Pn|d}|jdjd}|s:Pn|d}|tks`|jd rdPn|ddkrxPn|jdjd}|dkrPn|dkstd|||kr|jd|j|jdqPqd S( s pre_delete and post_delete implicitly point to a place in the document (where the two were split). This moves that point (by popping items from one and pushing them onto the other). It moves the point to try to find a place where unbalanced_start applies. As an example:: >>> unbalanced_start = ['
'] >>> doc = ['

', 'Text', '

', '
', 'More Text', '
'] >>> pre, post = doc[:3], doc[3:] >>> pre, post (['

', 'Text', '

'], ['
', 'More Text', '
']) >>> locate_unbalanced_start(unbalanced_start, pre, post) >>> pre, post (['

', 'Text', '

', '
'], ['More Text', '
']) As you can see, we moved the point so that the dangling
that we found will be effectively replaced by the div in the original document. If this doesn't work out, we just throw away unbalanced_start without doing anything. is<>RjiRktinstdelsUnexpected delete tag: %rN(RmRR]RlR/RoR8(RZRhRitfindingt finding_nametnextRv((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyReis*      cCsx|s Pn|d}|jdjd}|s:Pn|d}|tks`|jd rdPn|jdjd}|dks|dkrPn||kr|j|jd|jqPqdS(st like locate_unbalanced_start, except handling end tags and possibly moving the point earlier in the document. iis<>/s tag, which takes up visible space just like a word but is only represented in a document by a tag. c CsMtj|dt|fd|d|d|}||_||_||_|S(Ns%s: %sR6R5R:(R<RR;Rxtdatat html_repr(RRxRRR6R5R:R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs    cCs,d|j|j|j|j|j|jfS(NsRtag_token(%s, %s, html_repr=%s, post_tags=%r, pre_tags=%r, trailing_whitespace=%s)(RxRRR6R5R:(R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRscCs|jS(N(R(R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR?sN(R^R_RbRpRRRR?(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs  t href_tokencBseZdZeZdZRS(sh Represents the href in an anchor tag. Unlike other words, we only show the href when it changes. cCsd|S(Ns Link: %s((R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR?s(R^R_RbRNRVR?(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRscCsLtj|r|}nt|dt}t|dtd|}t|S(sk Parse the given HTML and returns token objects (words with attached tags). This parses only the content of a page; anything in the head is ignored, and the and elements are themselves optional. The content is then parsed by lxml, which ensures the validity of the resulting parsed document (though lxml may make incorrect guesses when the markup is particular bad). and tags are also eliminated from the document, as that gets confusing. If include_hrefs is true, then the href attribute of tags is included as a special kind of diffable token.tcleanuptskip_tagR(Rt iselementt parse_htmlRNt flatten_elt fixup_chunks(R?Rtbody_elRg((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs  cCs%|rt|}nt|dtS(s Parses an HTML fragment, returning an lxml element. Note that the HTML will be wrapped in a
tag that was not in the original document. If cleanup is true, make sure there's no or , and get rid of any and tags. t create_parent(t cleanup_htmlRRN(R?R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRss s scCsftj|}|r(||j}ntj|}|rP||j }ntjd|}|S(s This 'cleans' the HTML, meaning that any page structure is removed (only the contents of are used, if there is any and tags are removed. R (t_body_retsearchRrt _end_body_reRqt _ins_del_retsub(R?tmatch((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR,ss [ \t\n\r]$c Csg}d }g}x|D]}t|tr|ddkr|d}|d}|jdrt|d }t}nt}td|d|d|d |}g}|j|q|dd kr|d}t|d|d t}g}|j|qqnt |ra|jdr-|d }t}nt}t |d|d |}g}|j|qt |r}|j|qt |r|r|j|q|st d ||||f|jj|qdst qW|st d d|gS|djj||S(sM This function takes a list of chunks and produces a list of tokens. itimgiiR9iRR6R:threfs4Weird state, cur_word=%r, result=%r, chunks=%r of %rR N(Rpt isinstancettupleRXRNRRR8Rtis_wordR<t is_start_tagt is_end_tagR/R5RL( Rgt tag_accumtcur_wordRRtR1RxR:R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR<sZ             tparamRtareatbrtbasefonttinputtbasetmetatlinktcoltaddresst blockquotetcentertdirtdivtdltfieldsettformth1th2th3th4th5th6thrtisindextmenutnoframestnoscripttoltpRAttabletultddtdttframesettlittbodyttdttfoottthttheadttrccsS|sC|jdkr5d|jdt|fVqCt|Vn|jtkrw|j rwt| rw|j rwdSt|j}x|D]}t|VqWx0|D](}xt |d|D] }|VqWqW|jdkr|jdr|rd|jdfVn|sOt |Vt|j}x|D]}t|Vq7WndS(s Takes an lxml element el, and generates all the text chunks for that tag. Each start tag is a chunk, each word is a chunk, and each end tag is a chunk. If skip_tag is true, then the outermost container tag is not returned (just its contents).RR1NRRR( Rxtgett start_tagRnRR.ttailt split_wordsRRtend_tag(telRRt start_wordstwordtchildtitemt end_words((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs& 0   $  cCsi| s|j rgSg|jjD]}|d^q+}tj|se|dd |dR s %s="%s"(RxRtattribtitemsRRN(RRvtvalue((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRscCs;|jr$tj|jr$d}nd}d|j|fS(sg The text representation of an end tag for a tag. Includes trailing whitespace when appropriate. R9R s%s(Rtstart_whitespace_reRRx(Rtextra((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs cCs|jd S(NRj(Rl(R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRscCs |jdS(Ns or tags inside of any block-level elements, e.g. transform

word

to

word

Rt skip_outer(RRt_fixup_ins_del_tagstserialize_html_fragmentRN(R?R((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRDs cCs}t|t s td|tj|dddt}|ru||jdd}||jd }|jS|SdS( s Serialize a single lxml element as HTML. The serialized form includes the elements tail. If skip_outer is true, then don't serialize the outermost tag s3You should pass in an element, not a string like %rtmethodR?tencodingt>iRjN( Rt basestringR/RttostringRtfindtrfindR(RRR?((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs  cCsex^ddgD]P}xG|jd|D]2}t|s?q'nt|d||jq'Wq WdS(s?fixup_ins_del_tags that works on an lxml document in-place R|R}sdescendant-or-self::%sRxN(txpatht_contains_block_level_tagt_move_el_inside_blocktdrop_tag(RRxR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs  cCsG|jtks|jtkr"tSx|D]}t|r)tSq)WtS(sPTrue if the element contains any block-level elements, like

, , etc. (Rxtblock_level_tagstblock_level_container_tagsRNRR(RR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR s   cCsbxo|D]}t|rPqqWddl}tj|}|j|_d|_|jt||g|(dSxt|D]}t|rt|||j rtj|}|j |_d|_ |j |j |d|qqtj|}|j |||j |qW|jr^tj|}|j|_d|_|j d|ndS(st helper for _fixup_ins_del_tags; actually takes the etc tags and moves them inside any block-level tags. iNii(RtsysRtElementRRpRLtlistRRRIRyRJR8(RRxRRt children_tagttail_tagt child_tagttext_tag((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRs4           #   cCs&|j}|jpd}|jryt|s@||j7}qy|djrf|dj|j7_qy|j|d_n|j|}|r |dkrd}n||d}|dkr|jr|j|7_q||_q |jr|j|7_q ||_n|j|||d+dS(s Removes an element, but merges its contents into its place, e.g., given

Hi there!

, if you remove the element you get

Hi there!

R iiiN(t getparentRRR.RyRpt getchildren(RtparentRRytprevious((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyt_merge_element_contents7s*           R cBseZdZdZdZRS(st Acts like SequenceMatcher, but tries not to find very small equal blocks amidst large spans of changes icCs{tt|jt|j}t|j|d}tjj|}g|D]'}|d|ksq|d rP|^qPS(Nii(tminR.Rt thresholdtdifflibtSequenceMatchertget_matching_blocks(RtsizeRtactualR((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyRas ! (R^R_RbRR(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyR Yst__main__(t _diffcommand((( RsimgRRRsinputsbaseRslinkR(RRscentersdirRRRRRRRRRRRRRRRRRsprestableR( RRRRRRRRRR(MRtlxmlRt lxml.htmlRtret__all__R?RRt ImportErrortcgitunicodeRt NameErrortstrRR RR R R"R R7RRRCRRMROR]R`t ExceptionRaRPRQRWRdReRfR<RRRNRRtcompiletItSRRRRRRRnRRRRRRRRRRRDRRRRRRR R^Rtmain(((s4/usr/lib64/python2.7/site-packages/lxml/html/diff.pyts           (       &    ' ( 2 (    <        ! "