Attachment 'TagCloud.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3    MoinMoin - TagCloud
   4 
   5    Create tagcloud, example: http://www.iam-wiki.org
   6 
   7    @copyright: 2007 by Christian Groh
   8 """
   9 
  10 import re
  11 from MoinMoin import search
  12 from MoinMoin.Page import Page
  13 from MoinMoin.PageEditor import PageEditor
  14 
  15 Dependencies = ["namespace"]
  16 
  17 def execute(macro, args):
  18 
  19    request = macro.request
  20 
  21    # get params
  22    if args:
  23       args = args.split(',')
  24    else:
  25       args = []
  26 
  27    kw = {}
  28    for arg in args :
  29       if '=' in arg:
  30          key, value = arg.split('=', 1)
  31          kw[str(key.strip())] = value.strip()
  32 
  33    try:
  34       maxTags = int( kw["maxTags"] )
  35    except (KeyError, ValueError):
  36       maxTags = 50
  37 
  38    try:
  39       autoCreate = kw["autoCreate"]
  40       if autoCreate == "true" or autoCreate == "True":
  41          autoCreate = True
  42       else:
  43          autoCreate = False
  44 
  45    except (KeyError):
  46       autoCreate = False
  47 
  48    #{level:hits , level:hits , ...}
  49    level = { 0 : 4 , 1 : 7 , 2 : 12 , 3 : 18 , 4 : 25 , 5 : 35 , 6 : 50 , 7 : 60 , 8 : 90 }
  50 
  51    args = r'regex:((\r)?\n----(\r)?\n[a-zA-Z])'
  52 
  53    # Search the pages
  54    query = search.QueryParser().parse_query(args)
  55    results = search.searchPages(request, query)
  56    pages = [hit.page_name for hit in results.hits]
  57 
  58    tags = []
  59 
  60    for page in pages:
  61       page = Page(request, page)
  62       if page.isStandardPage() and not page.isUnderlayPage():
  63          body = page.get_raw_body()
  64          match = re.search(r'----(\r)?\n(?P<tags>.*)(\r)?\n', body)
  65          match = match.group('tags')
  66          match = match.split(',')
  67          for tag in match:
  68             tags.insert(0, (str(tag)).strip())
  69 
  70    taglist = []
  71    taglist = list(frozenset(tags))
  72 
  73    def sort(t):
  74       return t[1]
  75 
  76    show = []
  77    for tag in taglist:
  78       show.append( (tag, tags.count(tag)) )
  79    show.sort(key=sort, reverse=True)
  80    show = show[0:maxTags]
  81    show.sort()
  82 
  83    html = []
  84 
  85    for tag in show:
  86 
  87       pagename = tag[0]
  88       hits = tag[1]
  89 
  90       # auto create tag page if not exist
  91       if autoCreate:
  92          page = Page(request, pagename)
  93          if page.isStandardPage(includeDeleted=False) == False and page.isUnderlayPage() == False:
  94 
  95             from MoinMoin.security import Permissions
  96             class SecurityPolicy(Permissions):
  97                def write(*args, **kw):
  98                   return True
  99                def save(*args, **kw):
 100                   return True
 101             request.user.may = SecurityPolicy(request.user)
 102 
 103             PageEditor(request, pagename).saveText(ur"[[FullSearch(regex:(-{4}(\r)?\n(.*)%s))]]"%(tag[0]), 0)
 104 
 105       #level0
 106       if hits < level[0]:
 107          html.append(u'<span  style="font-size:0.65em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 108       #level1
 109       elif hits < level[1]:
 110          html.append(u'<span  style="font-size:0.75em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 111       #level2
 112       elif hits < level[2]:
 113          html.append(u'<span  style="font-size:0.9em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 114       #level3
 115       elif hits < level[3]:
 116          html.append(u'<span  style="font-size:1.0em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 117       #level4
 118       elif hits < level[4]:
 119          html.append(u'<span  style="font-size:1.05em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 120       #level5
 121       elif hits < level[5]:
 122          html.append(u'<span  style="font-size:1.1em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 123       #level6
 124       elif hits < level[6]:
 125          html.append(u'<span  style="font-size:1.15em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 126       #level7
 127       elif hits < level[7]:
 128          html.append(u'<span  style="font-size:1.2em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 129       #level8
 130       elif hits < level[8]:
 131          html.append(u'<span  style="font-size:1.25em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 132       #level9
 133       else:
 134          html.append(u'<span  style="font-size:1.3em;"><a href="%s"> %s</a></span>'%(pagename, tag[0]))
 135 
 136    return ''.join(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.
  • [get | view] (2007-02-27 13:19:45, 4.1 KB) [[attachment:TagCloud.py]]
  • [get | view] (2008-12-03 08:54:44, 4.7 KB) [[attachment:TagCloud_utf8.py]]
 All files | Selected Files: delete move to page copy to page

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