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