Attachment 'CategoryCloud-0.2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 u"""
   3     MoinMoin - CategoryCloud macro Version 0.2 ALPHA
   4                Output a tag cloud of categry pages
   5 
   6     
   7     @copyright: 2009 by MarcelHäfner (http://moinmo.in/MarcelHäfner)
   8                 (this code is a bigger rewrite of the macro TagCloud by Christian Groh 
   9     @license: GNU GPL, see COPYING for details.
  10     
  11     @TODO:
  12      * The final output should be optimized and not using that much code
  13      * Use some better caching like the origin macro did
  14      * The regex for finding the "Category" should avoid FooCategory for counting
  15 
  16 """
  17 
  18 from MoinMoin.Page import Page
  19 import re
  20 
  21 Dependencies = [] #TODO: optimizing caching
  22 
  23 def macro_CategoryCloud(macro,maxTags=30,fontSize=0.65):
  24 
  25     #inital stuff
  26     request = macro.request
  27     fmt = macro.formatter
  28     _ = request.getText
  29     errorMsg = []
  30     html = []
  31     tags = []
  32     taglist = []
  33     show = []
  34     level = { 0 : 4 , 1 : 7 , 2 : 12 , 3 : 18 , 4 : 25 , 5 : 35 , 6 : 50 , 7 : 60 , 8 : 90 }     #{level:hits , level:hits , ...}
  35 
  36     #fetch all pages, except underlays
  37     pages = request.rootpage.getPageList(exists=1,include_underlay=False,)
  38     if not pages:
  39         errorMsg.append(fmt.div(True,css_class="error"))
  40         errorMsg.append(_("No pages exist or you have not enough rights to view them"))
  41         errorMsg.append(fmt.div(False))
  42         return ''.join(errorMsg)
  43 
  44     #find CategoryFoo tags and count them
  45     for page in pages:
  46         page = Page(request, page)
  47         if page.isStandardPage() and not page.isUnderlayPage():
  48             body = page.get_raw_body()
  49             #match = re.search(ur'(?P<all>Category(?P<category>\S+))', body)
  50             match = re.search(ur'----(\r)?\n(.*)(?P<all>Category(?P<category>\S+))', body) #TODO
  51             if match == None: 
  52                 continue
  53             match = match.group('category')
  54             match = match.split(',')
  55             for tag in match:
  56                 tags.insert(0, (str(tag.encode('utf8'))).strip())
  57     taglist = list(frozenset(tags))
  58 
  59     #sorting the taglist and output as show
  60     def sort(t):
  61         return t[1]
  62 
  63     for tag in taglist:
  64         show.append( (tag, tags.count(tag)) )
  65     show.sort(key=sort, reverse=True)
  66     show = show[0:maxTags]
  67     show.sort()
  68     
  69     #generate the cloud. TODO: code should be optimized
  70     html.append(fmt.div(True,css_class="PageCloud",style="display:inline;"))       
  71     for tag in show:
  72         pagename = "Category" + tag[0].decode('utf8')
  73         hits = tag[1]       
  74          #level0
  75         if hits < level[0]:
  76             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.0))
  77             html.append(fmt.pagelink(True,pagename))
  78             html.append(fmt.text(tag[0].decode('utf8')))
  79             html.append(fmt.pagelink(False))
  80             html.append(fmt.span(False))
  81             html.append(fmt.text(" "))
  82         #level1
  83         elif hits < level[1]:
  84             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.15))
  85             html.append(fmt.pagelink(True,pagename))
  86             html.append(fmt.text(tag[0].decode('utf8')))
  87             html.append(fmt.pagelink(False))
  88             html.append(fmt.span(False))
  89             html.append(fmt.text(" "))
  90         #level2
  91         elif hits < level[2]:
  92             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.25))
  93             html.append(fmt.pagelink(True,pagename))
  94             html.append(fmt.text(tag[0].decode('utf8')))
  95             html.append(fmt.pagelink(False))
  96             html.append(fmt.span(False))
  97             html.append(fmt.text(" "))
  98         #level3
  99         elif hits < level[3]:
 100             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.35))
 101             html.append(fmt.pagelink(True,pagename))
 102             html.append(fmt.text(tag[0].decode('utf8')))
 103             html.append(fmt.pagelink(False))
 104             html.append(fmt.span(False))
 105             html.append(fmt.text(" "))
 106         #level4
 107         elif hits < level[4]:
 108             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.45))
 109             html.append(fmt.pagelink(True,pagename))
 110             html.append(fmt.text(tag[0].decode('utf8')))
 111             html.append(fmt.pagelink(False))
 112             html.append(fmt.span(False))
 113             html.append(fmt.text(" "))
 114         #level5
 115         elif hits < level[5]:
 116             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.55))
 117             html.append(fmt.pagelink(True,pagename))
 118             html.append(fmt.text(tag[0].decode('utf8')))
 119             html.append(fmt.pagelink(False))
 120             html.append(fmt.span(False))
 121             html.append(fmt.text(" "))
 122         #level6
 123         elif hits < level[6]:
 124             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.65))
 125             html.append(fmt.pagelink(True,pagename))
 126             html.append(fmt.text(tag[0].decode('utf8')))
 127             html.append(fmt.pagelink(False))
 128             html.append(fmt.span(False))
 129             html.append(fmt.text(" "))
 130         #level7
 131         elif hits < level[7]:
 132             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.75))
 133             html.append(fmt.pagelink(True,pagename))
 134             html.append(fmt.text(tag[0].decode('utf8')))
 135             html.append(fmt.pagelink(False))
 136             html.append(fmt.span(False))
 137             html.append(fmt.text(" "))
 138         #level8
 139         elif hits < level[8]:
 140             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+0.85))
 141             html.append(fmt.pagelink(True,pagename))
 142             html.append(fmt.text(tag[0].decode('utf8')))
 143             html.append(fmt.pagelink(False))
 144             html.append(fmt.span(False))
 145             html.append(fmt.text(" "))
 146         #level9
 147         else:
 148             html.append(fmt.span(True,style="font-size:%sem;") % str(fontSize+1.05))
 149             html.append(fmt.pagelink(True,pagename))
 150             html.append(fmt.text(tag[0].decode('utf8')))
 151             html.append(fmt.pagelink(False))
 152             html.append(fmt.span(False))
 153             html.append(fmt.text(" "))
 154 
 155     #output    
 156     html.append(fmt.div(False))
 157     return ''.join(html)
 158     

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] (2009-10-20 22:28:04, 6.1 KB) [[attachment:CategoryCloud-0.2.py]]
  • [get | view] (2010-04-22 22:12:16, 6.1 KB) [[attachment:CategoryCloud-0.3.1.py]]
  • [get | view] (2011-06-24 10:44:55, 6.2 KB) [[attachment:CategoryCloud-0.3.2.py]]
  • [get | view] (2009-10-20 22:28:17, 43.0 KB) [[attachment:CategoryCloud_example.png]]
 All files | Selected Files: delete move to page copy to page

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