00001 """
00002 MoinMoin - Color2 Macro
00003
00004 @copyright: 2006 by Clif Kussmaul <clif@kussmaul.org>
00005 2008 by Clif Kussmaul, Dave Hein (MoinMoin:DaveHein)
00006 @license: GNU GPL, see COPYING for details
00007
00008 Usage: [[Color2(color,bgcolor,font,text)]]
00009 [[Color2(color,bgcolor,text)]]
00010 [[Color2(color,text)]]
00011
00012 History:
00013 - 2008.01.25: [Moin 1.6] updated for Moin 1.6 by Dave Hein,
00014 no functional changes.
00015 - 2006: [Moin 1.5] written by Clif Kussmaul
00016 - originally based on Color Macro
00017 Copyright (c) 2002 by Markus Gritsch <gritsch@iue.tuwien.ac.at>
00018 """
00019
00020 from MoinMoin.parser import text_moin_wiki
00021 import re, string, StringIO
00022
00023 """
00024 # pattern is color, optional color, optional font, and text
00025 <pattern> := <color><sep> (<color><sep1>)? (<font><sep1>)? <text>
00026 # color is rgb(nn,nn,nn), #hhhhhh, or colorname
00027 <color> := rgb\([^)]+\) | [#a-zA-Z0-9_]*
00028 # separator is "," or ":", but first separator must be used consistently
00029 <sep> := [,:]
00030 <sep1> := <previous sep>
00031 # font is anything except a separator
00032 <font> := [^,:]+
00033 <text> := .+
00034 """
00035 pat = re.compile(r'\s*(rgb\([^)]+\)|[#a-zA-Z0-9_]*)\s*([,:])' +
00036 r'(\s*(rgb\([^)]+\)|[#a-zA-Z0-9_]*)\s*\2)?' +
00037 r'\s*(([^,:]+)\s*\2)?' +
00038 r'\s*(.+)')
00039
00040 def formattext(macro, text):
00041
00042 text=string.replace(string.join(text,''),'\\n','\n')
00043 out=StringIO.StringIO()
00044 macro.request.redirect(out)
00045 wikiizer = text_moin_wiki.Parser(text,macro.request,line_anchors=False)
00046 wikiizer.format(macro.formatter)
00047 result=out.getvalue()
00048 macro.request.redirect()
00049 del out
00050 return(result)
00051
00052 def execute(macro, args):
00053 f = macro.formatter
00054 vals = None
00055 if args:
00056 result = pat.match(args)
00057 if result:
00058
00059 vals = result.group(1,4,6,7)
00060 if not vals:
00061 return f.strong(1) + \
00062 f.text('Color2 Examples : ') + \
00063 f.text('[[Color2(red,blue,18px courier,Hello World!)]], ') + \
00064 f.text('[[Color2(#8844AA:Hello World!)]]') + \
00065 f.strong(0) + f.linebreak(0) + \
00066 f.text(' - specifies color, background color, and/or font') + \
00067 f.text(' (can be separated with "," or ":")')
00068 style = ''
00069 if vals[0]:
00070 style += 'color:%s; ' % vals[0]
00071 if vals[1]:
00072 style += 'background-color:%s; ' % vals[1]
00073 if vals[2]:
00074 style += 'font:%s; ' % vals[2]
00075 text = formattext(macro, vals[3])
00076
00077 text = re.sub('<p class="line\d*">', '', text).strip()
00078 return f.rawHTML('<span style="%s">' % style) + text + f.rawHTML('</span>')
00079
00080
00081 def execute0(macro, args):
00082 if args:
00083
00084 p1 = args.find(',')
00085 p2 = args.find(':')
00086 if p1 < 0 : p1 = 10000
00087 if p2 < 0 : p2 = 10000
00088 if p1 < p2 :
00089 schar = ','
00090 else:
00091 schar = ':'
00092 args = [arg.strip() for arg in args.split(schar)]
00093 else:
00094 args = []
00095 argc = len(args)
00096 f = macro.formatter
00097 if argc <= 1:
00098 return f.strong(1) + \
00099 f.text('Examples: ') + \
00100 f.text('[[Color2(red,blue,18px courier,Hello World!)]], ') + \
00101 f.text('[[Color2(#8844AA:Hello World!)]]') + \
00102 f.strong(0) + f.linebreak(0) + \
00103 f.text(' - specifies color, background color, and/or font') + \
00104 f.text(' (can be separated with "," or ":")')
00105 style = ''
00106 if argc > 1:
00107 style += 'color:%s; ' % args[0]
00108 if argc > 2:
00109 style += 'background-color:%s; ' % args[1]
00110 if argc > 3:
00111 style += 'font:%s; ' % args[2]
00112 text = formattext(macro, args[-1])
00113
00114 text = re.sub('<p class="line\d*">', '', text)
00115 return f.rawHTML('<span style="%s">' % style) + text.strip() + f.rawHTML('</span>')
00116
00117
00118 def unittest():
00119 testset = [
00120 "red , s1:s1 s1",
00121 "red : s1,s1 s1",
00122 "#effeff,s3:s3 s3",
00123 "#effeff:s4,s4 s4",
00124 "rgb(12,23,34),s5:s5 s5",
00125 "rgb(12,23,34):s6,s6 s6",
00126 "red,green, 18px courier,this is a test",
00127 "#effeff,rgb(23,34,45),12px altona, this is another test",
00128 ",red , s1:s1 s1",
00129 ":red : s1,s1 s1",
00130 ",,12px altona , s1:s1 s1",
00131 " : : 12px altona : s1,s1 s1",
00132 ]
00133 for testval in testset:
00134 print ":: " + testval
00135 result = pat.match(testval)
00136 if result:
00137
00138 print result.group(1,4,6,7)
00139 else:
00140 print result
00141
00142
00143
00144