paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinForms library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinMoin import wikiutil |
paul@0 | 10 | from MoinSupport import * |
paul@0 | 11 | import re |
paul@0 | 12 | |
paul@0 | 13 | __version__ = "0.1" |
paul@0 | 14 | |
paul@1 | 15 | form_field_regexp_str = r"<<FormField\((.*?)\)>>" |
paul@1 | 16 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 17 | |
paul@0 | 18 | # Common formatting functions. |
paul@0 | 19 | |
paul@0 | 20 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@0 | 21 | |
paul@0 | 22 | """ |
paul@0 | 23 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@0 | 24 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 25 | |
paul@0 | 26 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 27 | write output using the request. |
paul@0 | 28 | """ |
paul@0 | 29 | |
paul@0 | 30 | write = write or request.write |
paul@0 | 31 | page = request.page |
paul@0 | 32 | |
paul@0 | 33 | fields = getFields(get_form(request)) |
paul@0 | 34 | |
paul@1 | 35 | queryparams = [] |
paul@1 | 36 | |
paul@1 | 37 | for argname in ["fragment", "action"]: |
paul@1 | 38 | if attrs and attrs.has_key(argname): |
paul@1 | 39 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@1 | 40 | |
paul@1 | 41 | querystr = "&".join(queryparams) |
paul@0 | 42 | |
paul@0 | 43 | write(fmt.rawHTML('<form method="post" action="%s">' % |
paul@0 | 44 | escattr(page.url(request, querystr)) |
paul@0 | 45 | )) |
paul@0 | 46 | |
paul@0 | 47 | output = getFormOutput(text, fields) |
paul@0 | 48 | write(formatText(output, request, fmt)) |
paul@0 | 49 | |
paul@0 | 50 | write(fmt.rawHTML('</form>')) |
paul@0 | 51 | |
paul@0 | 52 | def getFormOutput(text, fields): |
paul@0 | 53 | |
paul@0 | 54 | """ |
paul@0 | 55 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 56 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 57 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 58 | regions, and such sections may not define separate subregions but instead |
paul@0 | 59 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 60 | |
paul@0 | 61 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 62 | control whether sections are populated or not. |
paul@0 | 63 | """ |
paul@0 | 64 | |
paul@0 | 65 | output = [] |
paul@0 | 66 | section = fields |
paul@0 | 67 | |
paul@0 | 68 | for region in getRegions(text, True): |
paul@0 | 69 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 70 | |
paul@0 | 71 | # Include bare regions as they are. |
paul@0 | 72 | |
paul@0 | 73 | if format is None: |
paul@0 | 74 | output.append(region) |
paul@0 | 75 | |
paul@0 | 76 | # Include form sections only if fields exist for those sections. |
paul@0 | 77 | |
paul@0 | 78 | elif format == "form": |
paul@0 | 79 | section_name = attributes.get("section") |
paul@0 | 80 | if section_name and section.has_key(section_name): |
paul@1 | 81 | |
paul@1 | 82 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 83 | |
paul@1 | 84 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@1 | 85 | |
paul@1 | 86 | # Adjust FormField macros to use hierarchical names. |
paul@1 | 87 | |
paul@1 | 88 | adjusted_body = adjustFormFields(body, section_name, index) |
paul@1 | 89 | output.append(getFormOutput(adjusted_body, element)) |
paul@0 | 90 | |
paul@0 | 91 | # Inspect and include other regions. |
paul@0 | 92 | |
paul@0 | 93 | else: |
paul@0 | 94 | output.append(header) |
paul@0 | 95 | output.append(getFormOutput(body, section)) |
paul@0 | 96 | output.append(close) |
paul@0 | 97 | |
paul@0 | 98 | return "".join(output) |
paul@0 | 99 | |
paul@1 | 100 | def adjustFormFields(body, section_name, index): |
paul@1 | 101 | |
paul@1 | 102 | """ |
paul@1 | 103 | Return a version of the 'body' with the names in FormField macros updated to |
paul@1 | 104 | incorporate the given 'section_name' and 'index'. |
paul@1 | 105 | """ |
paul@1 | 106 | |
paul@1 | 107 | result = [] |
paul@1 | 108 | in_macro = False |
paul@1 | 109 | |
paul@1 | 110 | for match in form_field_regexp.split(body): |
paul@1 | 111 | if not in_macro: |
paul@1 | 112 | result.append(match) |
paul@1 | 113 | else: |
paul@1 | 114 | result.append("<<FormField(%s)>>" % ",".join( |
paul@1 | 115 | adjustMacroArguments(parseMacroArguments(match), section_name, index) |
paul@1 | 116 | )) |
paul@1 | 117 | |
paul@1 | 118 | in_macro = not in_macro |
paul@1 | 119 | |
paul@1 | 120 | return "".join(result) |
paul@1 | 121 | |
paul@1 | 122 | def adjustMacroArguments(args, section_name, index): |
paul@0 | 123 | |
paul@0 | 124 | """ |
paul@1 | 125 | Adjust the given 'args' so that the path incorporates the given |
paul@1 | 126 | 'section_name' and 'index', returning a new list containing the revised |
paul@1 | 127 | path and remaining arguments. |
paul@1 | 128 | """ |
paul@1 | 129 | |
paul@1 | 130 | result = [] |
paul@1 | 131 | path = None |
paul@1 | 132 | |
paul@1 | 133 | for arg in args: |
paul@1 | 134 | if arg.startswith("path="): |
paul@1 | 135 | path = arg[5:] |
paul@1 | 136 | else: |
paul@1 | 137 | result.append(arg) |
paul@1 | 138 | |
paul@1 | 139 | qualified = "%s%s$%s" % (path and ("%s/" % path) or "", section_name, index) |
paul@1 | 140 | result.append("path=%s" % qualified) |
paul@1 | 141 | |
paul@1 | 142 | return result |
paul@1 | 143 | |
paul@1 | 144 | def parseMacroArguments(args): |
paul@1 | 145 | |
paul@1 | 146 | """ |
paul@1 | 147 | Interpret the arguments. |
paul@1 | 148 | NOTE: The argument parsing should really be more powerful in order to |
paul@1 | 149 | NOTE: support labels. |
paul@1 | 150 | """ |
paul@1 | 151 | |
paul@1 | 152 | try: |
paul@1 | 153 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 154 | except AttributeError: |
paul@1 | 155 | parsed_args = args.split(",") |
paul@1 | 156 | |
paul@1 | 157 | return [arg for arg in parsed_args if arg] |
paul@1 | 158 | |
paul@1 | 159 | def getFields(d, remove=False): |
paul@1 | 160 | |
paul@1 | 161 | """ |
paul@1 | 162 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 163 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 164 | the fields from 'd'. |
paul@0 | 165 | """ |
paul@0 | 166 | |
paul@0 | 167 | fields = {} |
paul@0 | 168 | |
paul@0 | 169 | for key, value in d.items(): |
paul@0 | 170 | |
paul@1 | 171 | # Detect modifying fields. |
paul@1 | 172 | |
paul@1 | 173 | if key.find("=") != -1: |
paul@1 | 174 | fields[key] = value |
paul@1 | 175 | if remove: |
paul@1 | 176 | del d[key] |
paul@1 | 177 | continue |
paul@1 | 178 | |
paul@0 | 179 | # Reproduce the original hierarchy of the fields. |
paul@0 | 180 | |
paul@0 | 181 | section = fields |
paul@1 | 182 | parts = getPathDetails(key) |
paul@0 | 183 | |
paul@1 | 184 | for name, index in parts[:-1]: |
paul@1 | 185 | |
paul@1 | 186 | # Add an entry for instances of the section. |
paul@0 | 187 | |
paul@0 | 188 | if not section.has_key(name): |
paul@0 | 189 | section[name] = {} |
paul@0 | 190 | |
paul@1 | 191 | # Add an entry for the specific instance of the section. |
paul@1 | 192 | |
paul@0 | 193 | if not section[name].has_key(index): |
paul@0 | 194 | section[name][index] = {} |
paul@0 | 195 | |
paul@0 | 196 | section = section[name][index] |
paul@0 | 197 | |
paul@1 | 198 | section[parts[-1][0]] = value |
paul@1 | 199 | |
paul@1 | 200 | if remove: |
paul@1 | 201 | del d[key] |
paul@0 | 202 | |
paul@0 | 203 | return fields |
paul@0 | 204 | |
paul@1 | 205 | def getPathDetails(path): |
paul@1 | 206 | parts = [] |
paul@1 | 207 | |
paul@1 | 208 | for part in path.split("/"): |
paul@1 | 209 | try: |
paul@1 | 210 | name, index = part.split("$", 1) |
paul@1 | 211 | index = int(index) |
paul@1 | 212 | except ValueError: |
paul@1 | 213 | name, index = part, None |
paul@1 | 214 | |
paul@1 | 215 | parts.append((name, index)) |
paul@1 | 216 | |
paul@1 | 217 | return parts |
paul@1 | 218 | |
paul@1 | 219 | def getSectionForPath(path, fields): |
paul@1 | 220 | |
paul@1 | 221 | """ |
paul@1 | 222 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 223 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 224 | parent section contains the referenced section, where name is the name of |
paul@1 | 225 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 226 | specific section instance within the named section. |
paul@1 | 227 | """ |
paul@1 | 228 | |
paul@1 | 229 | parts = getPathDetails(path) |
paul@1 | 230 | section = fields |
paul@1 | 231 | |
paul@1 | 232 | for name, index in parts[:-1]: |
paul@1 | 233 | section = fields[name][index] |
paul@1 | 234 | |
paul@1 | 235 | return section, parts[-1] |
paul@1 | 236 | |
paul@1 | 237 | def getSectionElements(section_elements): |
paul@1 | 238 | |
paul@1 | 239 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 240 | |
paul@1 | 241 | keys = map(int, section_elements.keys()) |
paul@1 | 242 | keys.sort() |
paul@1 | 243 | |
paul@1 | 244 | elements = [] |
paul@1 | 245 | |
paul@1 | 246 | for key in keys: |
paul@1 | 247 | elements.append(section_elements[key]) |
paul@1 | 248 | |
paul@1 | 249 | return elements |
paul@1 | 250 | |
paul@0 | 251 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 252 | |
paul@0 | 253 | """ |
paul@0 | 254 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 255 | 'mimetype'. |
paul@0 | 256 | |
paul@0 | 257 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 258 | |
paul@0 | 259 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 260 | write output using the request. |
paul@0 | 261 | """ |
paul@0 | 262 | |
paul@0 | 263 | write = write or request.write |
paul@0 | 264 | |
paul@0 | 265 | if mimetype == "text/html": |
paul@0 | 266 | write('<html>') |
paul@0 | 267 | write('<body>') |
paul@0 | 268 | fmt = request.html_formatter |
paul@0 | 269 | fmt.setPage(request.page) |
paul@0 | 270 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 271 | write('</body>') |
paul@0 | 272 | write('</html>') |
paul@0 | 273 | |
paul@0 | 274 | # vim: tabstop=4 expandtab shiftwidth=4 |