# HG changeset patch # User Paul Boddie # Date 1281889886 -7200 # Node ID b78423a026624a77a46908c6664efd12957f1972 # Parent d358fd48cf8115728a0972d0c93741d8537df3a8 Moved the link details retrieval and editing into the action, providing a default form for the action. diff -r d358fd48cf81 -r b78423a02662 actions/AddLinkToPage.py --- a/actions/AddLinkToPage.py Sun Aug 15 01:52:41 2010 +0200 +++ b/actions/AddLinkToPage.py Sun Aug 15 18:31:26 2010 +0200 @@ -11,20 +11,142 @@ Dependencies = ['pages'] +from MoinMoin import wikiutil from MoinMoin.action import ActionBase from MoinMoin.PageEditor import PageEditor from MoinContentSupport import ActionSupport +import urllib import re +# Page parsing. + macro_pattern = re.compile(ur'^(?P.*?)<[^\s,)]+).*?\)>>(?P.*)$', re.MULTILINE | re.UNICODE) +# Link visiting and parsing. + +title_pattern = re.compile(ur'<(?Ptitle|h\d)(\s.*?)?>(?P.*?)</(?P=tag)>', re.MULTILINE | re.UNICODE | re.DOTALL) +paragraph_pattern = re.compile(ur'<p(\s.*?)?>(?P<text>.*?)(?=<p(\s.*?)?>|</p>)', re.MULTILINE | re.UNICODE | re.DOTALL) +tag_pattern = re.compile(ur'<.*?>', re.MULTILINE | re.UNICODE | re.DOTALL) + +def get_link_info(link): + + "Get information from the given 'link'." + + # NOTE: Insist on remote URLs! + + try: + f = urllib.urlopen(link) + except IOError: + return None + + try: + s = f.read() + first_title = "" + + for title_match in title_pattern.finditer(s): + title = title_match.group("title").strip() + start, end = title_match.span() + + if not first_title: + first_title = title + + for intro_match in paragraph_pattern.finditer(s[end:]): + intro = get_flattened_content(intro_match.group("text")).strip() + if intro: + return title, intro + finally: + f.close() + + return first_title, "" + +def get_flattened_content(s): + + "Get HTML or XHTML without the tags." + + l = [] + last = 0 + for match in tag_pattern.finditer(s): + start, end = match.span() + l.append(s[last:start]) + last = end + l.append(s[last:]) + return "".join(l).replace("\n", " ") + # Action class and supporting functions. class AddLinkToPage(ActionBase, ActionSupport): "An action adding links to pages." + def get_form_html(self, buttons_html): + _ = self._ + request = self.request + page = self.page + form = self.get_form() + + identifier = form.get("identifier", [None])[0] + link = form.get("link", [None])[0] + insert_before = form.get('insert_before', [""])[0] + title = "" + introduction = "" + + # Acquire information from the link. + + if link is not None: + link_info = get_link_info(link) + + # NOTE: Perhaps show a message upon success/failure. + + if link_info is not None: + title, introduction = link_info + + d = { + "identifier" : wikiutil.escape(identifier, 1), + "insert_before" : insert_before and "true" or "", + "link" : wikiutil.escape(link, 1), + "title" : wikiutil.escape(title, 1), + "intro" : wikiutil.escape(introduction, 1), + "url_label" : wikiutil.escape(_("URL")), + "title_label" : wikiutil.escape(_("Title")), + "intro_label" : wikiutil.escape(_("Introduction")), + "description_label" : wikiutil.escape(_("Description")), + "submit_label" : wikiutil.escape(_("Submit link")), + "script_name" : request.getScriptname(), + "page_url" : wikiutil.quoteWikinameURL(page.page_name) + } + + html = ''' +<form class="macro" method="POST" action="%(script_name)s/%(page_url)s"> + <input type="hidden" name="identifier" value="%(identifier)s" /> + <input type="hidden" name="doit" value="1" /> + <input type="hidden" name="insert_before" value="%(insert_before)s" /> + <input type="hidden" name="action" value="AddLinkToPage" /> + <table> + <tr> + <td class="label">%(url_label)s</td> + <td><input type="text" name="link" value="%(link)s" size="40" /></td> + </tr> + <tr> + <td class="label">%(title_label)s</td> + <td><input type="text" name="title" value="%(title)s" size="40" /></td> + </tr> + <tr> + <td class="label">%(intro_label)s</td> + <td><textarea name="introduction" cols="40" rows="3">%(intro)s</textarea></td> + </tr> + <tr> + <td class="label">%(description_label)s</td> + <td><input type="text" name="description" size="40" /></td> + </tr> + <tr> + <td colspan="2"><input type="submit" value="%(submit_label)s" /></td> + </tr> + </table> +</form>''' % d + + return html + def do_action(self): "Create the new event." @@ -35,12 +157,11 @@ # If no title exists in the request, an error message is returned. identifier = form.get("identifier", [None])[0] + link = form.get("link", [None])[0] if not identifier: return 0, _("No identifier specified.") - link = form.get("link", [None])[0] - if not link: return 0, _("No link specified.") @@ -80,7 +201,7 @@ # NOTE: Should support different formatting options. link_details = "%s[[%s%s]]%s" % ( - introduction and ('"%s" ' % formatter.escapedText(introduction)) or "", + introduction and (get_verbatim('"%s" ' % introduction)) or "", link, title and ('|%s' % title) or "", description and (" - ''%s''" % description) or "" @@ -119,6 +240,20 @@ request.http_redirect(page.url(request)) return 1, None +def get_verbatim(s): + + "Return 's' encoded as verbatim text." + + output = [] + + for part in s.split('"'): + if part: + output.append('<<Verbatim("%s")>>' % part) + else: + output.append('') + + return '"'.join(output) + # Action function. def execute(pagename, request): diff -r d358fd48cf81 -r b78423a02662 macros/AddLinkToPage.py --- a/macros/AddLinkToPage.py Sun Aug 15 01:52:41 2010 +0200 +++ b/macros/AddLinkToPage.py Sun Aug 15 18:31:26 2010 +0200 @@ -12,58 +12,6 @@ Dependencies = ["pages"] from MoinMoin import wikiutil -from MoinMoin.PageEditor import PageEditor -from MoinContentSupport import get_form -import urllib -import re - -title_pattern = re.compile(ur'<(?P<tag>title|h\d)(\s.*?)?>(?P<title>.*?)</(?P=tag)>', re.MULTILINE | re.UNICODE | re.DOTALL) -paragraph_pattern = re.compile(ur'<p(\s.*?)?>(?P<text>.*?)(?=<p(\s.*?)?>|</p>)', re.MULTILINE | re.UNICODE | re.DOTALL) -tag_pattern = re.compile(ur'<.*?>', re.MULTILINE | re.UNICODE | re.DOTALL) - -def get_link_info(link): - - "Get information from the given 'link'." - - # NOTE: Insist on remote URLs! - - try: - f = urllib.urlopen(link) - except IOError: - return None - - try: - s = f.read() - first_title = "" - - for title_match in title_pattern.finditer(s): - title = title_match.group("title").strip() - start, end = title_match.span() - - if not first_title: - first_title = title - - for intro_match in paragraph_pattern.finditer(s[end:]): - intro = get_flattened_content(intro_match.group("text")).strip() - if intro: - return title, intro - finally: - f.close() - - return first_title, "" - -def get_flattened_content(s): - - "Get HTML or XHTML without the tags." - - l = [] - last = 0 - for match in tag_pattern.finditer(s): - start, end = match.span() - l.append(s[last:start]) - last = end - l.append(s[last:]) - return "".join(l).replace("\n", " ") # Macro functions. @@ -76,11 +24,8 @@ request = macro.request formatter = macro.formatter page = formatter.page - form = get_form(request) _ = macro._ - output = [] - # Interpret the arguments. try: @@ -98,102 +43,27 @@ insert_before = "before" in parsed_args[1:] or not ("after" in parsed_args[1:]) - # If the request refers to this macro, another kind of form will be shown. - - active_identifier = form.get('add_link_to_page', [None])[0] - link = form.get('add_link_to_page_link', [None])[0] - - # Test for usage of this macro. - # Where this macro is not active, don't do anything. - - if identifier != active_identifier: - show_macro = 1 - - # Otherwise, show a form containing link details. - - else: - - # Acquire information from the link. - - link_info = get_link_info(link) - - # NOTE: Perhaps show a message upon success/failure. + # Show the fields for link submission. - if link_info is None: - show_macro = 1 - - # Information was retrieved and is now shown. - - else: - title, introduction = link_info - - # Show a form containing the retrieved information suitable for the - # corresponding action. - - output.append(u'<form class="macro" method="POST" action="%s/%s">' % ( - request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name))) - - output.append(u''' - <input type="hidden" name="identifier" value="%(identifier)s" /> - <input type="hidden" name="doit" value="1" /> - <input type="hidden" name="insert_before" value="%(insert_before)s" /> - <input type="hidden" name="action" value="AddLinkToPage" />''' % { - "identifier" : wikiutil.escape(identifier, 1), - "insert_before" : insert_before and "true" or "" - }) + d = { + "script_name" : request.getScriptname(), + "page_url" : wikiutil.quoteWikinameURL(page.page_name), + "identifier" : wikiutil.escape(identifier, 1), + "insert_before" : insert_before and "true" or "", + "submit_label" : wikiutil.escape(_("Add link")) + } - output.append(u''' - <table> - <tr> - <th>%(url_label)s</th> - <td><input type="text" name="link" value="%(link)s" /></td> - </tr> - <tr> - <th>%(title_label)s</th> - <td><input type="text" name="title" value="%(title)s" /></td> - </tr> - <tr> - <th>%(intro_label)s</th> - <td><input type="text" name="introduction" value="%(intro)s" /></td> - </tr> - <tr> - <th>%(description_label)s</th> - <td><input type="text" name="description" /></td> - </tr> - <tr> - <td colspan="2"><input type="submit" value="%(submit_label)s" /></td> - </tr> - </table>''' % { - "link" : wikiutil.escape(link, 1), - "title" : wikiutil.escape(title, 1), - "intro" : wikiutil.escape(introduction, 1), - "url_label" : wikiutil.escape(_("URL")), - "title_label" : wikiutil.escape(_("Title")), - "intro_label" : wikiutil.escape(_("Introduction")), - "description_label" : wikiutil.escape(_("Description")), - "submit_label" : wikiutil.escape(_("Submit link")) - }) + html = ''' +<form class="macro" method="POST" action="%(script_name)s/%(page_url)s"> + <input type="hidden" name="action" value="AddLinkToPage" /> + <input type="hidden" name="identifier" value="%(identifier)s" /> + <input type="hidden" name="insert_before" value="%(insert_before)s" /> + <div> + <input type="text" name="link" /> + <input type="submit" value="%(submit_label)s" /> + </div> +</form>''' % d - output.append(u'</form>') - - # Don't show the macro. - - show_macro = 0 - - # If the macro is to be shown, emit the usual fields. - - if show_macro: - output.append(u'<form class="macro" method="POST" action="%s/%s">' % ( - request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name))) - output.append(u'<input type="hidden" name="add_link_to_page" value="%s" />' % - wikiutil.escape(identifier, 1)) - output.append(u'<div>') - output.append(u'<input type="text" name="add_link_to_page_link" />') - output.append(u'<input type="submit" value="%s" />' % - wikiutil.escape(_("Add link"))) - output.append(u'</div>') - output.append(u'</form>') - - return formatter.rawHTML('\n'.join(output)) + return formatter.rawHTML(html) # vim: tabstop=4 expandtab shiftwidth=4