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.action import ActionBase |
paul@0 | 15 | from MoinMoin.PageEditor import PageEditor |
paul@0 | 16 | from MoinContentSupport import ActionSupport |
paul@0 | 17 | import re |
paul@0 | 18 | |
paul@0 | 19 | macro_pattern = re.compile(ur'^(?P<leading>.*?)<<AddLinkToPage\((?P<identifier>[^\s,)]+).*?\)>>(?P<trailing>.*)$', |
paul@0 | 20 | re.MULTILINE | re.UNICODE) |
paul@0 | 21 | |
paul@0 | 22 | # Action class and supporting functions. |
paul@0 | 23 | |
paul@0 | 24 | class AddLinkToPage(ActionBase, ActionSupport): |
paul@0 | 25 | |
paul@0 | 26 | "An action adding links to pages." |
paul@0 | 27 | |
paul@0 | 28 | def do_action(self): |
paul@0 | 29 | |
paul@0 | 30 | "Create the new event." |
paul@0 | 31 | |
paul@0 | 32 | _ = self._ |
paul@0 | 33 | form = self.get_form() |
paul@0 | 34 | |
paul@0 | 35 | # If no title exists in the request, an error message is returned. |
paul@0 | 36 | |
paul@0 | 37 | identifier = form.get("identifier", [None])[0] |
paul@0 | 38 | |
paul@0 | 39 | if not identifier: |
paul@0 | 40 | return 0, _("No identifier specified.") |
paul@0 | 41 | |
paul@0 | 42 | link = form.get("link", [None])[0] |
paul@0 | 43 | |
paul@0 | 44 | if not link: |
paul@0 | 45 | return 0, _("No link specified.") |
paul@0 | 46 | |
paul@0 | 47 | return self.add_link_to_page(identifier, link) |
paul@0 | 48 | |
paul@0 | 49 | def render_success(self, msg, msgtype=None): |
paul@0 | 50 | |
paul@0 | 51 | """ |
paul@0 | 52 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@0 | 53 | instead. |
paul@0 | 54 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@0 | 55 | """ |
paul@0 | 56 | |
paul@0 | 57 | pass |
paul@0 | 58 | |
paul@0 | 59 | def add_link_to_page(self, identifier, link): |
paul@0 | 60 | |
paul@0 | 61 | """ |
paul@0 | 62 | For the macro with the given 'identifier', add 'link' to the current |
paul@0 | 63 | page. |
paul@0 | 64 | """ |
paul@0 | 65 | |
paul@0 | 66 | _ = self._ |
paul@0 | 67 | request = self.request |
paul@0 | 68 | page = self.page |
paul@0 | 69 | formatter = request.formatter |
paul@0 | 70 | form = self.get_form() |
paul@0 | 71 | |
paul@0 | 72 | # Get the link details. |
paul@0 | 73 | |
paul@0 | 74 | title = form.get('title', [link])[0] |
paul@0 | 75 | introduction = form.get('introduction', [""])[0] |
paul@0 | 76 | description = form.get('description', [""])[0] |
paul@0 | 77 | insert_before = form.get('insert_before', [""])[0] |
paul@0 | 78 | |
paul@0 | 79 | # Encode the link details. |
paul@0 | 80 | # NOTE: Should support different formatting options. |
paul@0 | 81 | |
paul@0 | 82 | link_details = "%s[[%s%s]]%s" % ( |
paul@0 | 83 | introduction and ('"%s" ' % formatter.escapedText(introduction)) or "", |
paul@0 | 84 | link, |
paul@0 | 85 | title and ('|%s' % title) or "", |
paul@0 | 86 | description and (" - ''%s''" % description) or "" |
paul@0 | 87 | ) |
paul@0 | 88 | |
paul@0 | 89 | # Open the page for editing. |
paul@0 | 90 | |
paul@0 | 91 | new_page = PageEditor(request, page.page_name) |
paul@0 | 92 | |
paul@0 | 93 | # Parse the page. |
paul@0 | 94 | |
paul@0 | 95 | page_body = page.get_raw_body() |
paul@0 | 96 | |
paul@0 | 97 | for match in macro_pattern.finditer(page_body): |
paul@0 | 98 | macro_identifier = match.group("identifier") |
paul@0 | 99 | leading_text = match.group("leading") |
paul@0 | 100 | trailing_text = match.group("trailing") |
paul@0 | 101 | start, end = match.span() |
paul@0 | 102 | |
paul@0 | 103 | # Where this identifier matches this macro's identifier, insert the |
paul@0 | 104 | # link details. |
paul@0 | 105 | |
paul@0 | 106 | if macro_identifier == identifier: |
paul@0 | 107 | if insert_before: |
paul@0 | 108 | page_body = page_body[:start] + leading_text + link_details + trailing_text + "\n" + page_body[start:] |
paul@0 | 109 | else: |
paul@0 | 110 | page_body = page_body[:end] + "\n" + leading_text + link_details + trailing_text + page_body[end:] |
paul@0 | 111 | |
paul@0 | 112 | # Save the new version of the page. |
paul@0 | 113 | |
paul@0 | 114 | new_page.saveText(page_body, 0) |
paul@0 | 115 | break |
paul@0 | 116 | |
paul@0 | 117 | # NOTE: Perhaps show a message upon failure. |
paul@0 | 118 | |
paul@0 | 119 | request.http_redirect(page.url(request)) |
paul@0 | 120 | return 1, None |
paul@0 | 121 | |
paul@0 | 122 | # Action function. |
paul@0 | 123 | |
paul@0 | 124 | def execute(pagename, request): |
paul@0 | 125 | AddLinkToPage(pagename, request).render() |
paul@0 | 126 | |
paul@0 | 127 | # vim: tabstop=4 expandtab shiftwidth=4 |