Jc#@s=ddkZddklZddklZddkZddkZddgZy eZ Wne j o e Z nXye dZ Wnee fj o e Z nXdZedZd Zd Zd Zd Zd ZdZdZdZedZdZddkdYZddldYZdefdYZdZ dZ!dZ"dZ#dZ$dZ%de fd YZ&d!e&fd"YZ'd#e&fd$YZ(e)d%Z*e)d&Z+ei,d'ei-ei.BZ/ei,d(ei-ei.BZ0ei,d)ei-ei.BZ1d*Z2ei,d+Z3d,Z4dmZ5dnZ6doZ7edYZ8dZZ9ei,d[Z:d\Z;d]Z<d^Z=d_Z>d`Z?daZ@edbZAdcZBddZCdeZDdfZEdgeiFfdhYZGeHdijoddjklIZIeIiJndS(piN(tetree(tfragment_fromstringt html_annotatethtmldifft basestringcCs dtit|d|fS(Ns%si(tcgitescapet_unicode(ttexttversion((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pytdefault_markupsc Csg}|D]\}}|t||q ~}|d}x%|dD]}t|||}qEWt|}t||}di|iS(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( tdoclisttmarkupt_[1]tdocR t tokenlistt cur_tokensttokenstresult((s4/usr/lib64/python2.6/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.6/site-packages/lxml/html/diff.pyR Ds  c Cs}td|d|}|i}xU|D]M\}}}}}|djo+|||!} |||!} 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.6/site-packages/lxml/html/diff.pyR Ls    cCsPt|t|jptx)t||D]\}}|i|_q0WdS(sN Copy annotations from the tokens listed in src to the tokens in dest N(tlentAssertionErrortzipR(tsrctdesttsrc_toktdest_tok((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR$Ys  cCsu|dg}xa|dD]U}|di o3|i o(|di|ijot||q|i|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.6/site-packages/lxml/html/diff.pyRas   cCs|d}t|tj pt|tj o|i|nlt|}|io|d7}n||7}t|d|id|id|i}|i|_||dtpreRAtpost((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs     cCsFt|}t|}t||}di|i}t|S(s Do a diff of the old and new document. The documents are HTML *fragments* (str/UTF8 or unicode), they are not complete documents (i.e., no 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.6/site-packages/lxml/html/diff.pyRs   c Cstd|d|}|i}g}x|D]\}}}}} |djo'|it||| !dtq.n|djp |djo$t||| !} t| |n|djp |djo$t|||!} t| |q.q.Wt|}|S(s] Does a diff on the tokens themselves, returning a list of text chunks (not tokens). RR R!tinserttreplacetdelete(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.6/site-packages/lxml/html/diff.pyREs     ccsx|D]w}x|iD] }|VqW| p |i o,|io|idVqe|iVnx|iD] }|VqoWqWdS(seGiven a list of tokens, return a generator of the chunks of text for the data in the tokens. R;N(R8thide_when_equalR<RAR7(RR!R>RCRD((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyROs    cCst|\}}}|i||o)|did o|dcd7ins_chunks to the end of that. iR;ss N(tsplit_unbalancedRNtendswithR:(t ins_chunksRtunbalanced_starttbalancedtunbalanced_end((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRQs    t DEL_STARTcBseZRS((t__name__t __module__(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR_stDEL_ENDcBseZRS((R`Ra(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRbst NoDeletescBseZdZRS(sY Raised when the document no longer contains any pending deletes (DEL_START/DEL_END) (R`Rat__doc__(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRcscCs+|it|i||itdS(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(R:R_RNRb(t del_chunksR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRRs  cCsx yt|\}}}Wntj oPnXt|\}}}t|||t||||}|o)|did o|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)iR;ss (t split_deleteRcRYtlocate_unbalanced_starttlocate_unbalanced_endRZR:RN(tchunkst pre_deleteRMt post_deleteR\R]R^R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRS s&      c Csg}g}g}g}xX|D]P}|idp|i|qn|ddj}|idid}|tjo|i|qn|o|oE|dd|jo0|i||i\}}} | ||/iN( t startswithR:tsplitRt empty_tagstpopRNR0tNone( Ritstarttendt tag_stackR]tchunktendtagtnametposttagRt_[2]t_[3]((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRY2s<    1+2c Csby|it}Wntj o tnX|it}|| ||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 ValueErrorRcRb(RiRytpos2((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRfZs  cCsx|pPn|d}|idid}|pPn|d}|tjp|id oPn|ddjoPn|idid}|djoPn|djptd|||jo'|id|i|idqPqd 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<>RliRmtinstdelsUnexpected delete tag: %rN(RoRR_RnR1RqR:(R\RjRktfindingt finding_nametnextRx((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRgfs*      cCsx|pPn|d}|idid}|pPn|d}|tjp|id oPn|idid}|djp |djoPn||jo$|i|id|iqPqdS(st like locate_unbalanced_start, except handling end tags and possibly moving the point earlier in the document. iis<>/scBs8eZdZeZddedZdZdZRS(s8 Represents a diffable token, generally a word that is displayed to the user. Opening tags are attached to this token when they are adjacent (pre_tags) and closing tags that follow the word (post_tags). Some exceptions occur when there are empty tags adjacent to a word, so there may be close tags in pre_tags, or open tags in post_tags. We also keep track of whether the word was originally followed by whitespace, even though we do not want to treat the word as equivalent to a similar word that does not have a trailing space.cCseti||}|dj o ||_n g|_|dj o ||_n g|_||_|S(N(Rt__new__RrR8R7R<(tclsRR8R7R<tobj((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs       cCs dti||i|ifS(Nstoken(%s, %r, %r)(Rt__repr__R8R7(tself((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRscCs t|S(N(R(R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRAsN( R`RaRdRRXRrRRRA(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR>s   t tag_tokencBs2eZdZddedZdZdZRS(s Represents a token that is actually a tag. Currently this is just the tag, which takes up visible space just like a word but is only represented in a document by a tag. c CsMti|dt|fd|d|d|}||_||_||_|S(Ns%s: %sR8R7R<(R>RR=Rztdatat html_repr(RRzRRR8R7R<R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs    cCs,d|i|i|i|i|i|ifS(NsRtag_token(%s, %s, html_repr=%s, post_tags=%r, pre_tags=%r, trailing_whitespace=%s)(RzRRR8R7R<(R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRscCs|iS(N(R(R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRAsN(R`RaRdRrRRRRA(((s4/usr/lib64/python2.6/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.6/site-packages/lxml/html/diff.pyRAs(R`RaRdRPRXRA(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRscCsNti|o |}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_htmlRPt flatten_elt fixup_chunks(RARtbody_elRi((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs  cCs'|ot|}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_htmlRRP(RAR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRss s scCsjti|}|o||i}nti|}|o||i }ntid|}|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_retsearchRtt _end_body_reRst _ins_del_retsub(RAtmatch((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR)ss [ \t\n\r]$c Cs*g}d }g}x|D]}t|to|ddjov|d}|d}|ido|d }t}nt}td|d|d|d |}g}|i|q|dd jo9|d}t|d|d t}g}|i|qqnt |oY|ido|d }t}nt}t |d|d |}g}|i|qt |o|i|qt |oM|o|i|q|pt d ||||f|ii|qdpt qW|pt d d|gS|dii||S(sM This function takes a list of chunks and produces a list of tokens. itimgiiR;iRR8R<threfs4Weird state, cur_word=%r, result=%r, chunks=%r of %rR N(Rrt isinstancettupleRZRPRRR:Rtis_wordR>t is_start_tagt is_end_tagR1R7RN( Rit tag_accumtcur_wordRRvR3RzR<R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR9s\            tparamRtareatbrtbasefonttinputtbasetmetatlinktcoltaddresst blockquotetcentertdirtdivtdltfieldsettformth1th2th3th4th5th6thrtisindextmenutnoframestnoscripttoltpRCttabletultddtdttframesettlittbodyttdttfoottthttheadttrccsg|p>|idjod|idt|fVqEt|Vn|itjo)|i ot| o|i odSt|i}x|D]}ti |VqWx0|D](}xt |d|D] }|VqWqW|idjo0|ii do|od|idfVn|p=t |Vt|i}x |D]}ti |VqGWndS(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).RR3NRRR( Rztattribt start_tagRpRR0ttailt split_wordsRRRtgettend_tag(telRRt start_wordstwordtchildtitemt end_words((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs.4 * cCsv| p|i ogSg}|iiD]}||dq2~}ti|p|dd |dR s %s="%s"(RzRRtitemsRRRP(RRRxtvalue((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRscCs>|ioti|io d}nd}d|i|fS(sg The text representation of an end tag for a tag. Includes trailing whitespace when appropriate. R;R s%s(Rtstart_whitespace_reRRz(Rtextra((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs cCs|id S(NRl(Rn(R((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRscCs |idS(Ns or tags inside of any block-level elements, e.g. transform

