Attachment 'CategoryCloud-0.3.1.py'

Download

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