Attachment 'Annotate-1.9-1.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - show diff between 2 page revisions
4
5 @copyright: 2010 Henryk Gerlach
6 @license: GNU GPL, see COPYING for details.
7 """
8
9
10 import difflib
11 from MoinMoin import log
12 logging = log.getLogger(__name__)
13
14 from MoinMoin import wikiutil
15 from MoinMoin.logfile import editlog
16 from MoinMoin.Page import Page
17 from MoinMoin.widget import html
18
19 def execute(pagename, request):
20 """ Handle "action=annotate"
21 checking for parameter rev
22 """
23 if not request.user.may.read(pagename):
24 Page(request, pagename).send_page()
25 return
26
27 try:
28 rev = int(request.values.get('rev', None))
29 except StandardError:
30 rev = None
31
32 currentrev = Page(request, pagename).current_rev()
33 if rev == None or rev > currentrev:
34 rev = currentrev
35
36 # spacing flag?
37 ignorews = int(request.values.get('ignorews', 0))
38
39 _ = request.getText
40
41
42 def merge_annotation(oldlines, newlines, **kw):
43 """
44 Find changes between oldlines and newlines.
45
46 @param oldlines: list of old decoration and text lines (decoration, line)
47 @param newlines: list of new decoration and text lines (decoration, line)
48 @keyword ignorews: if 1: ignore whitespace
49 @rtype: list
50 @return: changed old lines get replaced by new lines with new decoration
51 """
52 false = lambda s: None
53 if kw.get('ignorews', 0):
54 d = difflib.Differ(false)
55 else:
56 d = difflib.Differ(false, false)
57
58 lines = list(d.compare( map(lambda s: s[1], oldlines), map(lambda s: s[1], newlines)))
59
60 result = []
61 iold = inew = 0
62 for l in lines:
63 if l[0] == " ":
64 result.append( oldlines[iold] )
65 iold += 1
66 inew += 1
67 if l[0] == "-":
68 iold+=1
69 if l[0] == "+":
70 result.append( newlines[inew] )
71 inew += 1
72 return result
73
74 def annotate(rev):
75 page = Page(request, pagename, rev=rev)
76 log = page.editlog_entry()
77 editor = log.getEditor(request) or _('N/A'),
78 date = request.user.getFormattedDateTime(wikiutil.version2timestamp(log.ed_time_usecs)) or _('N/A')
79 return map(lambda l: [(rev, editor, date), l], page.get_raw_body().split("\n"))
80
81 currrev = 1
82 currtext = annotate(currrev)
83 while currrev < rev:
84 oldtext = currtext
85 currrev += 1
86 currtext = annotate(currrev)
87 currtext = merge_annotation(oldtext, currtext, ignorews=ignorews)
88
89
90
91 # build and send the page
92 page = Page(request, pagename)
93 def render_action(text, query, **kw):
94 kw.update(dict(rel='nofollow'))
95 return page.link_to(request, text, querystr=query, **kw)
96
97 f = request.formatter
98 if rev == currentrev:
99 request.theme.send_title(_('Annotate for "%s"') % (pagename, ), pagename=pagename)
100 else:
101 request.theme.send_title(_('Annotate for "%s" revison %d') % (pagename, rev ), pagename=pagename)
102
103 # Start content (important for RTL support)
104 request.write(request.formatter.startContent("content"))
105
106 request.write("<table><tr><th>"+_("Line")+"</th><th>"+_("Editor")+"</th><th>"+_("Date")+"</th><th>"+_("Rev")+"</th></tr>")
107 for annotation, l in currtext:
108 rev, editor, date = annotation
109 request.write("<tr><td>")
110 request.write(f.text(l))
111 request.write("</td><td>"+ editor[0] + "</td><td>"+date+"</td>")
112 request.write("<td>"+render_action(_(str(rev)), {'action': 'recall', 'rev': '%d' % rev})+"</td></tr>")
113
114 request.write("</table>")
115 request.write(request.formatter.endContent())
116 request.theme.send_footer(pagename)
117 request.theme.send_closing_html()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.