changeset 1019:6488692b1eb8

have all diff implementations at one place
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 24 Jul 2006 01:43:02 +0200
parents f5b796658f93
children b7544e3bd478 d3f7552b9e0a 9085983fc624
files MoinMoin/action/diff.py MoinMoin/util/diff.py MoinMoin/util/diff_html.py MoinMoin/util/diff_text.py MoinMoin/wikiutil.py
diffstat 5 files changed, 228 insertions(+), 218 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/diff.py	Mon Jul 24 00:19:55 2006 +0200
+++ b/MoinMoin/action/diff.py	Mon Jul 24 01:43:02 2006 +0200
@@ -7,6 +7,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
+from MoinMoin import wikiutil
 from MoinMoin.logfile import editlog
 from MoinMoin.Page import Page
 
@@ -126,11 +127,12 @@
     request.write('</p>')
 
     if request.user.show_fancy_diff:
-        from MoinMoin.util.diff import diff
-        request.write(diff(request, oldpage.get_raw_body(), newpage.get_raw_body()))
+        from MoinMoin.util import diff_html
+        request.write(diff_html.diff(request, oldpage.get_raw_body(), newpage.get_raw_body()))
         newpage.send_page(request, count_hit=0, content_only=1, content_id="content-below-diff")
     else:
-        lines = wikiutil.linediff(oldpage.getlines(), newpage.getlines())
+        from MoinMoin.util import diff_text
+        lines = diff_text.diff(oldpage.getlines(), newpage.getlines())
         if not lines:
             msg = _("No differences found!")
             if edit_count > 1:
