1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/actions/AddLinkToPage.py Sun Aug 15 01:52:41 2010 +0200
1.3 @@ -0,0 +1,127 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - AddLinkToPage
1.7 +
1.8 + Add a link using a form in the page, getting details of the linked document
1.9 + and inserting them with the link itself.
1.10 +
1.11 + @copyright: 2010 Paul Boddie <paul@boddie.org.uk>
1.12 + @license: GNU GPL, see COPYING for details.
1.13 +"""
1.14 +
1.15 +Dependencies = ['pages']
1.16 +
1.17 +from MoinMoin.action import ActionBase
1.18 +from MoinMoin.PageEditor import PageEditor
1.19 +from MoinContentSupport import ActionSupport
1.20 +import re
1.21 +
1.22 +macro_pattern = re.compile(ur'^(?P<leading>.*?)<<AddLinkToPage\((?P<identifier>[^\s,)]+).*?\)>>(?P<trailing>.*)$',
1.23 + re.MULTILINE | re.UNICODE)
1.24 +
1.25 +# Action class and supporting functions.
1.26 +
1.27 +class AddLinkToPage(ActionBase, ActionSupport):
1.28 +
1.29 + "An action adding links to pages."
1.30 +
1.31 + def do_action(self):
1.32 +
1.33 + "Create the new event."
1.34 +
1.35 + _ = self._
1.36 + form = self.get_form()
1.37 +
1.38 + # If no title exists in the request, an error message is returned.
1.39 +
1.40 + identifier = form.get("identifier", [None])[0]
1.41 +
1.42 + if not identifier:
1.43 + return 0, _("No identifier specified.")
1.44 +
1.45 + link = form.get("link", [None])[0]
1.46 +
1.47 + if not link:
1.48 + return 0, _("No link specified.")
1.49 +
1.50 + return self.add_link_to_page(identifier, link)
1.51 +
1.52 + def render_success(self, msg, msgtype=None):
1.53 +
1.54 + """
1.55 + Render neither 'msg' nor 'msgtype' since redirection should occur
1.56 + instead.
1.57 + NOTE: msgtype is optional because MoinMoin 1.5.x does not support it.
1.58 + """
1.59 +
1.60 + pass
1.61 +
1.62 + def add_link_to_page(self, identifier, link):
1.63 +
1.64 + """
1.65 + For the macro with the given 'identifier', add 'link' to the current
1.66 + page.
1.67 + """
1.68 +
1.69 + _ = self._
1.70 + request = self.request
1.71 + page = self.page
1.72 + formatter = request.formatter
1.73 + form = self.get_form()
1.74 +
1.75 + # Get the link details.
1.76 +
1.77 + title = form.get('title', [link])[0]
1.78 + introduction = form.get('introduction', [""])[0]
1.79 + description = form.get('description', [""])[0]
1.80 + insert_before = form.get('insert_before', [""])[0]
1.81 +
1.82 + # Encode the link details.
1.83 + # NOTE: Should support different formatting options.
1.84 +
1.85 + link_details = "%s[[%s%s]]%s" % (
1.86 + introduction and ('"%s" ' % formatter.escapedText(introduction)) or "",
1.87 + link,
1.88 + title and ('|%s' % title) or "",
1.89 + description and (" - ''%s''" % description) or ""
1.90 + )
1.91 +
1.92 + # Open the page for editing.
1.93 +
1.94 + new_page = PageEditor(request, page.page_name)
1.95 +
1.96 + # Parse the page.
1.97 +
1.98 + page_body = page.get_raw_body()
1.99 +
1.100 + for match in macro_pattern.finditer(page_body):
1.101 + macro_identifier = match.group("identifier")
1.102 + leading_text = match.group("leading")
1.103 + trailing_text = match.group("trailing")
1.104 + start, end = match.span()
1.105 +
1.106 + # Where this identifier matches this macro's identifier, insert the
1.107 + # link details.
1.108 +
1.109 + if macro_identifier == identifier:
1.110 + if insert_before:
1.111 + page_body = page_body[:start] + leading_text + link_details + trailing_text + "\n" + page_body[start:]
1.112 + else:
1.113 + page_body = page_body[:end] + "\n" + leading_text + link_details + trailing_text + page_body[end:]
1.114 +
1.115 + # Save the new version of the page.
1.116 +
1.117 + new_page.saveText(page_body, 0)
1.118 + break
1.119 +
1.120 + # NOTE: Perhaps show a message upon failure.
1.121 +
1.122 + request.http_redirect(page.url(request))
1.123 + return 1, None
1.124 +
1.125 +# Action function.
1.126 +
1.127 +def execute(pagename, request):
1.128 + AddLinkToPage(pagename, request).render()
1.129 +
1.130 +# vim: tabstop=4 expandtab shiftwidth=4