Attachment 'Columns.py'
Download 1 #format python
2 """
3 MoinMoin macro to specify columns
4 Columns.py
5 ark 06/14/04 0.1
6
7 ***********
8 by Antti Kuntsi <antti@kuntsi.com>
9
10 As far as the author is concerned, this code is released to the public
11 domain, but some restrictions in the MoinMoin COPYING file may apply. Ask a
12 lawyer.
13 ***********
14 This code has not been tested exhaustively. Use at your own risk.
15 ***********
16
17 This macro generates containers, that can be used with CSS to create
18 columns.
19
20 Usage:
21 [[Columns(mode,name,keywordargs)]]
22
23 mode start, next or end.
24
25 name Name of the div id. <div id="name">
26
27 keywordargs Zero or more key="value" arguments for the HTML div
28 tag.
29
30 Examples:
31
32 [[Columns(start, leftcolumn)]]
33 This text is in the left column.
34 [[Columns(next, rightcolumn)]]
35 This text is in the right column.
36 [[Columns(end)]]
37
38 requires class definitions in CSS for the used column names, eg
39
40 #leftcolumn {
41 position: relative;
42 background: transparent;
43 float: left;
44 width: 45%;
45 border-right: 1px solid gray;
46 padding-left: 5%;
47 padding-right: 1%;
48 margin: 0px;
49 }
50
51 #rightcolumn {
52 position: relative;
53 background: transparent;
54 float: right;
55 width: 42%;
56 padding-right: 1%;
57 margin: 0px;
58 }
59
60
61 Demo. Try pasting the above examples into a MoinMoin sandbox page and the
62 current CSS.
63
64
65 """
66
67
68 import string
69 from MoinMoin import wikiutil, config
70
71
72 DEFAULT_KEYWORD_ARGS = {}
73
74 def execute(macro, args):
75 '''handle the Columns Macro. return the generated display HTML'''
76 # parse the arguments
77 if args==None:
78 args = ''
79 parseargs = _parse_arguments(args, ',', '"')
80 #print parseargs, '<BR>'
81 if len(parseargs)<1:
82 return '<p><strong class="error">Columns macro must have at least one argument!</strong></p>'
83 if parseargs[0] not in ("start", "next", "end"):
84 return '<p><strong class="error">Unknown mode "<i>%s</i>"!</strong></p>'%(parseargs[0])
85 mode = parseargs[0]
86
87 if mode != "end":
88 classname = parseargs[1].strip()
89 else:
90 classname=""
91 kwargs = _parse_keyword_arguments(parseargs[2:], DEFAULT_KEYWORD_ARGS)
92 # generate a string containing the keyword arguments
93 kwstring = ''
94 for key in kwargs.keys():
95 kwstring = '%s %s="%s"'%(kwstring, key, kwargs[key])
96 # generate a string specifying the location of the image
97 divtag=""
98 if mode in ("end", "next"):
99 divtag += '</div>'
100 if mode == "end":
101 divtag += '<br clear="both">'
102 if mode in ("start", "next"):
103 divtag += '<div id="%s"%s>'%(classname, kwstring)
104 # combine target location, image location, and keyword arguments into HTML IMG tag
105 result=[macro.formatter.paragraph(0),
106 macro.formatter.rawHTML(divtag),
107 macro.formatter.paragraph(1)
108 ]
109
110 return "".join(result)
111
112
113 def _parse_arguments(argstring, delimchar=',', quotechar='"'):
114 '''parse argstring into separate arguments originally separated by delimchar.
115 occurrences of delimchar found within quotechars are ignored.
116 This parser is not particularly elegant. Improvements are welcome.
117 jjk 03/28/01'''
118 # replace delimiters not in quotes with a special delimiter
119 delim2 = chr(0)
120 inquote = 0
121 for i1 in range(len(argstring)):
122 cchar = argstring[i1]
123 if cchar==quotechar:
124 inquote = not inquote
125 elif (not inquote) and cchar==delimchar:
126 argstring = argstring[:i1] + delim2 + argstring[i1+1:]
127 # split the argument string on the special delimiter, and remove external quotes
128 args = string.split(argstring, delim2)
129 for i1 in range(len(args)):
130 arg = args[i1]
131 if arg[:1]==quotechar and arg[-1:]==quotechar:
132 args[i1] = arg[1:-1]
133 return args
134
135
136 def _parse_keyword_arguments(raw_kw_args=[], default={}):
137 '''given a list of 'key=value' and/or 'key="value"',
138 answer a dictionary of {key:value}
139 jjk 03/28/01'''
140 kwargs = {}
141 kwargs.update(default)
142 for keyvalstr in raw_kw_args:
143 keyval = string.split(keyvalstr, '=')
144 if len(keyval)==0:
145 continue
146 elif len(keyval)==1:
147 key = keyval[0]
148 value = ''
149 elif len(keyval)==2:
150 key = keyval[0]
151 value = keyval[1]
152 elif len(keyval)>2:
153 key = keyval[0]
154 value = string.join(keyval[1:],'=')
155 if value[:1]=='"':
156 value = value[1:]
157 if value[-1:]=='"':
158 value = value[:-1]
159 kwargs[string.lower(key)] = value
160 return kwargs
161
162
163 def _is_URL(aString):
164 '''answer true if aString is a URL.
165 The method used here is pretty dumb. Improvements are welcome.
166 jjk 03/28/01'''
167 return string.find(aString, '://')>0
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.