2017-10-30 21:11:34 +01:00
|
|
|
import lxml
|
2016-06-17 16:59:29 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
from lxml.html.diff import htmldiff
|
2017-10-30 21:24:22 +01:00
|
|
|
from typing import Optional
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def highlight_with_class(text: str, klass: str) -> str:
|
2013-05-31 21:03:16 +02:00
|
|
|
return '<span class="%s">%s</span>' % (klass, text)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def highlight_html_differences(s1: str, s2: str, msg_id: Optional[int]=None) -> str:
|
2017-10-30 21:11:34 +01:00
|
|
|
retval = htmldiff(s1, s2)
|
2017-11-25 19:05:12 +01:00
|
|
|
fragment = lxml.html.fromstring(retval)
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
for elem in fragment.cssselect('del'):
|
|
|
|
elem.tag = 'span'
|
|
|
|
elem.set('class', 'highlight_text_deleted')
|
2017-10-12 08:38:02 +02:00
|
|
|
|
2017-10-30 21:11:34 +01:00
|
|
|
for elem in fragment.cssselect('ins'):
|
|
|
|
elem.tag = 'span'
|
|
|
|
elem.set('class', 'highlight_text_inserted')
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2017-11-25 19:05:12 +01:00
|
|
|
retval = lxml.html.tostring(fragment)
|
2013-05-31 21:03:16 +02:00
|
|
|
|
|
|
|
return retval
|