paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - FormField Macro |
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 | |
paul@0 | 12 | Dependencies = ['pages'] |
paul@0 | 13 | |
paul@0 | 14 | escape = wikiutil.escape |
paul@0 | 15 | |
paul@0 | 16 | # Macro functions. |
paul@0 | 17 | |
paul@0 | 18 | def execute(macro, args): |
paul@0 | 19 | |
paul@0 | 20 | """ |
paul@0 | 21 | Execute the 'macro' with the given 'args' to produce a form field element: |
paul@0 | 22 | |
paul@0 | 23 | * A field name |
paul@0 | 24 | * The WikiDict describing form fields |
paul@0 | 25 | |
paul@0 | 26 | The following optional named arguments are also supported: |
paul@0 | 27 | |
paul@0 | 28 | label=TEXT The label employed by button-like fields |
paul@0 | 29 | |
paul@0 | 30 | The nature of each field is described by a WikiDict entry for the given |
paul@0 | 31 | field name. |
paul@0 | 32 | """ |
paul@0 | 33 | |
paul@0 | 34 | request = macro.request |
paul@0 | 35 | fmt = macro.formatter |
paul@0 | 36 | page = fmt.page |
paul@0 | 37 | _ = request.getText |
paul@0 | 38 | |
paul@0 | 39 | # Interpret the arguments. |
paul@0 | 40 | # NOTE: The argument parsing should really be more powerful in order to |
paul@0 | 41 | # NOTE: support labels. |
paul@0 | 42 | |
paul@0 | 43 | try: |
paul@0 | 44 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 45 | except AttributeError: |
paul@0 | 46 | parsed_args = args.split(",") |
paul@0 | 47 | |
paul@0 | 48 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 49 | |
paul@0 | 50 | # Get special arguments. |
paul@0 | 51 | |
paul@0 | 52 | name = None |
paul@0 | 53 | dictpage = None |
paul@0 | 54 | label = None |
paul@0 | 55 | |
paul@0 | 56 | for arg in parsed_args: |
paul@0 | 57 | if arg.startswith("name="): |
paul@0 | 58 | name = arg[5:] |
paul@0 | 59 | |
paul@0 | 60 | elif arg.startswith("dict="): |
paul@0 | 61 | dictpage = arg[5:] |
paul@0 | 62 | |
paul@0 | 63 | elif arg.startswith("label="): |
paul@0 | 64 | label = arg[6:] |
paul@0 | 65 | |
paul@0 | 66 | elif name is None: |
paul@0 | 67 | name = arg |
paul@0 | 68 | |
paul@0 | 69 | elif dictpage is None: |
paul@0 | 70 | dictpage = arg |
paul@0 | 71 | |
paul@0 | 72 | if not name: |
paul@0 | 73 | return showError(_("No field name specified."), request) |
paul@0 | 74 | |
paul@0 | 75 | if not dictpage: |
paul@0 | 76 | return showError(_("No WikiDict specified."), request) |
paul@0 | 77 | |
paul@0 | 78 | # Get the WikiDict and the field's definition. |
paul@0 | 79 | |
paul@0 | 80 | wikidict = getWikiDict(dictpage, request) |
paul@0 | 81 | |
paul@0 | 82 | if not wikidict: |
paul@0 | 83 | return showError(_("WikiDict %s cannot be loaded.") % dictpage, request) |
paul@0 | 84 | |
paul@0 | 85 | try: |
paul@0 | 86 | field_definition = wikidict[name] |
paul@0 | 87 | except KeyError: |
paul@0 | 88 | return showError(_("No entry for %s in %s.") % (name, dictpage), request) |
paul@0 | 89 | |
paul@0 | 90 | field_args = {} |
paul@0 | 91 | |
paul@0 | 92 | for field_arg in field_definition.split(): |
paul@0 | 93 | |
paul@0 | 94 | # Record the key-value details. |
paul@0 | 95 | |
paul@0 | 96 | try: |
paul@0 | 97 | argname, argvalue = field_arg.split("=", 1) |
paul@0 | 98 | field_args[argname] = argvalue |
paul@0 | 99 | |
paul@0 | 100 | # Single keywords are interpreted as type descriptions. |
paul@0 | 101 | |
paul@0 | 102 | except ValueError: |
paul@0 | 103 | if not field_args.has_key("type"): |
paul@0 | 104 | field_args["type"] = field_arg |
paul@0 | 105 | |
paul@0 | 106 | # Obtain any request parameters corresponding to the field. |
paul@0 | 107 | |
paul@0 | 108 | form = get_form(request) |
paul@0 | 109 | value = form.get(name, [""])[0] |
paul@0 | 110 | |
paul@0 | 111 | # Render the field. |
paul@0 | 112 | |
paul@0 | 113 | type = field_args.get("type", "text") |
paul@0 | 114 | |
paul@0 | 115 | if type == "text": |
paul@0 | 116 | return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % ( |
paul@0 | 117 | escattr(name), escattr(type), escattr(field_args.get("size", "10")), escattr(value) |
paul@0 | 118 | )) |
paul@0 | 119 | |
paul@0 | 120 | elif type == "textarea": |
paul@0 | 121 | return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % ( |
paul@0 | 122 | escattr(name), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value) |
paul@0 | 123 | )) |
paul@0 | 124 | |
paul@0 | 125 | elif type == "submit": |
paul@0 | 126 | return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % ( |
paul@0 | 127 | escattr(name), escattr(_(label or name)) |
paul@0 | 128 | )) |
paul@0 | 129 | |
paul@0 | 130 | # NOTE: Support select fields at the very least. |
paul@0 | 131 | |
paul@0 | 132 | else: |
paul@0 | 133 | return '' |
paul@0 | 134 | |
paul@0 | 135 | def showError(text, request): |
paul@0 | 136 | fmt = request.formatter |
paul@0 | 137 | |
paul@0 | 138 | output = [] |
paul@0 | 139 | append = output.append |
paul@0 | 140 | |
paul@0 | 141 | append(fmt.span(on=1, attrs={"class" : "form-field-error"})) |
paul@0 | 142 | append(fmt.text(text)) |
paul@0 | 143 | append(fmt.span(on=0)) |
paul@0 | 144 | |
paul@0 | 145 | return "".join(output) |
paul@0 | 146 | |
paul@0 | 147 | # vim: tabstop=4 expandtab shiftwidth=4 |