--- a/MoinMoin/util/diff.py	Mon Jul 24 00:19:55 2006 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,143 +0,0 @@
-# -*- coding: iso-8859-1 -*-
-"""
-    MoinMoin - Side by side diffs
-
-    @copyright: 2002 by Jürgen Hermann <jh@web.de>
-    @copyright: 2002 by Scott Moonen <smoonen@andstuff.org>
-    @license: GNU GPL, see COPYING for details.
-"""
-
-from MoinMoin.support import difflib
-from MoinMoin.wikiutil import escape
-
-def indent(line):
-    eol = ''
-    while line and line[0] == '\n':
-        eol += '\n'
-        line = line[1:]
-    stripped = line.lstrip()
-    if len(line) - len(stripped):
-        line = "&nbsp;" * (len(line) - len(stripped)) + stripped
-    #return "%d / %d / %s" % (len(line), len(stripped), line)
-    return eol + line
-
-
-# This code originally by Scott Moonen, used with permission.
-def diff(request, old, new):
-    """ Find changes between old and new and return
-        HTML markup visualising them.
-    """
-    _ = request.getText
-    t_line = _("Line") + " %d"
-
-    seq1 = old.splitlines()
-    seq2 = new.splitlines()
-
-    seqobj = difflib.SequenceMatcher(None, seq1, seq2)
-    linematch = seqobj.get_matching_blocks()
-
-    if len(seq1) == len(seq2) and linematch[0] == (0, 0, len(seq1)):
-        # No differences.
-        return _("No differences found!")
-
-    lastmatch = (0, 0)
-
-    result = """
-<table class="diff">
-<tr>
-<td class="diff-removed">
-<span>
-%s
-</span>
-</td>
-<td class="diff-added">
-<span>
-%s
-</span>
-</td>
-</tr>
-""" % (_('Deletions are marked like this.'), _('Additions are marked like this.'),)
-
-    # Print all differences
-    for match in linematch:
-        # Starts of pages identical?
-        if lastmatch == match[0:2]:
-            lastmatch = (match[0] + match[2], match[1] + match[2])
-            continue
-        llineno, rlineno = lastmatch[0]+1, lastmatch[1]+1
-        result += """
-<tr class="diff-title">
-<td>
-%s:
-</td>
-<td>
-%s:
-</td>
-</tr>
-""" % (request.formatter.line_anchorlink(1, llineno) + request.formatter.text(t_line % llineno) + request.formatter.line_anchorlink(0),
-       request.formatter.line_anchorlink(1, rlineno) + request.formatter.text(t_line % rlineno) + request.formatter.line_anchorlink(0))
-
-        leftpane = ''
-        rightpane = ''
-        linecount = max(match[0] - lastmatch[0], match[1] - lastmatch[1])
-        for line in range(linecount):
-            if line < match[0] - lastmatch[0]:
-                if line > 0:
-                    leftpane += '\n'
-                leftpane += seq1[lastmatch[0] + line]
-            if line < match[1] - lastmatch[1]:
-                if line > 0:
-                    rightpane += '\n'
-                rightpane += seq2[lastmatch[1] + line]
-
-        charobj = difflib.SequenceMatcher(None, leftpane, rightpane)
-        charmatch = charobj.get_matching_blocks()
-
-        if charobj.ratio() < 0.5:
-            # Insufficient similarity.
-            if leftpane:
-                leftresult = """<span>%s</span>""" % indent(escape(leftpane))
-            else:
-                leftresult = ''
-
-            if rightpane:
-                rightresult = """<span>%s</span>""" % indent(escape(rightpane))
-            else:
-                rightresult = ''
-        else:
-            # Some similarities; markup changes.
-            charlast = (0, 0)
-
-            leftresult = ''
-            rightresult = ''
-            for thismatch in charmatch:
-                if thismatch[0] - charlast[0] != 0:
-                    leftresult += """<span>%s</span>""" % indent(
-                        escape(leftpane[charlast[0]:thismatch[0]]))
-                if thismatch[1] - charlast[1] != 0:
-                    rightresult += """<span>%s</span>""" % indent(
-                        escape(rightpane[charlast[1]:thismatch[1]]))
-                leftresult += escape(leftpane[thismatch[0]:thismatch[0] + thismatch[2]])
-                rightresult += escape(rightpane[thismatch[1]:thismatch[1] + thismatch[2]])
-                charlast = (thismatch[0] + thismatch[2], thismatch[1] + thismatch[2])
-
-        leftpane = '<br>\n'.join(map(indent, leftresult.splitlines()))
-        rightpane = '<br>\n'.join(map(indent, rightresult.splitlines()))
-
-        # removed width="50%%"
-        result += """
-<tr>
-<td class="diff-removed">
-%s
-</td>
-<td class="diff-added">
-%s
-</td>
-</tr>
-""" % (leftpane, rightpane)
-
-        lastmatch = (match[0] + match[2], match[1] + match[2])
-
-    result += '</table>\n'
-    return result
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/diff_html.py	Mon Jul 24 01:43:02 2006 +0200
@@ -0,0 +1,143 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - Side by side diffs
+
+    @copyright: 2002 by Jürgen Hermann <jh@web.de>
+    @copyright: 2002 by Scott Moonen <smoonen@andstuff.org>
+    @license: GNU GPL, see COPYING for details.
+"""
+
+from MoinMoin.support import difflib
+from MoinMoin.wikiutil import escape
+
+def indent(line):
+    eol = ''
+    while line and line[0] == '\n':
+        eol += '\n'
+        line = line[1:]
+    stripped = line.lstrip()
+    if len(line) - len(stripped):
+        line = "&nbsp;" * (len(line) - len(stripped)) + stripped
+    #return "%d / %d / %s" % (len(line), len(stripped), line)
+    return eol + line
+
+
+# This code originally by Scott Moonen, used with permission.
+def diff(request, old, new):
+    """ Find changes between old and new and return
+        HTML markup visualising them.
+    """
+    _ = request.getText
+    t_line = _("Line") + " %d"
+
+    seq1 = old.splitlines()
+    seq2 = new.splitlines()
+
+    seqobj = difflib.SequenceMatcher(None, seq1, seq2)
+    linematch = seqobj.get_matching_blocks()
+
+    if len(seq1) == len(seq2) and linematch[0] == (0, 0, len(seq1)):
+        # No differences.
+        return _("No differences found!")
+
+    lastmatch = (0, 0)
+
+    result = """
+<table class="diff">
+<tr>
+<td class="diff-removed">
+<span>
+%s
+</span>
+</td>
+<td class="diff-added">
+<span>
+%s
+</span>
+</td>
+</tr>
+""" % (_('Deletions are marked like this.'), _('Additions are marked like this.'),)
+
+    # Print all differences
+    for match in linematch:
+        # Starts of pages identical?
+        if lastmatch == match[0:2]:
+            lastmatch = (match[0] + match[2], match[1] + match[2])
+            continue
+        llineno, rlineno = lastmatch[0]+1, lastmatch[1]+1
+        result += """
+<tr class="diff-title">
+<td>
+%s:
+</td>
+<td>
+%s:
+</td>
+</tr>
+""" % (request.formatter.line_anchorlink(1, llineno) + request.formatter.text(t_line % llineno) + request.formatter.line_anchorlink(0),
+       request.formatter.line_anchorlink(1, rlineno) + request.formatter.text(t_line % rlineno) + request.formatter.line_anchorlink(0))
+
+        leftpane = ''
+        rightpane = ''
+        linecount = max(match[0] - lastmatch[0], match[1] - lastmatch[1])
+        for line in range(linecount):
+            if line < match[0] - lastmatch[0]:
+                if line > 0:
+                    leftpane += '\n'
+                leftpane += seq1[lastmatch[0] + line]
+            if line < match[1] - lastmatch[1]:
+                if line > 0:
+                    rightpane += '\n'
+                rightpane += seq2[lastmatch[1] + line]
+
+        charobj = difflib.SequenceMatcher(None, leftpane, rightpane)
+        charmatch = charobj.get_matching_blocks()
+
+        if charobj.ratio() < 0.5:
+            # Insufficient similarity.
+            if leftpane:
+                leftresult = """<span>%s</span>""" % indent(escape(leftpane))
+            else:
+                leftresult = ''
+
+            if rightpane:
+                rightresult = """<span>%s</span>""" % indent(escape(rightpane))
+            else:
+                rightresult = ''
+        else:
+            # Some similarities; markup changes.
+            charlast = (0, 0)
+
+            leftresult = ''
+            rightresult = ''
+            for thismatch in charmatch:
+                if thismatch[0] - charlast[0] != 0:
+                    leftresult += """<span>%s</span>""" % indent(
+                        escape(leftpane[charlast[0]:thismatch[0]]))
+                if thismatch[1] - charlast[1] != 0:
+                    rightresult += """<span>%s</span>""" % indent(
+                        escape(rightpane[charlast[1]:thismatch[1]]))
+                leftresult += escape(leftpane[thismatch[0]:thismatch[0] + thismatch[2]])
+                rightresult += escape(rightpane[thismatch[1]:thismatch[1] + thismatch[2]])
+                charlast = (thismatch[0] + thismatch[2], thismatch[1] + thismatch[2])
+
+        leftpane = '<br>\n'.join(map(indent, leftresult.splitlines()))
+        rightpane = '<br>\n'.join(map(indent, rightresult.splitlines()))
+
+        # removed width="50%%"
+        result += """
+<tr>
+<td class="diff-removed">
+%s
+</td>
+<td class="diff-added">
+%s
+</td>
+</tr>
+""" % (leftpane, rightpane)
+
+        lastmatch = (match[0] + match[2], match[1] + match[2])
+
+    result += '</table>\n'
+    return result
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MoinMoin/util/diff_text.py	Mon Jul 24 01:43:02 2006 +0200
@@ -0,0 +1,78 @@
+# -*- coding: iso-8859-1 -*-
+"""
+    MoinMoin - simple text diff (uses difflib)
+
+    @copyright: 2006 by MoinMoin:ThomasWaldmann
+    @license: GNU GPL, see COPYING for details.
+"""
+from MoinMoin.support import difflib
+
+def diff(oldlines, newlines, **kw):
+    """
+    Find changes between oldlines and newlines.
+    
+    @param oldlines: list of old text lines
+    @param newlines: list of new text lines
+    @keyword ignorews: if 1: ignore whitespace
+    @rtype: list
+    @return: lines like diff tool does output.
+    """
+    false = lambda s: None
+    if kw.get('ignorews', 0):
+        d = difflib.Differ(false)
+    else:
+        d = difflib.Differ(false, false)
+
+    lines = list(d.compare(oldlines, newlines))
+
+    # return empty list if there were no changes
+    changed = 0
+    for l in lines:
+        if l[0] != ' ':
+            changed = 1
+            break
+    if not changed: return []
+
+    if not "we want the unchanged lines, too":
+        if "no questionmark lines":
+            lines = filter(lambda line: line[0] != '?', lines)
+        return lines
+
+
+    # calculate the hunks and remove the unchanged lines between them
+    i = 0              # actual index in lines
+    count = 0          # number of unchanged lines
+    lcount_old = 0     # line count old file
+    lcount_new = 0     # line count new file
+    while i < len(lines):
+        marker = lines[i][0]
+        if marker == ' ':
+            count = count + 1
+            i = i + 1
+            lcount_old = lcount_old + 1
+            lcount_new = lcount_new + 1
+        elif marker in ['-', '+']:
+            if (count == i) and count > 3:
+                lines[:i-3] = []
+                i = 4
+                count = 0
+            elif count > 6:
+                # remove lines and insert new hunk indicator
+                lines[i-count+3:i-3] = ['@@ -%i, +%i @@\n' %
+                                        (lcount_old, lcount_new)]
+                i = i - count + 8
+                count = 0
+            else:
+                count = 0
+                i += 1
+            if marker == '-': lcount_old = lcount_old + 1
+            else: lcount_new = lcount_new + 1
+        elif marker == '?':
+            lines[i:i+1] = []
+
+    # remove unchanged lines a the end
+    if count > 3:
+        lines[-count+3:] = []
+
+    return lines
+
--- a/MoinMoin/wikiutil.py	Mon Jul 24 00:19:55 2006 +0200
+++ b/MoinMoin/wikiutil.py	Mon Jul 24 01:43:02 2006 +0200
@@ -9,7 +9,6 @@
 import os, re, urllib, cgi
 import codecs, types
 
-from MoinMoin.support import difflib
 from MoinMoin import util, version, config
 from MoinMoin.util import pysupport, filesys
 
@@ -1449,76 +1448,6 @@
     """ Returns true if there is a conflict marker in the text. """
     return "/!\ '''Edit conflict" in text
 
-def linediff(oldlines, newlines, **kw):
-    """
-    Find changes between oldlines and newlines.
-    
-    @param oldlines: list of old text lines
-    @param newlines: list of new text lines
-    @keyword ignorews: if 1: ignore whitespace
-    @rtype: list
-    @return: lines like diff tool does output.
-    """
-    false = lambda s: None
-    if kw.get('ignorews', 0):
-        d = difflib.Differ(false)
-    else:
-        d = difflib.Differ(false, false)
-
-    lines = list(d.compare(oldlines, newlines))
-
-    # return empty list if there were no changes
-    changed = 0
-    for l in lines:
-        if l[0] != ' ':
-            changed = 1
-            break
-    if not changed: return []
-
-    if not "we want the unchanged lines, too":
-        if "no questionmark lines":
-            lines = filter(lambda line: line[0] != '?', lines)
-        return lines
-
-
-    # calculate the hunks and remove the unchanged lines between them
-    i = 0              # actual index in lines
-    count = 0          # number of unchanged lines
-    lcount_old = 0     # line count old file
-    lcount_new = 0     # line count new file
-    while i < len(lines):
-        marker = lines[i][0]
-        if marker == ' ':
-            count = count + 1
-            i = i + 1
-            lcount_old = lcount_old + 1
-            lcount_new = lcount_new + 1
-        elif marker in ['-', '+']:
-            if (count == i) and count > 3:
-                lines[:i-3] = []
-                i = 4
-                count = 0
-            elif count > 6:
-                # remove lines and insert new hunk indicator
-                lines[i-count+3:i-3] = ['@@ -%i, +%i @@\n' %
-                                        (lcount_old, lcount_new)]
-                i = i - count + 8
-                count = 0
-            else:
-                count = 0
-                i += 1
-            if marker == '-': lcount_old = lcount_old + 1
-            else: lcount_new = lcount_new + 1
-        elif marker == '?':
-            lines[i:i+1] = []
-
-    # remove unchanged lines a the end
-    if count > 3:
-        lines[-count+3:] = []
-
-    return lines
-
-
 def pagediff(request, pagename1, rev1, pagename2, rev2, **kw):
     """
     Calculate the "diff" between two page contents.
@@ -1532,10 +1461,11 @@
     @return: lines of diff output
     """
     from MoinMoin.Page import Page
+    from MoinMoin.util import diff_text
     lines1 = Page(request, pagename1, rev=rev1).getlines()
     lines2 = Page(request, pagename2, rev=rev2).getlines()
 
-    lines = linediff(lines1, lines2, **kw)
+    lines = diff_text.diff(lines1, lines2, **kw)
     return lines