1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinForms library 4 5 @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin import wikiutil 10 from StringIO import StringIO 11 from MoinSupport import * 12 import re 13 14 __version__ = "0.1" 15 16 # Common formatting functions. 17 18 def formatForm(text, request, fmt, attrs=None, write=None): 19 20 """ 21 Format the given 'text' using the specified 'request' and formatter 'fmt'. 22 The optional 'attrs' can be used to control the presentation of the form. 23 24 If the 'write' parameter is specified, use it to write output; otherwise, 25 write output using the request. 26 """ 27 28 write = write or request.write 29 page = request.page 30 31 fields = getFields(get_form(request)) 32 33 querystr = attrs and attrs.has_key("action") and ("action=%s" % attrs["action"]) or None 34 35 write(fmt.rawHTML('<form method="post" action="%s">' % 36 escattr(page.url(request, querystr)) 37 )) 38 39 output = getFormOutput(text, fields) 40 write(formatText(output, request, fmt)) 41 42 write(fmt.rawHTML('</form>')) 43 44 def getFormOutput(text, fields): 45 46 """ 47 Combine regions found in the given 'text' and then return them as a single 48 block. The reason for doing this, as opposed to just passing each region to 49 a suitable parser for formatting, is that form sections may break up 50 regions, and such sections may not define separate subregions but instead 51 act as a means of conditional inclusion of text into an outer region. 52 53 The given 'fields' are used to populate fields provided in forms and to 54 control whether sections are populated or not. 55 """ 56 57 output = [] 58 section = fields 59 60 for region in getRegions(text, True): 61 format, attributes, body, header, close = getFragmentFromRegion(region) 62 63 # NOTE: Need to adjust FormField macros to use hierarchical names. 64 65 # Include bare regions as they are. 66 67 if format is None: 68 output.append(region) 69 70 # Include form sections only if fields exist for those sections. 71 72 elif format == "form": 73 section_name = attributes.get("section") 74 if section_name and section.has_key(section_name): 75 output.append(header) 76 output.append(getFormOutput(body, section[section_name])) 77 output.append(close) 78 79 # Inspect and include other regions. 80 81 else: 82 output.append(header) 83 output.append(getFormOutput(body, section)) 84 output.append(close) 85 86 return "".join(output) 87 88 def getFields(d): 89 90 """ 91 Return the form fields hierarchy for the given dictionary 'd'. 92 """ 93 94 fields = {} 95 96 for key, value in d.items(): 97 98 # Reproduce the original hierarchy of the fields. 99 100 section = fields 101 parts = key.split("/") 102 103 for part in parts[:-1]: 104 try: 105 name, index = part.split("$", 1) 106 index = int(index) 107 except ValueError: 108 name, index = part, None 109 110 if not section.has_key(name): 111 section[name] = {} 112 113 if not section[name].has_key(index): 114 section[name][index] = {} 115 116 section = section[name][index] 117 118 section[parts[-1]] = value 119 120 return fields 121 122 def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): 123 124 """ 125 Format the given 'text' using the specified 'request' for the given output 126 'mimetype'. 127 128 The optional 'attrs' can be used to control the presentation of the form. 129 130 If the 'write' parameter is specified, use it to write output; otherwise, 131 write output using the request. 132 """ 133 134 write = write or request.write 135 136 if mimetype == "text/html": 137 write('<html>') 138 write('<body>') 139 fmt = request.html_formatter 140 fmt.setPage(request.page) 141 formatForm(text, request, fmt, attrs, write) 142 write('</body>') 143 write('</html>') 144 145 # vim: tabstop=4 expandtab shiftwidth=4