Attachment 'BlurpSearch.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - BlurpSearch Macro
   4 
   5     just a modified FullSearch.
   6 
   7     [[BlurpSearch(term, prefix)]]
   8         A category serach, but only for pages beginning with prefix.
   9         If a ##BLURP pragma is on the page, it's contents are displayed
  10         after the match.
  11         The page links are shortened by the given prefix.
  12         The Category is only matched if exactly after a '----' line!
  13 
  14     @copyright: 2004 by Oliver Graf <ograf@bitart.de>
  15     @license: GNU GPL, see COPYING for details.
  16 """
  17 
  18 # Imports
  19 import re, urllib
  20 from MoinMoin import wikiutil
  21 from MoinMoin.Page import Page
  22 
  23 _blurp_re = r'^##BLURP (.+)$'
  24 _search_re = r'^----\n.*\b%s\b.*$'
  25 
  26 Dependencies = ["pages"]
  27 
  28 def execute(macro, text, blurp_re=re.compile(_blurp_re, re.M)):
  29     _ = macro.request.getText
  30 
  31     if text:
  32         parts = re.split('\s*,\s*',text,1)
  33         if len(parts)==1:
  34             needle = parts[0]
  35             prefix = ''
  36         else:
  37             needle = parts[0]
  38             prefix = parts[1]
  39     else:
  40         needle = macro.formatter.page.page_name
  41         prefix = ''
  42 
  43     # do the search
  44     pagecount, hits = wikiutil.searchPages(needle, literal=1, context=0)
  45 
  46     # generate the result
  47     result = []
  48     result.append(macro.formatter.number_list(1))
  49     sre = re.compile(_search_re%(needle,),re.M)
  50     for (count, pagename, dummy) in hits:
  51         if not macro.request.user.may.read(pagename):
  52             continue
  53         if pagename == macro.formatter.page.page_name:
  54             continue
  55         if prefix and not pagename.startswith(prefix):
  56             continue
  57         text=Page(pagename).get_raw_body()
  58         if not sre.search(text):
  59             continue
  60         result.append(macro.formatter.listitem(1))
  61         result.append(wikiutil.link_tag(macro.request,
  62             '%s?action=highlight&value=%s' % (
  63                 wikiutil.quoteWikiname(pagename),
  64                 urllib.quote_plus(needle)),
  65             pagename[len(prefix):]))
  66         blurp=None
  67         m=blurp_re.search(text)
  68         if m is not None:
  69             blurp=m.group(1)
  70         if blurp:
  71             result.append(': %s'%(blurp))
  72         result.append(macro.formatter.listitem(0))
  73     result.append(macro.formatter.number_list(0))
  74 
  75     return ''.join(result)

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.
  • [get | view] (2004-06-20 07:48:40, 2.2 KB) [[attachment:BlurpSearch.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.