Adding diff patch html for description fields
parent
be5e37187c
commit
53425bedba
|
@ -18,6 +18,7 @@ pygments>=1.6
|
||||||
django-sites==0.4
|
django-sites==0.4
|
||||||
Markdown==2.4
|
Markdown==2.4
|
||||||
fn==0.2.13
|
fn==0.2.13
|
||||||
|
diff-match-patch==20110725.1
|
||||||
|
|
||||||
# Comment it if you are using python >= 3.4
|
# Comment it if you are using python >= 3.4
|
||||||
enum34==0.9.23
|
enum34==0.9.23
|
||||||
|
|
|
@ -32,6 +32,9 @@ def _make_extensions_list(wikilinks_config=None):
|
||||||
"codehilite"]
|
"codehilite"]
|
||||||
|
|
||||||
|
|
||||||
|
import diff_match_patch
|
||||||
|
|
||||||
|
|
||||||
def cache_by_sha(func):
|
def cache_by_sha(func):
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def _decorator(project, text):
|
def _decorator(project, text):
|
||||||
|
@ -71,5 +74,23 @@ def render(project, text):
|
||||||
renderer = F() >> F(_preprocessors, project) >> F(_render_markdown, project) >> F(_postprocessors, project)
|
renderer = F() >> F(_preprocessors, project) >> F(_render_markdown, project) >> F(_postprocessors, project)
|
||||||
return renderer(text)
|
return renderer(text)
|
||||||
|
|
||||||
|
class DiffMatchPatch(diff_match_patch.diff_match_patch):
|
||||||
|
def diff_pretty_html(self, diffs):
|
||||||
|
html = []
|
||||||
|
for (op, data) in diffs:
|
||||||
|
text = (data.replace("&", "&").replace("<", "<")
|
||||||
|
.replace(">", ">").replace("\n", "<br />"))
|
||||||
|
if op == self.DIFF_INSERT:
|
||||||
|
html.append("<ins style=\"background:#e6ffe6;\">%s</ins>" % text)
|
||||||
|
elif op == self.DIFF_DELETE:
|
||||||
|
html.append("<del style=\"background:#ffe6e6;\">%s</del>" % text)
|
||||||
|
elif op == self.DIFF_EQUAL:
|
||||||
|
html.append("<span>%s</span>" % text)
|
||||||
|
return "".join(html)
|
||||||
|
|
||||||
__all__ = ['render']
|
def get_diff_of_htmls(html1, html2):
|
||||||
|
diffutil = DiffMatchPatch()
|
||||||
|
diff = diffutil.diff_main(html1, html2)
|
||||||
|
return diffutil.diff_pretty_html(diff)
|
||||||
|
|
||||||
|
__all__ = ['render', 'get_diff_of_htmls']
|
||||||
|
|
|
@ -39,6 +39,7 @@ from django.contrib.contenttypes.models import ContentType
|
||||||
from .models import HistoryType
|
from .models import HistoryType
|
||||||
|
|
||||||
from taiga.mdrender.service import render as mdrender
|
from taiga.mdrender.service import render as mdrender
|
||||||
|
from taiga.mdrender.service import get_diff_of_htmls
|
||||||
|
|
||||||
# Type that represents a freezed object
|
# Type that represents a freezed object
|
||||||
FrozenObj = namedtuple("FrozenObj", ["key", "snapshot"])
|
FrozenObj = namedtuple("FrozenObj", ["key", "snapshot"])
|
||||||
|
@ -155,6 +156,13 @@ def make_diff(oldobj:FrozenObj, newobj:FrozenObj) -> FrozenDiff:
|
||||||
if key not in first:
|
if key not in first:
|
||||||
diff[key] = (not_found_value, second[key])
|
diff[key] = (not_found_value, second[key])
|
||||||
|
|
||||||
|
if "description" in diff:
|
||||||
|
description_diff = get_diff_of_htmls(
|
||||||
|
diff["description"][0],
|
||||||
|
diff["description"][1]
|
||||||
|
)
|
||||||
|
diff["description_diff"] = (not_found_value, description_diff)
|
||||||
|
|
||||||
return FrozenDiff(newobj.key, diff, newobj.snapshot)
|
return FrozenDiff(newobj.key, diff, newobj.snapshot)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue