Attachment 'ForEach-0.9.py'
Download 1 # -*- coding: iso-8859-1 -*-
2
3 """
4 MoinMoin - ForEach macro
5 Version 0.9
6
7 (C) 2008 Francesco Chemolli <kinkie@squid-cache.org>
8 Distributed under the terms of the GNU General Public License
9 version three.
10
11 [[ForEach(FullTextExpression,TextExpression)]]
12
13 searches for FullTextExpression and builds a list of TextExpression's,
14 one per page found. %% gets replaced with the page name.
15 Special characters can be escaped by \ (\ is expressed as \\)
16
17 e.g.
18 [[ForEach(CategoryFoo,== %% ==
19 \[\[TocOf(\%%\)\]\]
20 )]]
21
22 If pages contains a ##priority <number> directive, they will be sorted
23 according to it (higher number have higher preference),
24 pages with no priority specification come in last
25
26 Bazaar development branch at
27 http://eu.squid-cache.org/~kinkie/moinmoin
28
29 to get a checkout:
30 bzr co http://eu.squid-cache.org/~kinkie/moinmoin
31
32 """
33
34 import re
35 import StringIO
36 from MoinMoin import config, wikiutil, search
37 from MoinMoin.Page import Page
38
39 Dependencies = ["pages"]
40
41 def execute(macro,args):
42 request = macro.request
43 formatter = macro.formatter
44
45 #only in moin 1.6.. :\
46 #wikiutil.parse_quoted_separated(args,',',False);
47 args = parse_arguments(args)
48 if (len(args)==0):
49 return "No arguments given to [[ForEach()]]"
50
51 needle = args[0]
52 repl = args[1]
53
54 query = search.QueryParser().parse_query(needle)
55 results = search.searchPages(request,query).hits
56
57 results = [p.page_name for p in results]
58 macro.formatter.text('got pages: %s' % ', '.join(results))
59 results = sort_by_prios(request,results)
60
61 #results now contains the sorted list of pages matched.
62 #we need to change them to the matched text.
63
64 #for debugging purposes:
65 #tmp = [ (r+'('+get_page_prio(request,r)+')') for r in results]
66
67 results = [repl.replace('%%',r) for r in results]
68 p = re.compile( r'\\(.)' )
69 results = [p.sub(r'\1',r) for r in results]
70
71 #return macro.formatter.text('[[ForEach()]] is not yet complete. Pages found:'+", ".join(tmp))
72 if 0:
73 #FIXME: following block is for version 1.6 of MoinMoin
74 # buf is needed since the parser will try to output directly
75 buf = StringIO.StringIO()
76 try:
77 request.redirect(buf)
78 ret = request.formatter.parser('wiki',''.join(results))
79 finally:
80 request.redirect()
81 buf.flush()
82 writ = buf.getvalue()
83 buf.close()
84 return macro.formatter.rawHTML('%s%s' % (ret,writ))
85
86 #for version 1.5 of MoinMoin
87 Parser = wikiutil.importPlugin(request.cfg,'parser','wiki','Parser')
88 parser = Parser(''.join(results),request)
89 return parser.format(request.formatter) or ''
90
91
92
93 def parse_arguments(args):
94 if args:
95 args = args.split(',',1)
96 args = [arg.strip() for arg in args]
97 else:
98 args = []
99 return args
100
101 def sort_by_prios(request,pages):
102 """ Identifies the priority for a page
103
104 This function returns a priority specifier for a page, greater prios first.
105 It does so by checking the page contents.
106 Priority for pages not specifying any is set to -1 (default)
107 """
108 tmp = [(get_page_prio(request,p),p) for p in pages]
109 tmp.sort()
110 tmp.reverse()
111 tmp = [t[1] for t in tmp]
112 return tmp
113
114 def get_page_prio(request,path):
115 """ Gets the priority for the named page """
116
117 p = re.compile(r'##priority (?P<prio>\d+)')
118 page = Page(request,path)
119 body = page.get_raw_body()
120 m = p.search(body)
121 if m:
122 return m.group('prio')
123 else:
124 return -1
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.