2020-06-11 00:54:34 +02:00
|
|
|
from typing import Optional
|
2016-06-17 16:59:29 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import lxml
|
2017-10-30 21:11:34 +01:00
|
|
|
from lxml.html.diff import htmldiff
|
2020-06-11 00:54:34 +02:00
|
|
|
|
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:
|
2020-06-10 06:41:04 +02:00
|
|
|
return f'<span class="{klass}">{text}</span>'
|
2013-05-31 21:03:16 +02:00
|
|
|
|
2021-02-12 08:19:30 +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
|
|
|
|
2021-02-12 08:20:45 +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
|
|
|
|
2021-02-12 08:20:45 +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
|
|
|
|
2020-08-07 06:57:18 +02:00
|
|
|
retval = lxml.html.tostring(fragment, encoding="unicode")
|
2013-05-31 21:03:16 +02:00
|
|
|
|
|
|
|
return retval
|