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