word

to

word

Rt skip_outer(RRt_fixup_ins_del_tagstserialize_html_fragmentRP(RAR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRFs cCs~t|t ptd|ti|dddt}|o5||idd}||id }|iS|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 %rtmethodRAtencodingt>iRlN( RRR1RttostringRtfindtrfindR(RRRA((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs  cCsgx`ddgD]R}xI|id|D]4}t|pq'nt|d||iq'Wq WdS(s?fixup_ins_del_tags that works on an lxml document in-place RRsdescendant-or-self::%sRzN(txpatht_contains_block_level_tagt_move_el_inside_blocktdrop_tag(RRzR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs  cCsL|itjp|itjotSx |D]}t|otSq,WtS(sPTrue if the element contains any block-level elements, like

, , etc. (Rztblock_level_tagstblock_level_container_tagsRPRR(RR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR s   cCsjxq|D]}t|oPqqWddk}ti|}|i|_d|_|it||g|(dSxt|D]}t|o`t|||i oEti|}|i |_d|_ |i |i |d|q qti|}|i |||i |qW|io8ti|}|i|_d|_|i d|ndS(st helper for _fixup_ins_del_tags; actually takes the etc tags and moves them inside any block-level tags. iNii(RtsysRtElementRRrRNtlistRRRKR}RLR:(RRzRRt children_tagttail_tagt child_tagttext_tag((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyRs8            %   cCs7|i}|ipd}|ioZt|p||i7}q|dio|di|i7_q|i|d_n|i|}|o|djo d}n||d}|djo*|io|i|7_q||_q|io|i|7_q||_n|i|||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 getparentRRR0R}Rrt getchildren(RtparentRR}tprevious((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyt_merge_element_contents4s*          R"cBseZdZdZdZRS(st Acts like SequenceMatcher, but tries not to find very small equal blocks amidst large spans of changes icCstt|it|i}t|i|d}tii|}g}|D].}|d|jp |d o ||qTqT~S(Nii(tminR0R t thresholdtdifflibtSequenceMatchertget_matching_blocks(RtsizeRtactualRR((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR^s !(R`RaRdRR(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyR"Vst__main__(t _diffcommand((( RsimgRRRsinputsbaseRslinkR(RRscentersdirRRRRRRRRRRRRRRRRRspreRR( RRRRRRRRRR(KRtlxmlRt lxml.htmlRRtret__all__tunicodeRt NameErrortstrt __builtins__RtKeyErrorR RR R R$RR9RRRERRORQR_Rbt ExceptionRcRRRSRYRfRgRhR>RRRPRRtcompiletItSRRRRRRRpRRRRRRRRRRRFRRRRRRR"R`Rtmain(((s4/usr/lib64/python2.6/site-packages/lxml/html/diff.pyts         (       &    ' ( 2 (    <        ! "