Attachment 'WordCount.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - WordCount Macro
4
5 @copyright: 2004 by Walter Aprile (walter@raingod.com)
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 ## This is my first macro!
10 ## ... and it certainly shows ...
11 ##
12 ## Usage: [[WordCount]] display word count of the current page
13 ## [[WordCount(SummerInSiam)]] display word count of page SummerInSiam
14 ## [[WordCount(FrontPage, 500)]] display word count of FrontPage,
15 ## plus a message about the difference between the current world count
16 ## and the target world count of 500
17
18
19 Dependencies = ["pages"]
20
21 from MoinMoin.Page import Page
22 import string, re
23
24 def CountWords(Text):
25 total = 0
26 separator = re.compile(r"[' ,\.;:\-()]+")
27 sepCount = len(separator.findall(line))
28 return sepCount
29
30 def execute(macro, args):
31 targetLength = 0
32 argv = string.split(args,",")
33 # get list of pages and their objects
34 if (args!=None):
35 pagename = argv[0]
36 if (len(argv)>1):
37 targetLength = int(argv[1])
38 else:
39 pagename = macro.request.getPathinfo()[1:]
40 page = Page(macro.request, pagename)
41 body = page.get_raw_body()
42
43 separator = re.compile(r"[' ,\.;:\-()]+")
44 sepCount = len(separator.findall(body))
45
46 result = []
47 result.append(macro.formatter.paragraph(1))
48 result.append("Word count for " + pagename + " is ")
49 result.append(macro.formatter.strong(1))
50 result.append(macro.formatter.text(str(sepCount)))
51 result.append(macro.formatter.strong(0))
52 if (targetLength):
53 delta = sepCount - targetLength
54 message = " exactly"
55 if (delta > 0):
56 message = macro.formatter.sup(1) + str(delta) + macro.formatter.sup(0) + " over count"
57 elif (delta <0):
58 message = macro.formatter.sub(1) + str(abs(delta)) + macro.formatter.sub(0) + " to write"
59
60 result.append(" (target is " + str(targetLength) + ", " + message + ")")
61 result.append(macro.formatter.paragraph(0))
62
63 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.You are not allowed to attach a file to this page.