1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinForms.py Thu Nov 29 00:53:51 2012 +0100
1.3 @@ -0,0 +1,145 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinForms library
1.7 +
1.8 + @copyright: 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin import wikiutil
1.13 +from StringIO import StringIO
1.14 +from MoinSupport import *
1.15 +import re
1.16 +
1.17 +__version__ = "0.1"
1.18 +
1.19 +# Common formatting functions.
1.20 +
1.21 +def formatForm(text, request, fmt, attrs=None, write=None):
1.22 +
1.23 + """
1.24 + Format the given 'text' using the specified 'request' and formatter 'fmt'.
1.25 + The optional 'attrs' can be used to control the presentation of the form.
1.26 +
1.27 + If the 'write' parameter is specified, use it to write output; otherwise,
1.28 + write output using the request.
1.29 + """
1.30 +
1.31 + write = write or request.write
1.32 + page = request.page
1.33 +
1.34 + fields = getFields(get_form(request))
1.35 +
1.36 + querystr = attrs and attrs.has_key("action") and ("action=%s" % attrs["action"]) or None
1.37 +
1.38 + write(fmt.rawHTML('<form method="post" action="%s">' %
1.39 + escattr(page.url(request, querystr))
1.40 + ))
1.41 +
1.42 + output = getFormOutput(text, fields)
1.43 + write(formatText(output, request, fmt))
1.44 +
1.45 + write(fmt.rawHTML('</form>'))
1.46 +
1.47 +def getFormOutput(text, fields):
1.48 +
1.49 + """
1.50 + Combine regions found in the given 'text' and then return them as a single
1.51 + block. The reason for doing this, as opposed to just passing each region to
1.52 + a suitable parser for formatting, is that form sections may break up
1.53 + regions, and such sections may not define separate subregions but instead
1.54 + act as a means of conditional inclusion of text into an outer region.
1.55 +
1.56 + The given 'fields' are used to populate fields provided in forms and to
1.57 + control whether sections are populated or not.
1.58 + """
1.59 +
1.60 + output = []
1.61 + section = fields
1.62 +
1.63 + for region in getRegions(text, True):
1.64 + format, attributes, body, header, close = getFragmentFromRegion(region)
1.65 +
1.66 + # NOTE: Need to adjust FormField macros to use hierarchical names.
1.67 +
1.68 + # Include bare regions as they are.
1.69 +
1.70 + if format is None:
1.71 + output.append(region)
1.72 +
1.73 + # Include form sections only if fields exist for those sections.
1.74 +
1.75 + elif format == "form":
1.76 + section_name = attributes.get("section")
1.77 + if section_name and section.has_key(section_name):
1.78 + output.append(header)
1.79 + output.append(getFormOutput(body, section[section_name]))
1.80 + output.append(close)
1.81 +
1.82 + # Inspect and include other regions.
1.83 +
1.84 + else:
1.85 + output.append(header)
1.86 + output.append(getFormOutput(body, section))
1.87 + output.append(close)
1.88 +
1.89 + return "".join(output)
1.90 +
1.91 +def getFields(d):
1.92 +
1.93 + """
1.94 + Return the form fields hierarchy for the given dictionary 'd'.
1.95 + """
1.96 +
1.97 + fields = {}
1.98 +
1.99 + for key, value in d.items():
1.100 +
1.101 + # Reproduce the original hierarchy of the fields.
1.102 +
1.103 + section = fields
1.104 + parts = key.split("/")
1.105 +
1.106 + for part in parts[:-1]:
1.107 + try:
1.108 + name, index = part.split("$", 1)
1.109 + index = int(index)
1.110 + except ValueError:
1.111 + name, index = part, None
1.112 +
1.113 + if not section.has_key(name):
1.114 + section[name] = {}
1.115 +
1.116 + if not section[name].has_key(index):
1.117 + section[name][index] = {}
1.118 +
1.119 + section = section[name][index]
1.120 +
1.121 + section[parts[-1]] = value
1.122 +
1.123 + return fields
1.124 +
1.125 +def formatFormForOutputType(text, request, mimetype, attrs=None, write=None):
1.126 +
1.127 + """
1.128 + Format the given 'text' using the specified 'request' for the given output
1.129 + 'mimetype'.
1.130 +
1.131 + The optional 'attrs' can be used to control the presentation of the form.
1.132 +
1.133 + If the 'write' parameter is specified, use it to write output; otherwise,
1.134 + write output using the request.
1.135 + """
1.136 +
1.137 + write = write or request.write
1.138 +
1.139 + if mimetype == "text/html":
1.140 + write('<html>')
1.141 + write('<body>')
1.142 + fmt = request.html_formatter
1.143 + fmt.setPage(request.page)
1.144 + formatForm(text, request, fmt, attrs, write)
1.145 + write('</body>')
1.146 + write('</html>')
1.147 +
1.148 +# vim: tabstop=4 expandtab shiftwidth=4