Attachment 'TagCloud_utf8.py'

Download

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