comparison MoinMoin/macro/__init__.py @ 735:e3c44dc8f488

merge common code of TitleIndex and WordIndex into _make_index
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 02 Jun 2006 19:04:27 +0200
parents 15c55ecd7ccb
children d48400378d4c
comparison
equal deleted inserted replaced
734:bf2435e61f97 735:e3c44dc8f488
45 lnames = names[:] 45 lnames = names[:]
46 lnames.extend(i18n.wikiLanguages().keys()) 46 lnames.extend(i18n.wikiLanguages().keys())
47 lnames.extend(wikiutil.getPlugins('macro', cfg)) 47 lnames.extend(wikiutil.getPlugins('macro', cfg))
48 cfg.macro_names = lnames # remember it 48 cfg.macro_names = lnames # remember it
49 return lnames 49 return lnames
50
51 def _make_index_key(index_letters, additional_html=""):
52 index_letters.sort()
53 links = map(lambda ch:
54 '<a href="#%s">%s</a>' %
55 (wikiutil.quoteWikinameURL(ch), ch.replace('~', 'Others')),
56 index_letters)
57 return "<p>%s%s</p>" % (' | '.join(links), additional_html)
58 50
59 51
60 ############################################################################# 52 #############################################################################
61 ### Macros - Handlers for [[macroname]] markup 53 ### Macros - Handlers for [[macroname]] markup
62 ############################################################################# 54 #############################################################################
221 u'</form>', 213 u'</form>',
222 ] 214 ]
223 html = u'\n'.join(html) 215 html = u'\n'.join(html)
224 return self.formatter.rawHTML(html) 216 return self.formatter.rawHTML(html)
225 217
226 def _macro_WordIndex(self, args): 218 def _make_index(self, args, word_re=u'.+'):
219 """ make an index page (used for TitleIndex and WordIndex macro)
220
221 word_re is a regex used for splitting a pagename into fragments
222 matched by it (used for WordIndex). For TitleIndex, we just match
223 the whole page name, so we only get one fragment that is the same
224 as the pagename.
225
226 TODO: later this can get a macro on its own, more powerful and less
227 special than WordIndex and TitleIndex.
228 It should be able to filter for specific mimetypes, maybe match
229 pagenames by regex (replace PageList?), etc.
230
231 it should use the formatter asap
232 """
227 _ = self._ 233 _ = self._
228 allpages = int(self.form.get('allpages', [0])[0]) != 0 234 allpages = int(self.form.get('allpages', [0])[0]) != 0
229 # Get page list readable by current user 235 # Get page list readable by current user
230 # Filter by isSystemPage if needed 236 # Filter by isSystemPage if needed
231 if allpages: 237 if allpages:
233 pages = self.request.rootpage.getPageList() 239 pages = self.request.rootpage.getPageList()
234 else: 240 else:
235 def filter(name): 241 def filter(name):
236 return not wikiutil.isSystemPage(self.request, name) 242 return not wikiutil.isSystemPage(self.request, name)
237 pages = self.request.rootpage.getPageList(filter=filter) 243 pages = self.request.rootpage.getPageList(filter=filter)
244
245 word_re = re.compile(word_re, re.UNICODE)
238 map = {} 246 map = {}
239 word_re = re.compile(u'[%s][%s]+' % (config.chars_upper, config.chars_lower), re.UNICODE)
240 for name in pages: 247 for name in pages:
241 for word in word_re.findall(name): 248 for word in word_re.findall(name):
242 try: 249 try:
243 if not map[word].count(name): 250 if not map[word].count(name):
244 map[word].append(name) 251 map[word].append(name)
245 except KeyError: 252 except KeyError:
246 map[word] = [name] 253 map[word] = [name]
247 254
255 # Sort ignoring case
248 all_words = map.keys() 256 all_words = map.keys()
249 all_words.sort() 257 tmp = [(word.upper(), word) for word in all_words]
258 tmp.sort()
259 all_words = [item[1] for item in tmp]
260
250 index_letters = [] 261 index_letters = []
251 current_letter = None 262 current_letter = None
252 html = [] 263 html = []
253 for word in all_words: 264 for word in all_words:
254 letter = wikiutil.getUnicodeIndexGroup(word) 265 letter = wikiutil.getUnicodeIndexGroup(word)
255 if letter != current_letter: 266 if letter != current_letter:
256 #html.append(self.formatter.anchordef()) # XXX no text param available! 267 #html.append(self.formatter.anchordef()) # XXX no text param available!
257 html.append(u'<a name="%s"><h3>%s</h3></a>' % ( 268 html.append(u'<a name="%s"><h2>%s</h2></a>' % (
258 wikiutil.quoteWikinameURL(letter), letter.replace('~', 'Others'))) 269 wikiutil.quoteWikinameURL(letter), letter.replace('~', 'Others')))
259 current_letter = letter 270 current_letter = letter
260 if letter not in index_letters: 271 if letter not in index_letters:
261 index_letters.append(letter) 272 index_letters.append(letter)
262 273 links = map[word]
263 html.append(self.formatter.strong(1)) 274 if len(links) and links[0] != word: # show word fragment as on WordIndex
264 html.append(word) 275 html.append(self.formatter.strong(1))
265 html.append(self.formatter.strong(0)) 276 html.append(word)
277 html.append(self.formatter.strong(0))
278
266 html.append(self.formatter.bullet_list(1)) 279 html.append(self.formatter.bullet_list(1))
267 links = map[word]
268 links.sort() 280 links.sort()
269 last_page = None 281 last_page = None
270 for name in links: 282 for name in links:
271 if name == last_page: 283 if name == last_page:
272 continue 284 continue
273 html.append(self.formatter.listitem(1)) 285 html.append(self.formatter.listitem(1))
274 html.append(Page(self.request, name).link_to(self.request)) 286 html.append(Page(self.request, name).link_to(self.request, attachment_indicator=1))
275 html.append(self.formatter.listitem(0)) 287 html.append(self.formatter.listitem(0))
276 html.append(self.formatter.bullet_list(0)) 288 html.append(self.formatter.bullet_list(0))
277 289
290 def _make_index_key(index_letters, additional_html=''):
291 index_letters.sort()
292 def letter_link(ch):
293 return '<a href="#%s">%s</a>' % (wikiutil.quoteWikinameURL(ch), ch.replace('~', 'Others'))
294 links = [letter_link(letter) for letter in index_letters]
295 return "<p>%s%s</p>" % (' | '.join(links), additional_html)
296
278 qpagename = wikiutil.quoteWikinameURL(self.formatter.page.page_name) 297 qpagename = wikiutil.quoteWikinameURL(self.formatter.page.page_name)
298 allpages_txt = (_('Include system pages'), _('Exclude system pages'))[allpages]
279 index = _make_index_key(index_letters, u"""<br> 299 index = _make_index_key(index_letters, u"""<br>
280 <a href="%s?allpages=%d">%s</a> 300 <a href="%s?allpages=%d">%s</a>
281 """ % (qpagename, not allpages, (_('Include system pages'), _('Exclude system pages'))[allpages]) ) 301 """ % (qpagename, not allpages, allpages_txt) )
302 # ?action=titleindex and ?action=titleindex&mimetype=text/xml removed
303
282 return u'%s%s' % (index, u''.join(html)) 304 return u'%s%s' % (index, u''.join(html))
283 305
284 306
285 def _macro_TitleIndex(self, args): 307 def _macro_TitleIndex(self, args):
308 return self._make_index(args)
309
310 def _macro_WordIndex(self, args):
311 word_re = u'[%s][%s]+' % (config.chars_upper, config.chars_lower)
312 return self._make_index(args, word_re=word_re)
313
314
315 def _macro_PageList(self, needle):
316 from MoinMoin import search
286 _ = self._ 317 _ = self._
287 html = [] 318 literal = 0
288 index_letters = [] 319 case = 0
289 allpages = int(self.form.get('allpages', [0])[0]) != 0 320
290 # Get page list readable by current user 321 # If called with empty or no argument, default to regex search for .+, the full page list.
291 # Filter by isSystemPage if needed 322 if not needle:
292 if allpages: 323 needle = 'regex:.+'
293 # TODO: make this fast by caching full page list 324
294 pages = self.request.rootpage.getPageList() 325 # With whitespace argument, return same error message as FullSearch
295 else: 326 elif needle.isspace():
296 def filter(name): 327 err = _('Please use a more selective search term instead of {{{"%s"}}}') % needle
297 return not wikiutil.isSystemPage(self.request, name) 328 return '<span class="error">%s</span>' % err
298 pages = self.request.rootpage.getPageList(filter=filter) 329
299 330 # Return a title search for needle, sorted by name.
300 # Sort ignoring case 331 query = search.QueryParser(literal=literal, titlesearch=1, case=case).parse_query(needle)
301 tmp = [(name.upper(), name) for name in pages] 332 results = search.searchPages(self.request, query)
302 tmp.sort() 333 results.sortByPagename()
303 pages = [item[1] for item in tmp] 334 return results.pageList(self.request, self.formatter)
304 335
305 current_letter = None
306 for name in pages:
307 letter = wikiutil.getUnicodeIndexGroup(name)
308 if letter not in index_letters:
309 index_letters.append(letter)
310 if letter != current_letter:
311 html.append(u'<a name="%s"><h3>%s</h3></a>' % (
312 wikiutil.quoteWikinameURL(letter), letter.replace('~', 'Others')))
313 current_letter = letter
314 else:
315 html.append(u'<br>')
316 html.append(u'%s\n' % Page(self.request, name).link_to(self.request, attachment_indicator=1))
317
318 # add rss link
319 index = ''
320 if 0: # if wikixml.ok: # XXX currently switched off (not implemented)
321 from MoinMoin import wikixml
322 index = (index + self.formatter.url(1,
323 wikiutil.quoteWikinameURL(self.formatter.page.page_name) + "?action=rss_ti", do_escape=0) +
324 self.formatter.icon("rss") +
325 self.formatter.url(0))
326
327 qpagename = wikiutil.quoteWikinameURL(self.formatter.page.page_name)
328 index = index + _make_index_key(index_letters, u"""<br>
329 <a href="%s?allpages=%d">%s</a>&nbsp;|
330 <a href="%s?action=titleindex">%s</a>&nbsp;|
331 <a href="%s?action=titleindex&amp;mimetype=text/xml">%s</a>
332 """ % (qpagename, not allpages, (_('Include system pages'), _('Exclude system pages'))[allpages],
333 qpagename, _('Plain title index'),
334 qpagename, _('XML title index')) )
335
336 return u'%s%s' % (index, u''.join(html))
337
338
339 def _macro_InterWiki(self, args): 336 def _macro_InterWiki(self, args):
340 from StringIO import StringIO 337 from StringIO import StringIO
341 338
342 # load interwiki list 339 # load interwiki list
343 dummy = wikiutil.resolve_wiki(self.request, '') 340 dummy = wikiutil.resolve_wiki(self.request, '')
379 376
380 def _macro_Icon(self, args): 377 def _macro_Icon(self, args):
381 icon = args.lower() 378 icon = args.lower()
382 return self.formatter.icon(icon) 379 return self.formatter.icon(icon)
383 380
384 def _macro_PageList(self, needle):
385 from MoinMoin import search
386 _ = self._
387 literal=0
388 case=0
389
390 # If called with empty or no argument, default to regex search for .+,
391 # the full page list.
392 if not needle:
393 needle = 'regex:.+'
394
395 # With whitespace argument, return same error message as FullSearch
396 elif needle.isspace():
397 err = _('Please use a more selective search term instead of '
398 '{{{"%s"}}}') % needle
399 return '<span class="error">%s</span>' % err
400
401 # Return a title search for needle, sorted by name.
402 query = search.QueryParser(literal=literal, titlesearch=1,
403 case=case).parse_query(needle)
404 results = search.searchPages(self.request, query)
405 results.sortByPagename()
406 return results.pageList(self.request, self.formatter)
407
408 def _macro_TemplateList(self, args): 381 def _macro_TemplateList(self, args):
409 _ = self._ 382 _ = self._
410 try: 383 try:
411 needle_re = re.compile(args or '', re.IGNORECASE) 384 needle_re = re.compile(args or '', re.IGNORECASE)
412 except re.error, e: 385 except re.error, e: