Attachment 'Annotate-1.9-2.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - show diff between 2 page revisions
4
5 @copyright: 2010,2012 Henryk Gerlach <hgerlach@gmx.de>
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 allrevs = map(lambda l: int(l.rev), list(editlog.EditLog(request, rootpagename=pagename)))
82 if not allrevs:
83 #The page has no revisions. Maybe a default page.
84 currtext = []
85 else:
86 rev_iter = allrevs.pop(0)
87 currtext = annotate(rev_iter)
88 for rev_iter in allrevs:
89 oldtext = currtext
90 currtext = annotate(rev_iter)
91 currtext = merge_annotation(oldtext, currtext, ignorews=ignorews)
92 if rev_iter == rev: break
93
94
95
96 # build and send the page
97 page = Page(request, pagename)
98 def render_action(text, query, **kw):
99 kw.update(dict(rel='nofollow'))
100 return page.link_to(request, text, querystr=query, **kw)
101
102 f = request.formatter
103 if rev == currentrev:
104 request.theme.send_title(_('Annotate for "%s"') % (pagename, ), pagename=pagename)
105 else:
106 request.theme.send_title(_('Annotate for "%s" revison %d') % (pagename, rev ), pagename=pagename)
107
108 # Start content (important for RTL support)
109 request.write(request.formatter.startContent("content"))
110
111 request.write("<table><tr><th>"+_("Line")+"</th><th>"+_("Editor")+"</th><th>"+_("Date")+"</th><th>"+_("Rev")+"</th></tr>")
112 for annotation, l in currtext:
113 rev, editor, date = annotation
114 request.write("<tr><td>")
115 request.write(f.text(l))
116 request.write("</td><td>"+ editor[0] + "</td><td>"+date+"</td>")
117 request.write("<td>"+render_action(_(str(rev)), {'action': 'recall', 'rev': '%d' % rev})+"</td></tr>")
118
119 request.write("</table>")
120 request.write(request.formatter.endContent())
121 request.theme.send_footer(pagename)
122 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.