Attachment 'Color2.py'
Download 1 """
2 MoinMoin - Color2 Macro
3
4 @copyright: 2006 by Clif Kussmaul <clif@kussmaul.org>
5 @license: GNU GPL, see COPYING for details
6
7 Usage: [[Color2(color,bgcolor,font,text)]]
8 [[Color2(color,bgcolor,text)]]
9 [[Color2(color,text)]]
10
11 History:
12 - originally based on Color Macro
13 Copyright (c) 2002 by Markus Gritsch <gritsch@iue.tuwien.ac.at>
14 """
15 # wiki, string, StringIO used by formattext
16 from MoinMoin.parser import wiki
17 import re, string, StringIO
18
19 """
20 # pattern is color, optional color, optional font, and text
21 <pattern> := <color><sep> (<color><sep1>)? (<font><sep1>)? <text>
22 # color is rgb(nn,nn,nn), #hhhhhh, or colorname
23 <color> := rgb\([^)]+\) | [#a-zA-Z0-9_]*
24 # separator is "," or ":", but first separator must be used consistently
25 <sep> := [,:]
26 <sep1> := <previous sep>
27 # font is anything except a separator
28 <font> := [^,:]+
29 <text> := .+
30 """
31 pat = re.compile(r'\s*(rgb\([^)]+\)|[#a-zA-Z0-9_]*)\s*([,:])' +
32 r'(\s*(rgb\([^)]+\)|[#a-zA-Z0-9_]*)\s*\2)?' +
33 r'\s*(([^,:]+)\s*\2)?' +
34 r'\s*(.+)')
35
36 def formattext(macro, text):
37 # copied verbatim from MiniPage Macro by Reimar Bauer
38 text=string.replace(string.join(text,''),'\\n','\n')
39 out=StringIO.StringIO()
40 macro.request.redirect(out)
41 wikiizer = wiki.Parser(text,macro.request)
42 wikiizer.format(macro.formatter)
43 result=out.getvalue()
44 macro.request.redirect()
45 del out
46 return(result)
47
48 def execute(macro, args):
49 f = macro.formatter
50 vals = None
51 if args:
52 result = pat.match(args)
53 if result:
54 # be sure group arguments match unittest() below
55 vals = result.group(1,4,6,7)
56 if not vals:
57 return f.strong(1) + \
58 f.text('Color2 Examples: ') + \
59 f.text('[[Color2(red,blue,18px courier,Hello World!)]], ') + \
60 f.text('[[Color2(#8844AA:Hello World!)]]') + \
61 f.strong(0) + f.linebreak(0) + \
62 f.text(' - specifies color, background color, and/or font') + \
63 f.text(' (can be separated with "," or ":")')
64 style = ''
65 if vals[0]:
66 style += 'color:%s; ' % vals[0]
67 if vals[1]:
68 style += 'background-color:%s; ' % vals[1]
69 if vals[2]:
70 style += 'font:%s; ' % vals[2]
71 text = formattext(macro, vals[3])
72 # discard <p> tag that screws up background color
73 text = re.sub('<p class="line\d*">', '', text).strip()
74 return f.rawHTML('<span style="%s">' % style) + text + f.rawHTML('</span>')
75
76
77 def execute0(macro, args):
78 if args:
79 # use ',' or ':' as arg separator, whichever comes first
80 p1 = args.find(',')
81 p2 = args.find(':')
82 if p1 < 0 : p1 = 10000
83 if p2 < 0 : p2 = 10000
84 if p1 < p2 :
85 schar = ','
86 else:
87 schar = ':'
88 args = [arg.strip() for arg in args.split(schar)]
89 else:
90 args = []
91 argc = len(args)
92 f = macro.formatter
93 if argc <= 1:
94 return f.strong(1) + \
95 f.text('Examples: ') + \
96 f.text('[[Color2(red,blue,18px courier,Hello World!)]], ') + \
97 f.text('[[Color2(#8844AA:Hello World!)]]') + \
98 f.strong(0) + f.linebreak(0) + \
99 f.text(' - specifies color, background color, and/or font') + \
100 f.text(' (can be separated with "," or ":")')
101 style = ''
102 if argc > 1:
103 style += 'color:%s; ' % args[0]
104 if argc > 2:
105 style += 'background-color:%s; ' % args[1]
106 if argc > 3:
107 style += 'font:%s; ' % args[2]
108 text = formattext(macro, args[-1])
109 # discard <p> tag that screws up background color
110 text = re.sub('<p class="line\d*">', '', text)
111 return f.rawHTML('<span style="%s">' % style) + text.strip() + f.rawHTML('</span>')
112
113
114 def unittest():
115 testset = [
116 "red , s1:s1 s1",
117 "red : s1,s1 s1",
118 "#effeff,s3:s3 s3",
119 "#effeff:s4,s4 s4",
120 "rgb(12,23,34),s5:s5 s5",
121 "rgb(12,23,34):s6,s6 s6",
122 "red,green, 18px courier,this is a test",
123 "#effeff,rgb(23,34,45),12px altona, this is another test",
124 ",red , s1:s1 s1",
125 ":red : s1,s1 s1",
126 ",,12px altona , s1:s1 s1",
127 " : : 12px altona : s1,s1 s1",
128 ]
129 for testval in testset:
130 print ":: " + testval
131 result = pat.match(testval)
132 if result:
133 # be sure group arguments match execute() above
134 print result.group(1,4,6,7)
135 else:
136 print result
137
138 # things that happen if file is invoked at command line
139 #unittest()
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.