????
Your IP : 3.133.91.171
B
�|/\Yw�@s�ddlmZddlZddlmZddlmZddlZddgZyddl m
ZWn ek
rlddl
m
ZYnXyeZWnek
r�eZYnXyeWnek
r�eZYnXdd �Zefd
d�Zdd�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdidd�Zdd�ZGdd �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/d0�Z(Gd1d2�d2e�Z)Gd3d4�d4e)�Z*Gd5d6�d6e)�Z+djd8d9�Z,dkd:d;�Z-e�.d<ej/ej0B�Z1e�.d=ej/ej0B�Z2e�.d>ej/ej0B�Z3d?d@�Z4e�.dA�Z5dBdC�Z6dDdE�Z7dFZ8dGZ9dHZ:dldIdJ�Z;e�.dKej<�Z=dLdM�Z>e�.dN�Z?dOdP�Z@dQdR�ZAdSdT�ZBdUdV�ZCdWdX�ZDdYdZ�ZEdmd[d\�ZFd]d^�ZGd_d`�ZHdadb�ZIdcdd�ZJGdedf�dfejK�ZLeMdgk�r�ddhlmNZNeN�O�dS)n�)�absolute_importN)�etree)�fragment_fromstring�
html_annotate�htmldiff)�escapecCsdtt|�d�|fS)Nz<span title="%s">%s</span>�)�html_escape�_unicode)�text�version�r
�A/opt/alt/python37/lib64/python3.7/site-packages/lxml/html/diff.py�default_markupsrcCsZdd�|D�}|d}x"|dd�D]}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')]))
<span title="version 2">Goodbye</span> <span title="version 1">World</span>
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'))
<span title="by Joe">Some Text</span>
cSsg|]\}}t||��qSr
)�tokenize_annotated)�.0�docrr
r
r�
<listcomp>=sz!html_annotate.<locals>.<listcomp>rrN�)�html_annotate_merge_annotations�compress_tokens�markup_serialize_tokens�join�strip)ZdoclistZmarkupZ tokenlistZ
cur_tokens�tokens�resultr
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)rrr�tokr
r
rrKs
rcCsZt||d�}|��}x@|D]8\}}}}}|dkr|||�} |||�}
t| |
�qWdS)z�Merge the annotations from tokens_old into tokens_new, when the
tokens in the new document already existed in the old document.
)�a�b�equalN)�InsensitiveSequenceMatcher�get_opcodes�copy_annotations)Z
tokens_oldZ
tokens_new�s�commands�command�i1�i2�j1�j2Zeq_oldZeq_newr
r
rrSsrcCs8t|�t|�kst�xt||�D]\}}|j|_q WdS)zN
Copy annotations from the tokens listed in src to the tokens in dest
N)�len�AssertionError�zipr)�src�destZsrc_tokZdest_tokr
r
rr%`sr%cCsZ|dg}xJ|dd�D]:}|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_tags�pre_tagsr�compress_merge_back�append)rrrr
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). r2)r4r3�trailing_whitespaceN)�type�tokenr6r
r7r4r3r)rrZlastrZmergedr
r
rr5ws
r5ccshxb|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)r4�htmlrr7r3)rZmarkup_funcr9�prer:�postr
r
rr�s
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 <html> tag).
Returns HTML with <ins> and <del> 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 <img> tags, which
are treated like words, and the href attribute of <a> tags, which
are noted inside the tag itself when there are changes.
r)r�htmldiff_tokensrr�fixup_ins_del_tags)Zold_htmlZnew_htmlZold_html_tokensZnew_html_tokensrr
r
rr�s
cCs�t||d�}|��}g}x�|D]�\}}}}} |dkrP|�t||| �dd��q|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"�insert�replace�delete)r#r$�extend�
expand_tokens�merge_insert�merge_delete�cleanup_delete)Zhtml1_tokensZhtml2_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)r4�hide_when_equalr7r:r3)rr"r9r;r<r
r
rrC�s
rCcCs�t|�\}}}|�|�|r:|d�d�s:|dd7<|�d�|rj|d�d�rj|ddd�|d<|�|�|�d�|�|�dS)z| doc is the already-handled document (as a list of text chunks);
here we add <ins>ins_chunks</ins> to the end of that. r2� z<ins>Nz</ins> )�split_unbalancedrB�endswithr6)Z
ins_chunksr�unbalanced_start�balanced�unbalanced_endr
r
rrD�s
rDc@seZdZdS)� DEL_STARTN)�__name__�
__module__�__qualname__r
r
r
rrNsrNc@seZdZdS)�DEL_ENDN)rOrPrQr
r
r
rrRsrRc@seZdZdZdS)� NoDeleteszY Raised when the document no longer contains any pending deletes
(DEL_START/DEL_END) N)rOrPrQ�__doc__r
r
r
rrSsrScCs"|�t�|�|�|�t�dS)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 <del> tags.N)r6rNrBrR)Z
del_chunksrr
r
rrE
s
rEcCs�x�yt|�\}}}Wntk
r*PYnXt|�\}}}t|||�t|||�|}|rx|d�d�sx|dd7<|�d�|r�|d�d�r�|ddd�|d<|�|�|�d�|�|�|}qW|S)a� Cleans up any DEL_START/DEL_END markers in the document, replacing
them with <del></del>. 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 <div> tag, if the del looks like (DEL_START,
'Text</div>', DEL_END)r2rHz<del>Nz</del> )�split_deleterSrI�locate_unbalanced_start�locate_unbalanced_endrJr6rB)�chunks�
pre_deleterA�post_deleterKrLrMrr
r
rrFs&
rFc
Csg}g}g}g}x�|D]�}|�d�s0|�|�q|ddk}|��d�d�}|tkrb|�|�q|r�|r�|dd|kr�|�|�|��\}}} | ||<q�|r�|�dd�|D��g}|�|�q�|�|�q|�|t|�|f�|�d �qW|�d
d�|D��dd�|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.�<r�/rz<>/r2cSsg|]\}}}|�qSr
r
)r�name�pos�tagr
r
rrTsz$split_unbalanced.<locals>.<listcomp>NcSsg|]\}}}|�qSr
r
)rr]r^�chunkr
r
rr]scSsg|]}|dk r|�qS)Nr
)rr`r
r
rr^s)�
startswithr6�splitr�
empty_tags�poprBr-)
rX�start�endZ tag_stackrLr`Zendtagr]r^r_r
r
rrI9s:
rIcCs\y|�t�}Wntk
r&t�YnX|�t�}|d|�||d|�||dd�fS)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)�indexrN�
ValueErrorrSrR)rXr^Zpos2r
r
rrUas
rUcCs�x�|sP|d}|��d�d�}|s(P|d}|tksB|�d�sDP|ddkrRP|��d�d�}|dkrnP|dks�td|��||kr�|�d�|�|�d��qPqWd 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 = ['<div>']
>>> doc = ['<p>', 'Text', '</p>', '<div>', 'More Text', '</div>']
>>> pre, post = doc[:3], doc[3:]
>>> pre, post
(['<p>', 'Text', '</p>'], ['<div>', 'More Text', '</div>'])
>>> locate_unbalanced_start(unbalanced_start, pre, post)
>>> pre, post
(['<p>', 'Text', '</p>', '<div>'], ['More Text', '</div>'])
As you can see, we moved the point so that the dangling <div> 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\�ins�delzUnexpected delete tag: %rN)rbrrNrar.rdr6)rKrYrZ�finding�finding_name�nextr]r
r
rrVms*
rVcCs�x�|sP|d}|��d�d�}|s(P|d}|tksB|�d�sDP|��d�d�}|dksf|dkrhP||kr�|��|�d|���qPqWdS)zt like locate_unbalanced_start, except handling end tags and
possibly moving the point earlier in the document. r2rz<>/z</rirjN)rbrrRrardr?)rMrYrZrkrlrmr]r
r
rrW�s"rWc@s.eZdZdZdZddd�Zdd�Zd d
�ZdS)r9a8 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.FNrcCsBt�||�}|dk r||_ng|_|dk r2||_ng|_||_|S)N)r
�__new__r4r3r7)�clsrr4r3r7�objr
r
rrn�sz
token.__new__cCsdt�|�|j|j|jfS)Nztoken(%s, %r, %r, %r))r
�__repr__r4r3r7)�selfr
r
rrq�sztoken.__repr__cCst|�S)N)r
)rrr
r
rr:�sz
token.html)NNr)rOrPrQrTrGrnrqr:r
r
r
rr9�s
r9c@s*eZdZdZd
dd�Zdd�Zdd �ZdS)� tag_tokenz� Represents a token that is actually a tag. Currently this is just
the <img> 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)r4r3r7)r9rnr8r_�data� html_repr)ror_rtrur4r3r7rpr
r
rrn�sztag_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))r_rtrur4r3r7)rrr
r
rrq�sztag_token.__repr__cCs|jS)N)ru)rrr
r
rr:�sztag_token.html)NNr)rOrPrQrTrnrqr:r
r
r
rrs�s
rsc@seZdZdZdZdd�ZdS)�
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
)rrr
r
rr:szhref_token.htmlN)rOrPrQrTrGr:r
r
r
rrv�srvTcCs2t�|�r|}nt|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 <head> and <body> 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).
<ins> and <del> tags are also eliminated from the document, as
that gets confusing.
If include_hrefs is true, then the href attribute of <a> tags is
included as a special kind of diffable token.T)�cleanup)�skip_tagr)rZ iselement�
parse_html�
flatten_el�fixup_chunks)r:rZbody_elrXr
r
rrs
rcCs|rt|�}t|dd�S)a
Parses an HTML fragment, returning an lxml element. Note that the HTML will be
wrapped in a <div> tag that was not in the original document.
If cleanup is true, make sure there's no <head> or <body>, and get
rid of any <ins> and <del> tags.
T)Z
create_parent)�cleanup_htmlr)r:rwr
r
rry sryz <body.*?>z
</body.*?>z</?(ins|del).*?>cCsLt�|�}|r||��d�}t�|�}|r<|d|���}t�d|�}|S)z� This 'cleans' the HTML, meaning that any page structure is removed
(only the contents of <body> are used, if there is any <body).
Also <ins> and <del> tags are removed. Nr)�_body_re�searchrf�_end_body_rere�_ins_del_re�sub)r:�matchr
r
rr|1s
r|z
[ \t\n\r]$cCs$t|���}|d|�||d�fS)zP
This function takes a word, such as 'test
' and returns ('test','
')
rN)r-�rstrip)�wordZstripped_lengthr
r
r�split_trailing_whitespaceAsr�c CsXg}d}g}�x |D�]}t|t�r�|ddkrj|d}t|d�\}}td||||d�}g}|�|�q|ddkr|d}t||dd �}g}|�|�qt|�r�t|�\}}t|||d �}g}|�|�qt|�r�|�|�qt |��r$|r�|�|�n&|�st
d
||||f��|j�|�qdst
�qW|�sDtd|d
�gS|dj�|�|S)zM
This function takes a list of chunks and produces a list of tokens.
Nr�imgr�)rur4r7�hrefrH)r4r7z4Weird state, cur_word=%r, result=%r, chunks=%r of %rFr)r4r2)
�
isinstance�tupler�rsr6rv�is_wordr9�is_start_tag�
is_end_tagr.r3rB) rXZ tag_accumZcur_wordrr`r0r_r7r�r
r
rr{IsL
r{)
Zparamr�Zarea�brZbasefont�input�base�meta�link�col)ZaddressZ
blockquote�center�dirZdivZdlZfieldsetZformZh1Zh2Zh3Zh4Zh5Zh6ZhrZisindexZmenuZnoframesZnoscriptZol�pr;�tableZul)
ZddZdtZframesetZliZtbodyZtdZtfootZthZtheadZtrccs�|s0|jdkr&d|�d�t|�fVn
t|�V|jtkrR|jsRt|�sR|jsRdSt|j�}x|D]}t|�VqbWx(|D] }xt ||d�D]
}|Vq�WqzW|jdkr�|�d�r�|r�d|�d�fV|s�t
|�Vt|j�}x|D]}t|�Vq�WdS)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).r�r0N)rr r�)r_�get� start_tagrcrr-�tail�split_wordsr rz�end_tag)�elrrxZstart_wordsr��child�itemZ end_wordsr
r
rrz�s&
rzz\S+(?:\s+|$)cCs|r|��sgSt�|�}|S)z_ Splits some text into words. Includes trailing whitespace
on each word when appropriate. )r�split_words_re�findall)r�wordsr
r
rr��s
r�z
^[ \t\n\r]cCs$d|jd�dd�|j��D��fS)z=
The text representation of the start tag for a tag.
z<%s%s>rcSs"g|]\}}d|t|d�f�qS)z %s="%s"T)r )rr]�valuer
r
rr�szstart_tag.<locals>.<listcomp>)r_rZattrib�items)r�r
r
rr��sr�cCs*|jrt�|j�rd}nd}d|j|fS)zg The text representation of an end tag for a tag. Includes
trailing whitespace when appropriate. rHrz</%s>%s)r��start_whitespace_rer~r_)r�Zextrar
r
rr��sr�cCs|�d�S)Nr[)ra)rr
r
rr��sr�cCs
|�d�S)Nz</)ra)rr
r
rr��sr�cCs|�d�o|�d�S)Nr[z</)ra)rr
r
rr��sr�cCs$t|dd�}t|�t|dd�}|S)z� Given an html string, move any <ins> or <del> tags inside of any
block-level elements, e.g. transform <ins><p>word</p></ins> to
<p><ins>word</ins></p> F)rwT)�
skip_outer)ry�_fixup_ins_del_tags�serialize_html_fragment)r:rr
r
rr>�sr>cCsbt|t�rtd|��tj|dtd�}|rZ||�d�dd�}|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:)�method�encoding�>rNr[) r��
basestringr.rZtostringr
�find�rfindr)r�r�r:r
r
rr��s
r�cCsHxBdD]:}x4|�d|�D]"}t|�s(qt||d�|��qWqWdS)z?fixup_ins_del_tags that works on an lxml document in-place
)rirjzdescendant-or-self::%s)r_N)Zxpath�_contains_block_level_tag�_move_el_inside_blockZdrop_tag)rr_r�r
r
rr�
s
r�cCs6|jtks|jtkrdSx|D]}t|�rdSqWdS)zPTrue if the element contains any block-level elements, like <p>, <td>, etc.
TF)r_�block_level_tags�block_level_container_tagsr�)r�r�r
r
rr�s
r�cCs�xN|D]}t|�rPqWt�|�}|j|_d|_|�t|��|g|dd�<dSxxt|�D]l}t|�r�t||�|jr�t�|�}|j|_d|_|�|� |�d|�qZt�|�}|�
||�|�|�qZW|jr�t�|�}|j|_d|_|�d|�dS)zt helper for _fixup_ins_del_tags; actually takes the <ins> etc tags
and moves them inside any block-level tags. Nrr)r�rZElementrrB�listr�r�r?rgr@r6)r�r_r�Zchildren_tagZtail_tagZ child_tagZtext_tagr
r
rr�s2
r�cCs�|��}|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 <p>Hi <i>there!</i></p>, if you remove the <i> element you get
<p>Hi there!</p>
rr2rNr)Z getparentrr�r-rgZgetchildren)r��parentrrgZpreviousr
r
r�_merge_element_contents?s*
r�c@seZdZdZdZdd�ZdS)r#zt
Acts like SequenceMatcher, but tries not to find very small equal
blocks amidst large spans of changes
r�csDtt|j�t|j��}t|j|d��tj�|�}�fdd�|D�S)N�cs$g|]}|d�ks|ds|�qS)r�r
)rr�)� thresholdr
rrmszBInsensitiveSequenceMatcher.get_matching_blocks.<locals>.<listcomp>)�minr-r!r��difflib�SequenceMatcher�get_matching_blocks)rr�sizeZactualr
)r�rr�isz.InsensitiveSequenceMatcher.get_matching_blocksN)rOrPrQrTr�r�r
r
r
rr#asr#�__main__)�_diffcommand)F)T)T)F)F)PZ
__future__rr�ZlxmlrZ lxml.htmlr�re�__all__r:rr �ImportErrorZcgiZunicoder
� NameError�strr�rrrrr%rr5rrr=rCrDrNrR� ExceptionrSrErFrIrUrVrWr9rsrvrry�compile�I�Sr}rr�r|Zend_whitespace_rer�r{rcr�r�rz�Ur�r�r�r�r�r�r�r�r>r�r�r�r�r�r�r#rOr��mainr
r
r
r�<module>s�
(
&
'(2)
7
"