~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/util/diff_html.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - Side by side diffs
 
4
 
 
5
    @copyright: 2002 Juergen Hermann <jh@web.de>,
 
6
                2002 Scott Moonen <smoonen@andstuff.org>
 
7
    @license: GNU GPL, see COPYING for details.
 
8
"""
 
9
 
 
10
from MoinMoin.support import difflib
 
11
from MoinMoin.wikiutil import escape
 
12
 
 
13
def indent(line):
 
14
    eol = ''
 
15
    while line and line[0] == '\n':
 
16
        eol += '\n'
 
17
        line = line[1:]
 
18
    stripped = line.lstrip()
 
19
    if len(line) - len(stripped):
 
20
        line = "&nbsp;" * (len(line) - len(stripped)) + stripped
 
21
    #return "%d / %d / %s" % (len(line), len(stripped), line)
 
22
    return eol + line
 
23
 
 
24
 
 
25
# This code originally by Scott Moonen, used with permission.
 
26
def diff(request, old, new):
 
27
    """ Find changes between old and new and return
 
28
        HTML markup visualising them.
 
29
    """
 
30
    _ = request.getText
 
31
    t_line = _("Line") + " %d"
 
32
 
 
33
    seq1 = old.splitlines()
 
34
    seq2 = new.splitlines()
 
35
 
 
36
    seqobj = difflib.SequenceMatcher(None, seq1, seq2)
 
37
    linematch = seqobj.get_matching_blocks()
 
38
 
 
39
    if len(seq1) == len(seq2) and linematch[0] == (0, 0, len(seq1)):
 
40
        # No differences.
 
41
        return " - " + _("No differences found!")
 
42
 
 
43
    lastmatch = (0, 0)
 
44
 
 
45
    result = """
 
46
<table class="diff">
 
47
<tr>
 
48
<td class="diff-removed">
 
49
<span>
 
50
%s
 
51
</span>
 
52
</td>
 
53
<td class="diff-added">
 
54
<span>
 
55
%s
 
56
</span>
 
57
</td>
 
58
</tr>
 
59
""" % (_('Deletions are marked like this.'), _('Additions are marked like this.'), )
 
60
 
 
61
    # Print all differences
 
62
    for match in linematch:
 
63
        # Starts of pages identical?
 
64
        if lastmatch == match[0:2]:
 
65
            lastmatch = (match[0] + match[2], match[1] + match[2])
 
66
            continue
 
67
        llineno, rlineno = lastmatch[0]+1, lastmatch[1]+1
 
68
        result += """
 
69
<tr class="diff-title">
 
70
<td>
 
71
%s:
 
72
</td>
 
73
<td>
 
74
%s:
 
75
</td>
 
76
</tr>
 
77
""" % (request.formatter.line_anchorlink(1, llineno) + request.formatter.text(t_line % llineno) + request.formatter.line_anchorlink(0),
 
78
       request.formatter.line_anchorlink(1, rlineno) + request.formatter.text(t_line % rlineno) + request.formatter.line_anchorlink(0))
 
79
 
 
80
        leftpane = ''
 
81
        rightpane = ''
 
82
        linecount = max(match[0] - lastmatch[0], match[1] - lastmatch[1])
 
83
        for line in range(linecount):
 
84
            if line < match[0] - lastmatch[0]:
 
85
                if line > 0:
 
86
                    leftpane += '\n'
 
87
                leftpane += seq1[lastmatch[0] + line]
 
88
            if line < match[1] - lastmatch[1]:
 
89
                if line > 0:
 
90
                    rightpane += '\n'
 
91
                rightpane += seq2[lastmatch[1] + line]
 
92
 
 
93
        charobj = difflib.SequenceMatcher(None, leftpane, rightpane)
 
94
        charmatch = charobj.get_matching_blocks()
 
95
 
 
96
        if charobj.ratio() < 0.5:
 
97
            # Insufficient similarity.
 
98
            if leftpane:
 
99
                leftresult = """<span>%s</span>""" % indent(escape(leftpane))
 
100
            else:
 
101
                leftresult = ''
 
102
 
 
103
            if rightpane:
 
104
                rightresult = """<span>%s</span>""" % indent(escape(rightpane))
 
105
            else:
 
106
                rightresult = ''
 
107
        else:
 
108
            # Some similarities; markup changes.
 
109
            charlast = (0, 0)
 
110
 
 
111
            leftresult = ''
 
112
            rightresult = ''
 
113
            for thismatch in charmatch:
 
114
                if thismatch[0] - charlast[0] != 0:
 
115
                    leftresult += """<span>%s</span>""" % indent(
 
116
                        escape(leftpane[charlast[0]:thismatch[0]]))
 
117
                if thismatch[1] - charlast[1] != 0:
 
118
                    rightresult += """<span>%s</span>""" % indent(
 
119
                        escape(rightpane[charlast[1]:thismatch[1]]))
 
120
                leftresult += escape(leftpane[thismatch[0]:thismatch[0] + thismatch[2]])
 
121
                rightresult += escape(rightpane[thismatch[1]:thismatch[1] + thismatch[2]])
 
122
                charlast = (thismatch[0] + thismatch[2], thismatch[1] + thismatch[2])
 
123
 
 
124
        leftpane = '<br>\n'.join([indent(x) for x in leftresult.splitlines()])
 
125
        rightpane = '<br>\n'.join([indent(x) for x in rightresult.splitlines()])
 
126
 
 
127
        # removed width="50%%"
 
128
        result += """
 
129
<tr>
 
130
<td class="diff-removed">
 
131
%s
 
132
</td>
 
133
<td class="diff-added">
 
134
%s
 
135
</td>
 
136
</tr>
 
137
""" % (leftpane, rightpane)
 
138
 
 
139
        lastmatch = (match[0] + match[2], match[1] + match[2])
 
140
 
 
141
    result += '</table>\n'
 
142
    return result
 
143