JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3RbrJFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3RbrB |/\Yw@sddlmZddlZddlmZddlmZddlZddgZyddl m Z Wn e k rlddl m Z YnXyeZWnek reZYnXyeWnek reZYnXdd Zefd dZd d Zd dZddZddZddZddZddZddZdiddZddZGdd d ZGd!d"d"Z Gd#d$d$e!Z"d%d&Z#d'd(Z$d)d*Z%d+d,Z&d-d.Z'd/d0Z(Gd1d2d2eZ)Gd3d4d4e)Z*Gd5d6d6e)Z+djd8d9Z,dkd:d;Z-e.dej/ej0BZ3d?d@Z4e.dAZ5dBdCZ6dDdEZ7dFZ8dGZ9dHZ:dldIdJZ;e.dKej<Z=dLdMZ>e.dNZ?dOdPZ@dQdRZAdSdTZBdUdVZCdWdXZDdYdZZEdmd[d\ZFd]d^ZGd_d`ZHdadbZIdcddZJGdedfdfejKZLeMdgkrddhlmNZNeNOdS)n)absolute_importN)etree)fragment_fromstring html_annotatehtmldiff)escapecCsdtt|d|fS)Nz%s) html_escape_unicode)textversionr A/opt/alt/python37/lib64/python3.7/site-packages/lxml/html/diff.pydefault_markupsrcCsZdd|D}|d}x"|ddD]}t|||}q$Wt|}t||}d|S)a 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 cSsg|]\}}t||qSr )tokenize_annotated).0docr r r r =sz!html_annotate..rrN)html_annotate_merge_annotationscompress_tokensmarkup_serialize_tokensjoinstrip)ZdoclistZmarkup tokenlistZ cur_tokenstokensresultr r rr#s  cCs$t|dd}x|D] }||_qW|S)zFTokenize a document and add an annotation attribute to each token F) include_hrefs)tokenize annotation)rrrtokr r rrKs   rc CsZt||d}|}x@|D]8\}}}}}|dkr|||} |||} t| | qWdS)zMerge the annotations from tokens_old into tokens_new, when the tokens in the new document already existed in the old document. )abequalN)InsensitiveSequenceMatcher get_opcodescopy_annotations) Z tokens_oldZ tokens_newscommandscommandi1i2j1j2Zeq_oldZeq_newr r rrSs   rcCs8t|t|kstxt||D]\}}|j|_q WdS)zN Copy annotations from the tokens listed in src to the tokens in dest N)lenAssertionErrorzipr)srcdestZsrc_tokZdest_tokr r rr&`sr&cCsZ|dg}xJ|ddD]:}|djsH|jsH|dj|jkrHt||q||qW|S)zm Combine adjacent tokens when there is no HTML between the tokens, and they share an annotation rrN) post_tagspre_tagsrcompress_merge_backappend)rrr r r rrhs   rcCsv|d}t|tk s t|tk r,||nFt|}|jrD||j7}||7}t||j|j|jd}|j|_||d<dS)zY Merge tok into the last element of tokens (modifying the list of tokens in-place). r3)r5r4trailing_whitespaceN)typetokenr7r r8r5r4r)rr lastr Zmergedr r rr6ws   r6ccshxb|D]Z}x|jD] }|VqW|}|||j}|jrD||j7}|Vx|jD] }|VqRWqWdS)zz Serialize the list of tokens into a list of text chunks, calling markup_func around text to add annotations. N)r5htmlrr8r4)rZ markup_funcr:prer<postr r rrs      rcCs0t|}t|}t||}d|}t|S)a 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)rhtmldiff_tokensrrfixup_ins_del_tags)Zold_htmlZnew_htmlZold_html_tokensZnew_html_tokensrr r rrs  c Cst||d}|}g}x|D]\}}}}} |dkrP|t||| ddq|dks`|dkrzt||| } t| ||dks|dkrt|||} t| |qWt|}|S)z] Does a diff on the tokens themselves, returning a list of text chunks (not tokens). )r!r"r#T)r#insertreplacedelete)r$r%extend expand_tokens merge_insert merge_deletecleanup_delete) Z html1_tokensZ html2_tokensr'r(rr)r*r+r,r-Z ins_tokensZ del_tokensr r rr?s  r?Fccsjxd|D]\}x|jD] }|VqW|r*|jsL|jrB||jVn |Vx|jD] }|VqTWqWdS)zeGiven a list of tokens, return a generator of the chunks of text for the data in the tokens. N)r5hide_when_equalr8r<r4)rr#r:r=r>r r rrEs      rEcCst|\}}}|||r:|dds:|dd7<|d|rj|ddrj|ddd|d<|||d||dS)z| doc is the already-handled document (as a list of text chunks); here we add ins_chunks to the end of that. r3 zNz )split_unbalancedrDendswithr7)Z ins_chunksrunbalanced_startbalancedunbalanced_endr r rrFs    rFc@s eZdZdS) DEL_STARTN)__name__ __module__ __qualname__r r r rrPsrPc@s eZdZdS)DEL_ENDN)rQrRrSr r r rrTsrTc@seZdZdZdS) NoDeleteszY Raised when the document no longer contains any pending deletes (DEL_START/DEL_END) N)rQrRrS__doc__r r r rrUsrUcCs"|t|||tdS)z 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)r7rPrDrT)Z del_chunksrr r rrG s  rGcCsxyt|\}}}Wntk r*PYnXt|\}}}t|||t||||}|rx|ddsx|dd7<|d|r|ddr|ddd|d<|||d|||}qW|S)a Cleans up any DEL_START/DEL_END markers in the document, replacing them with . 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)r3rJzNz ) split_deleterUrKlocate_unbalanced_startlocate_unbalanced_endrLr7rD)chunks pre_deleterC post_deleterMrNrOrr r rrHs&       rHc Csg}g}g}g}x|D]}|ds0||q|ddk}|dd}|tkrb||q|r|r|dd|kr|||\}}} | ||<q|r|dd|Dg}||q||q||t||f|d qW|d d|Dd d|D}|||fS) a]Return (unbalanced_start, balanced, unbalanced_end), where each is a list of text and tag chunks. unbalanced_start is a list of all the tags that are opened, but not closed in this span. Similarly, unbalanced_end is a list of tags that are closed but were not opened. Extracting these might mean some reordering of the chunks./r3cSsg|]\}}}|qSr r )rnamepostagr r rrTsz$split_unbalanced..NcSsg|]\}}}|qSr r )rr_r`chunkr r rr]scSsg|]}|dk r|qS)Nr )rrbr r rr^s) startswithr7splitr empty_tagspoprDr.) rZstartendZ tag_stackrNrbZendtagr_r`rar r rrK9s:         rKcCs\y|t}Wntk r&tYnX|t}|d|||d|||ddfS)z 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. Nr)indexrP ValueErrorrUrT)rZr`pos2r r rrWas   rWcCsx|sP|d}|dd}|s(P|d}|tksB|dsDP|ddkrRP|dd}|dkrnP|dkstd|||kr|d||dqPqWd S) a 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. rz<>r]rr^insdelzUnexpected delete tag: %rN)rdrrPrcr/rfr7)rMr[r\finding finding_namenextr_r r rrXms*    rXcCsx|sP|d}|dd}|s(P|d}|tksB|dsDP|dd}|dksf|dkrhP||kr||d|qPqWdS)zt like locate_unbalanced_start, except handling end tags and possibly moving the point earlier in the document. r3rz<>/z tag, which takes up visible space just like a word but is only represented in a document by a tag. NrcCs2tj|dt|f|||d}||_||_||_|S)Nz%s: %s)r5r4r8)r:rqr9radata html_repr)rrrarwrxr5r4r8rsr r rrqsztag_token.__new__cCs d|j|j|j|j|j|jfS)NzRtag_token(%s, %s, html_repr=%s, post_tags=%r, pre_tags=%r, trailing_whitespace=%r))rarwrxr5r4r8)rur r rrtsztag_token.__repr__cCs|jS)N)rx)rur r rr<sztag_token.html)NNr)rQrRrSrVrqrtr<r r r rrvs  rvc@seZdZdZdZddZdS) href_tokenzh Represents the href in an anchor tag. Unlike other words, we only show the href when it changes. TcCsd|S)Nz Link: %sr )rur r rr<szhref_token.htmlN)rQrRrSrVrIr<r r r rrysryTcCs2t|r|}n t|dd}t|d|d}t|S)ak 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.T)cleanup)skip_tagr)rZ iselement parse_html flatten_el fixup_chunks)r<rZbody_elrZr r rrs   rcCs|r t|}t|ddS)a 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)Z create_parent) cleanup_htmlr)r<rzr r rr| sr|z z zcCsLt|}|r||d}t|}|r<|d|}td|}|S)z 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. Nr)_body_researchrh _end_body_rerg _ins_del_resub)r<matchr r rr1s   rz [ \t\n\r]$cCs$t|}|d|||dfS)zP This function takes a word, such as 'test ' and returns ('test',' ') rN)r.rstrip)wordZstripped_lengthr r rsplit_trailing_whitespaceAs rc CsXg}d}g}x |D]}t|tr|ddkrj|d}t|d\}}td||||d}g}||q|ddkr|d}t||dd }g}||qt|rt|\}}t|||d }g}||qt|r||qt |r$|r||n&|st d ||||f|j |qd st qW|sDtd |d gS|dj ||S)zM This function takes a list of chunks and produces a list of tokens. Nrimgr)rxr5r8hrefrJ)r5r8z4Weird state, cur_word=%r, result=%r, chunks=%r of %rFr)r5r3) isinstancetuplerrvr7ryis_wordr: is_start_tag is_end_tagr/r4rD) rZZ tag_accumZcur_wordrrbr1rar8rr r rr~IsL           r~) paramrZareabrZbasefontinputbasemetalinkcol)addressZ blockquotecenterdirZdivZdlZfieldsetZformZh1Zh2Zh3Zh4Zh5Zh6hrZisindexZmenuZnoframesZnoscriptZolpr=tableZul) dddtZframesetZliZtbodytdZtfootthZtheadtrccs|s0|jdkr&d|dt|fVn t|V|jtkrR|jsRt|sR|jsRdSt|j}x|D]}t|VqbWx(|D] }xt ||dD] }|VqWqzW|jdkr|dr|rd|dfV|st |Vt|j}x|D]}t|VqWdS)a 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).rr1N)rr!r) raget start_tagrer r.tail split_wordsr r}end_tag)elrr{Z start_wordsrchilditemZ end_wordsr r rr}s&        r}z \S+(?:\s+|$)cCs|r |sgSt|}|S)z_ Splits some text into words. Includes trailing whitespace on each word when appropriate. )rsplit_words_refindall)r wordsr r rrs  rz ^[ \t\n\r]cCs$d|jddd|jDfS)z= The text representation of the start tag for a tag. z<%s%s>rcSs"g|]\}}d|t|dfqS)z %s="%s"T)r )rr_valuer r rrszstart_tag..)rarZattribitems)rr r rrsrcCs*|jrt|jrd}nd}d|j|fS)zg The text representation of an end tag for a tag. Includes trailing whitespace when appropriate. rJrz%s)rstart_whitespace_rerra)rextrar r rrsrcCs |d S)Nr])rc)r r r rrsrcCs |dS)Nz or tags inside of any block-level elements, e.g. transform

