Attachment 'Color2-1.6.py'

Download

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

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