Annotate zerver/lib/html_diff.py.

This commit is contained in:
medullaskyline
2016-06-03 11:11:47 -07:00
committed by Tim Abbott
parent 2ec0114079
commit cb84f72f2d

View File

@@ -1,4 +1,5 @@
from __future__ import absolute_import
from typing import Callable, Tuple
from django.conf import settings
@@ -9,18 +10,23 @@ import logging
# TODO: handle changes in link hrefs
def highlight_with_class(klass, text):
# type: (str, str) -> str
return '<span class="%s">%s</span>' % (klass, text)
def highlight_inserted(text):
# type: (str) -> str
return highlight_with_class('highlight_text_inserted', text)
def highlight_deleted(text):
# type: (str) -> str
return highlight_with_class('highlight_text_deleted', text)
def highlight_replaced(text):
# type: (str) -> str
return highlight_with_class('highlight_text_replaced', text)
def chunkize(text, in_tag):
# type: (str, bool) -> Tuple[List[Tuple[str, str]], bool]
start = 0
idx = 0
chunks = []
@@ -42,6 +48,7 @@ def chunkize(text, in_tag):
return chunks, in_tag
def highlight_chunks(chunks, highlight_func):
# type: (List[Tuple[str, str]], Callable[[str], str]) -> str
retval = ''
for type, text in chunks:
if type == 'text':
@@ -51,6 +58,7 @@ def highlight_chunks(chunks, highlight_func):
return retval
def verify_html(html):
# type: (str) -> bool
# TODO: Actually parse the resulting HTML to ensure we don't
# create mal-formed markup. This is unfortunately hard because
# we both want pretty strict parsing and we want to parse html5
@@ -70,6 +78,7 @@ def verify_html(html):
return True
def highlight_html_differences(s1, s2):
# type: (str, str) -> str
differ = diff_match_patch()
ops = differ.diff_main(s1, s2)
differ.diff_cleanupSemantic(ops)