[diffoscope] 08/08: presenters.html: Split linediff into its own module.

Chris Lamb chris at chris-lamb.co.uk
Sun Jan 1 12:48:55 CET 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 050741480a141bc8b2c7d646e7e840d86392c138
Author: Chris Lamb <lamby at debian.org>
Date:   Sun Jan 1 11:46:55 2017 +0000

    presenters.html: Split linediff into its own module.
---
 diffoscope/presenters/html/html.py     | 81 ++---------------------------
 diffoscope/presenters/html/linediff.py | 94 ++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 78 deletions(-)

diff --git a/diffoscope/presenters/html/html.py b/diffoscope/presenters/html/html.py
index fb043b4..9b9f897 100644
--- a/diffoscope/presenters/html/html.py
+++ b/diffoscope/presenters/html/html.py
@@ -47,6 +47,7 @@ from diffoscope.logging import logger
 from ..icon import FAVICON_BASE64
 
 from . import templates
+from .linediff import linediff
 
 # minimum line size, we add a zero-sized breakable space every
 # LINESIZE characters
@@ -103,82 +104,6 @@ def new_unified_diff():
     spl_print_func, spl_print_ctrl = None, None
 
 
-def sane(x):
-    r = ""
-    for i in x:
-        j = ord(i)
-        if i not in ['\t', '\n'] and (j < 32):
-            r = r + "."
-        else:
-            r = r + i
-    return r
-
-
-def linediff(s, t):
-    '''
-    Original line diff algorithm of diff2html. It's character based.
-    '''
-    if len(s):
-        s = ''.join([ sane(c) for c in s ])
-    if len(t):
-        t = ''.join([ sane(c) for c in t ])
-
-    m, n = len(s), len(t)
-    d = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
-
-
-    d[0][0] = (0, (0, 0))
-    for i in range(m+1)[1:]:
-        d[i][0] = (i,(i-1, 0))
-    for j in range(n+1)[1:]:
-        d[0][j] = (j,(0, j-1))
-
-    for i in range(m+1)[1:]:
-        for j in range(n+1)[1:]:
-            if s[i-1] == t[j-1]:
-                cost = 0
-            else:
-                cost = 1
-            d[i][j] = min((d[i-1][j][0] + 1, (i-1, j)),
-                          (d[i][j-1][0] + 1, (i, j-1)),
-                          (d[i-1][j-1][0] + cost, (i-1, j-1)))
-
-    l = []
-    coord = (m, n)
-    while coord != (0, 0):
-        l.insert(0, coord)
-        x, y = coord
-        coord = d[x][y][1]
-
-    l1 = []
-    l2 = []
-
-    for coord in l:
-        cx, cy = coord
-        child_val = d[cx][cy][0]
-
-        father_coord = d[cx][cy][1]
-        fx, fy = father_coord
-        father_val = d[fx][fy][0]
-
-        diff = (cx-fx, cy-fy)
-
-        if diff == (0, 1):
-            l1.append("")
-            l2.append(DIFFON + t[fy] + DIFFOFF)
-        elif diff == (1, 0):
-            l1.append(DIFFON + s[fx] + DIFFOFF)
-            l2.append("")
-        elif child_val-father_val == 1:
-            l1.append(DIFFON + s[fx] + DIFFOFF)
-            l2.append(DIFFON + t[fy] + DIFFOFF)
-        else:
-            l1.append(s[fx])
-            l2.append(t[fy])
-
-    return ''.join(l1).replace(DIFFOFF + DIFFON, ''), ''.join(l2).replace(DIFFOFF + DIFFON, '')
-
-
 def convert(s, ponct=0, tag=''):
     i = 0
     t = io.StringIO()
@@ -246,7 +171,7 @@ def output_line(s1, s2):
         type_name = "unmodified"
     else:
         type_name = "changed"
-        s1, s2 = linediff(s1, s2)
+        s1, s2 = linediff(s1, s2, DIFFON, DIFFOFF)
 
     spl_print_func(u'<tr class="diff%s">' % type_name)
     try:
@@ -621,5 +546,5 @@ def output_html_directory(directory, difference, css_url=None, jquery_url=None):
             print_func(u'<div class="error">Max output size reached.</div>',
                        force=True)
         if jquery_url:
-            print_func(template.SCRIPTS % {'jquery_url': html.escape(jquery_url)}, force=True)
+            print_func(templates.SCRIPTS % {'jquery_url': html.escape(jquery_url)}, force=True)
         output_footer(print_func)
diff --git a/diffoscope/presenters/html/linediff.py b/diffoscope/presenters/html/linediff.py
new file mode 100644
index 0000000..710ce36
--- /dev/null
+++ b/diffoscope/presenters/html/linediff.py
@@ -0,0 +1,94 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+
+def sane(x):
+    r = ""
+    for i in x:
+        j = ord(i)
+        if i not in ['\t', '\n'] and (j < 32):
+            r = r + "."
+        else:
+            r = r + i
+    return r
+
+
+def linediff(s, t, diffon, diffoff):
+    '''
+    Original line diff algorithm of diff2html. It's character based.
+    '''
+    if len(s):
+        s = ''.join([ sane(c) for c in s ])
+    if len(t):
+        t = ''.join([ sane(c) for c in t ])
+
+    m, n = len(s), len(t)
+    d = [[(0, 0) for i in range(n+1)] for i in range(m+1)]
+
+
+    d[0][0] = (0, (0, 0))
+    for i in range(m+1)[1:]:
+        d[i][0] = (i,(i-1, 0))
+    for j in range(n+1)[1:]:
+        d[0][j] = (j,(0, j-1))
+
+    for i in range(m+1)[1:]:
+        for j in range(n+1)[1:]:
+            if s[i-1] == t[j-1]:
+                cost = 0
+            else:
+                cost = 1
+            d[i][j] = min((d[i-1][j][0] + 1, (i-1, j)),
+                          (d[i][j-1][0] + 1, (i, j-1)),
+                          (d[i-1][j-1][0] + cost, (i-1, j-1)))
+
+    l = []
+    coord = (m, n)
+    while coord != (0, 0):
+        l.insert(0, coord)
+        x, y = coord
+        coord = d[x][y][1]
+
+    l1 = []
+    l2 = []
+
+    for coord in l:
+        cx, cy = coord
+        child_val = d[cx][cy][0]
+
+        father_coord = d[cx][cy][1]
+        fx, fy = father_coord
+        father_val = d[fx][fy][0]
+
+        diff = (cx-fx, cy-fy)
+
+        if diff == (0, 1):
+            l1.append("")
+            l2.append(diffon + t[fy] + diffoff)
+        elif diff == (1, 0):
+            l1.append(diffon + s[fx] + diffoff)
+            l2.append("")
+        elif child_val-father_val == 1:
+            l1.append(diffon + s[fx] + diffoff)
+            l2.append(diffon + t[fy] + diffoff)
+        else:
+            l1.append(s[fx])
+            l2.append(t[fy])
+
+    return ''.join(l1).replace(diffoff + diffon, ''), ''.join(l2).replace(diffoff + diffon, '')

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list