word

to

word

F)rzT) skip_outer)r|_fixup_ins_del_tagsserialize_html_fragment)r<rr r rr@s  r@cCsbt|trtd|tj|dtd}|rZ||ddd}|d|d}|S|SdS)z 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 z3You should pass in an element, not a string like %rr<)methodencoding>rNr]) r basestringr/rtostringr findrfindr)rrr<r r rrs  rcCsHxBdD]:}x4|d|D]"}t|s(qt||d|qWqWdS)z?fixup_ins_del_tags that works on an lxml document in-place )rlrmzdescendant-or-self::%s)raN)Zxpath_contains_block_level_tag_move_el_inside_blockZdrop_tag)rrarr r rr s   rcCs6|jtks|jtkrdSx|D]}t|rdSqWdS)zPTrue if the element contains any block-level elements, like

, , etc. TF)rablock_level_tagsblock_level_container_tagsr)rrr r rrs  rcCsxN|D]}t|rPqWt|}|j|_d|_|t||g|dd<dSxxt|D]l}t|rt|||jrt|}|j|_d|_|| |d|qZt|}| ||| |qZW|jrt|}|j|_d|_|d|dS)zt helper for _fixup_ins_del_tags; actually takes the etc tags and moves them inside any block-level tags. Nrr) rrZElementr rDlistrrrArirBr7)rrarZ children_tagZtail_tagZ child_tagZtext_tagr r rrs2       rcCs|}|jpd}|jrXt|s,||j7}n,|djrL|dj|j7_n |j|d_||}|r|dkrtd}n ||d}|dkr|jr|j|7_q||_n|jr|j|7_n||_||||d<dS)z Removes an element, but merges its contents into its place, e.g., given

Hi there!

, if you remove the element you get

Hi there!

rr3rNr)Z getparentr rr.riZ getchildren)rparentr riZpreviousr r r_merge_element_contents?s*      rc@seZdZdZdZddZdS)r$zt Acts like SequenceMatcher, but tries not to find very small equal blocks amidst large spans of changes rcsDtt|jt|j}t|j|dtj|}fdd|DS)Ncs$g|]}|dks|ds|qS)rr )rr) thresholdr rrms zBInsensitiveSequenceMatcher.get_matching_blocks..)minr.r"rdifflibSequenceMatcherget_matching_blocks)rusizeactualr )rrris z.InsensitiveSequenceMatcher.get_matching_blocksN)rQrRrSrVrrr r r rr$asr$__main__) _diffcommand)F)T)T)F)F)P __future__rrlxmlrZ lxml.htmlrre__all__r<rr ImportErrorcgiunicoder NameErrorstrrrrrrr&rr6rrr?rErFrPrT ExceptionrUrGrHrKrWrXrYr:rvryrr|compileISrrrrZend_whitespace_rerr~rerrr}Urrrrrrrrr@rrrrrrr$rQrmainr r r rs      ( & '( 2)   7       "