paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - AddLinkToPage |
paul@0 | 4 | |
paul@0 | 5 | Add a link using a form in the page, getting details of the linked document |
paul@0 | 6 | and inserting them with the link itself. |
paul@0 | 7 | |
paul@0 | 8 | @copyright: 2010 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | @license: GNU GPL, see COPYING for details. |
paul@0 | 10 | """ |
paul@0 | 11 | |
paul@0 | 12 | Dependencies = ["pages"] |
paul@0 | 13 | |
paul@0 | 14 | from MoinMoin import wikiutil |
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': |
paul@0 | 22 | """ |
paul@0 | 23 | |
paul@0 | 24 | request = macro.request |
paul@0 | 25 | formatter = macro.formatter |
paul@0 | 26 | page = formatter.page |
paul@0 | 27 | _ = macro._ |
paul@0 | 28 | |
paul@0 | 29 | # Interpret the arguments. |
paul@0 | 30 | |
paul@0 | 31 | try: |
paul@0 | 32 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 33 | except AttributeError: |
paul@0 | 34 | parsed_args = args.split(",") |
paul@0 | 35 | |
paul@0 | 36 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 37 | |
paul@0 | 38 | # The macro's identifier should always appear first. |
paul@0 | 39 | |
paul@0 | 40 | identifier = parsed_args[0] |
paul@0 | 41 | |
paul@0 | 42 | # Look for keywords determining the action of the macro. |
paul@0 | 43 | |
paul@0 | 44 | insert_before = "before" in parsed_args[1:] or not ("after" in parsed_args[1:]) |
paul@0 | 45 | |
paul@1 | 46 | # Show the fields for link submission. |
paul@0 | 47 | |
paul@1 | 48 | d = { |
paul@1 | 49 | "script_name" : request.getScriptname(), |
paul@1 | 50 | "page_url" : wikiutil.quoteWikinameURL(page.page_name), |
paul@1 | 51 | "identifier" : wikiutil.escape(identifier, 1), |
paul@1 | 52 | "insert_before" : insert_before and "true" or "", |
paul@1 | 53 | "submit_label" : wikiutil.escape(_("Add link")) |
paul@1 | 54 | } |
paul@0 | 55 | |
paul@1 | 56 | html = ''' |
paul@1 | 57 | <form class="macro" method="POST" action="%(script_name)s/%(page_url)s"> |
paul@1 | 58 | <input type="hidden" name="action" value="AddLinkToPage" /> |
paul@1 | 59 | <input type="hidden" name="identifier" value="%(identifier)s" /> |
paul@1 | 60 | <input type="hidden" name="insert_before" value="%(insert_before)s" /> |
paul@1 | 61 | <div> |
paul@1 | 62 | <input type="text" name="link" /> |
paul@1 | 63 | <input type="submit" value="%(submit_label)s" /> |
paul@1 | 64 | </div> |
paul@1 | 65 | </form>''' % d |
paul@0 | 66 | |
paul@1 | 67 | return formatter.rawHTML(html) |
paul@0 | 68 | |
paul@0 | 69 | # vim: tabstop=4 expandtab shiftwidth=4 |