Attachment 'WordCount-0.2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - WordCount Macro
   4 
   5     @copyright: 2002 by Jürgen Hermann <jh@web.de>
   6     @license: GNU GPL, see COPYING for details.
   7 """
   8 
   9 
  10 Dependencies = ["pages"]
  11 
  12 from MoinMoin.Page import Page
  13 from MoinMoin.action import LikePages
  14 import string, re
  15 
  16 def execute(macro, args):
  17     targetLength = 0
  18     thisPage = macro.request.getPathinfo()[1:]
  19     # deal with arguments
  20     if ((args==None) or (args=="")):
  21         pagenames_raw = [thisPage]
  22         targetLength = 0
  23     else:
  24         argv = string.split(args,",")
  25         if (len(argv)>1):
  26            try:
  27                targetLength = int(argv[-1])
  28                pagenames_raw = argv[:-1]
  29            except:
  30                targetLength = 0
  31                pagenames_raw = argv
  32         else:
  33            arg0 = argv[0]
  34            try:
  35                targetLength = int(arg0)
  36                pagenames_raw = [thisPage]
  37            except:
  38                targetLength = 0
  39                pagenames_raw = [arg0]
  40 
  41     pagenames = []
  42     for pagename in pagenames_raw:
  43         if (pagename == "subpages"):
  44             allPages = macro.request.rootpage.getPageList(user='', exists='')
  45             matches = LikePages.wikiMatches(thisPage,allPages)
  46 	    for potentialPage in  matches[2].keys():
  47                 if (matches[2][potentialPage]==4):
  48                     pagenames.append(potentialPage)
  49         else:
  50             pagenames.append(pagename)
  51 
  52     sepCount = 0
  53     separator = re.compile(r"[' ,\.;:\-()]+")
  54 
  55     for pagename in pagenames:
  56         page = Page(macro.request, pagename)
  57         body = page.get_raw_body()
  58         sepCount += len(separator.findall(body))
  59 
  60     result = []
  61     result.append(macro.formatter.paragraph(1))
  62     result.append("Word count for " + string.join(pagenames," ") + " is ")
  63     result.append(macro.formatter.strong(1))
  64     result.append(macro.formatter.text(str(sepCount)))
  65     result.append(macro.formatter.strong(0))
  66     if (targetLength):
  67         delta = sepCount - targetLength
  68         message = " exactly"
  69         if (delta > 0):
  70             message = macro.formatter.sup(1) + str(delta) + macro.formatter.sup(0) + " over count" 
  71         elif (delta <0):
  72             message = macro.formatter.sub(1) + str(abs(delta)) + macro.formatter.sub(0) + " to write"
  73 
  74         result.append(" (target is " + str(targetLength) + ", " + message + ")")
  75     result.append(macro.formatter.paragraph(0))
  76     
  77     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] (2005-02-19 16:10:42, 2.4 KB) [[attachment:WordCount-0.2.py]]
  • [get | view] (2005-02-17 15:43:36, 2.0 KB) [[attachment:WordCount.py]]
 All files | Selected Files: delete move to page copy to page

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