Attachment 'State_macro.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - State macro
4 This macro create an clickable icon on the page content.
5 On clicking the image, an menu opens.
6 By selecting an other icon at the menu, the clicked image would be changed.
7
8 @copyright: 2007 by Richard Flieger
9 @license: GNU GPL, see COPYING for details.
10
11 ----
12
13 Purpose:
14
15 This macro is used to create todo-lists.
16
17 Functions:
18
19 * no identifier needet
20 * I18N support
21 * 2-klick editing
22
23 Usage:
24
25 [[State]] -> without arguments
26 [[State()]] -> with empty arguments
27 [[State( )]] -> with spaces as arguments
28 [[State( UnknownState )]] -> with wrong state
29
30 [[State(TODO)]] -> upper case
31 [[State( TODO )]] -> upper case and spaces
32 [[State(todo)]] -> lower case
33 [[State( todo )]] -> lower case and spaces
34
35 Examples:
36
37 All states are open/todo, without arguments
38 ---->8--------------------------
39
40 [[State]] buy apples
41
42 [[State]] looking for a present
43
44 [[State]] write a letter
45
46 ----8<-------------------------
47
48 A little game
49 ---->8--------------------------
50 = Connect Four =
51
52 (./) Player 1
53
54 {X} Player 2
55
56 || [[State]] || [[State]] || [[State]] || [[State]] ||
57 || [[State]] || [[State]] || [[State]] || [[State]] ||
58 || [[State]] || [[State]] || [[State]] || [[State]] ||
59 || [[State]] || [[State]] || [[State]] || [[State]] ||
60
61 Modification history:
62
63 2007-06-25: first release
64
65 """
66
67 from MoinMoin import config
68 from MoinMoin.Page import Page
69 import re
70
71 # the statelist has to be the same as the statelist of the macro
72 statelist = {"todo" : "/!\\",
73 "done" : "(./)",
74 "aborted" : "{X}",
75 "inprogress" : "(!)",
76 "fun" : ":)"}
77
78 Defaultstate = "todo"
79
80 Dependencies = ["pages", "user"]
81
82 class State:
83
84 Dependencies = Dependencies
85
86 def __init__(self, macro, args):
87 self.macro = macro
88 self.request = macro.request
89 self.formatter = macro.formatter
90 self.state = args
91
92 def getArgs(self, string):
93 if not string:
94 return ["", ""]
95 args = [s.strip() for s in string.split( ',' )]
96 return args
97
98 def renderInPage(self):
99 fmt = self.formatter
100 state = str( self.state )
101 self.pagename = self.request.page.page_name
102 state = validState( state )
103 smileycode = getSmileyCode( state )
104 smileys = config.smileys.items()
105 iconimg = ""
106 for s in smileys:
107 if smileycode == s[0]:
108 iconimg = fmt.smiley( smileycode )
109 break
110 smileys.sort()
111 rev = self.getReversion()
112 id = self.getID()
113 html = u""
114 permission = self.request.user.may.write( self.pagename )
115 if permission:
116 html += getHTML( state, iconimg, id, rev)
117 else:
118 html += iconimg
119 return u''.join(html)
120
121 def setID( self, id ):
122 self.request.stateID = id
123
124 def getID(self):
125 try:
126 macroid = self.request.stateID
127 except:
128 macroid = 1
129 self.setID( macroid + 1 )
130 return macroid
131
132 def getReversion(self):
133 page = Page( self.request, self.pagename )
134 rev = page.get_real_rev()
135 return str(rev)
136
137 def getHTML( old, iconimg, id, rev ):
138 html = u"""<a href="?action=state&old=%(old)s&id=%(id)s&rev=%(rev)s">%(iconimg)s</a>""" % { "old" : old.upper(), "id" : id, "rev" : rev, "iconimg" : iconimg }
139 return html
140
141 def getSmileyCode( state ):
142 smileyname = statelist.get(state.lower(), statelist.get( Defaultstate ))
143 return smileyname
144
145 def parsText( regex, source ):
146 source = source.encode( "iso-8859-1" )
147 variable = re.search( regex, source )
148 if variable != None:
149 variable = variable.group( 1 )
150 value = variable.decode( "iso-8859-1" )
151 else:
152 return 0
153 return value
154
155 def validState( state = "todo" ):
156 if state and ( type( state ) == str ):
157 state = state.strip()
158 if state.lower() in statelist:
159 return state.lower()
160 return Defaultstate.lower()
161
162 def execute(macro, args):
163 return State(macro, args).renderInPage()
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.