1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/macros/AddLinkToPage.py Sun Aug 15 01:52:41 2010 +0200
1.3 @@ -0,0 +1,199 @@
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 import wikiutil
1.18 +from MoinMoin.PageEditor import PageEditor
1.19 +from MoinContentSupport import get_form
1.20 +import urllib
1.21 +import re
1.22 +
1.23 +title_pattern = re.compile(ur'<(?P<tag>title|h\d)(\s.*?)?>(?P<title>.*?)</(?P=tag)>', re.MULTILINE | re.UNICODE | re.DOTALL)
1.24 +paragraph_pattern = re.compile(ur'<p(\s.*?)?>(?P<text>.*?)(?=<p(\s.*?)?>|</p>)', re.MULTILINE | re.UNICODE | re.DOTALL)
1.25 +tag_pattern = re.compile(ur'<.*?>', re.MULTILINE | re.UNICODE | re.DOTALL)
1.26 +
1.27 +def get_link_info(link):
1.28 +
1.29 + "Get information from the given 'link'."
1.30 +
1.31 + # NOTE: Insist on remote URLs!
1.32 +
1.33 + try:
1.34 + f = urllib.urlopen(link)
1.35 + except IOError:
1.36 + return None
1.37 +
1.38 + try:
1.39 + s = f.read()
1.40 + first_title = ""
1.41 +
1.42 + for title_match in title_pattern.finditer(s):
1.43 + title = title_match.group("title").strip()
1.44 + start, end = title_match.span()
1.45 +
1.46 + if not first_title:
1.47 + first_title = title
1.48 +
1.49 + for intro_match in paragraph_pattern.finditer(s[end:]):
1.50 + intro = get_flattened_content(intro_match.group("text")).strip()
1.51 + if intro:
1.52 + return title, intro
1.53 + finally:
1.54 + f.close()
1.55 +
1.56 + return first_title, ""
1.57 +
1.58 +def get_flattened_content(s):
1.59 +
1.60 + "Get HTML or XHTML without the tags."
1.61 +
1.62 + l = []
1.63 + last = 0
1.64 + for match in tag_pattern.finditer(s):
1.65 + start, end = match.span()
1.66 + l.append(s[last:start])
1.67 + last = end
1.68 + l.append(s[last:])
1.69 + return "".join(l).replace("\n", " ")
1.70 +
1.71 +# Macro functions.
1.72 +
1.73 +def execute(macro, args):
1.74 +
1.75 + """
1.76 + Execute the 'macro' with the given 'args':
1.77 + """
1.78 +
1.79 + request = macro.request
1.80 + formatter = macro.formatter
1.81 + page = formatter.page
1.82 + form = get_form(request)
1.83 + _ = macro._
1.84 +
1.85 + output = []
1.86 +
1.87 + # Interpret the arguments.
1.88 +
1.89 + try:
1.90 + parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or []
1.91 + except AttributeError:
1.92 + parsed_args = args.split(",")
1.93 +
1.94 + parsed_args = [arg for arg in parsed_args if arg]
1.95 +
1.96 + # The macro's identifier should always appear first.
1.97 +
1.98 + identifier = parsed_args[0]
1.99 +
1.100 + # Look for keywords determining the action of the macro.
1.101 +
1.102 + insert_before = "before" in parsed_args[1:] or not ("after" in parsed_args[1:])
1.103 +
1.104 + # If the request refers to this macro, another kind of form will be shown.
1.105 +
1.106 + active_identifier = form.get('add_link_to_page', [None])[0]
1.107 + link = form.get('add_link_to_page_link', [None])[0]
1.108 +
1.109 + # Test for usage of this macro.
1.110 + # Where this macro is not active, don't do anything.
1.111 +
1.112 + if identifier != active_identifier:
1.113 + show_macro = 1
1.114 +
1.115 + # Otherwise, show a form containing link details.
1.116 +
1.117 + else:
1.118 +
1.119 + # Acquire information from the link.
1.120 +
1.121 + link_info = get_link_info(link)
1.122 +
1.123 + # NOTE: Perhaps show a message upon success/failure.
1.124 +
1.125 + if link_info is None:
1.126 + show_macro = 1
1.127 +
1.128 + # Information was retrieved and is now shown.
1.129 +
1.130 + else:
1.131 + title, introduction = link_info
1.132 +
1.133 + # Show a form containing the retrieved information suitable for the
1.134 + # corresponding action.
1.135 +
1.136 + output.append(u'<form class="macro" method="POST" action="%s/%s">' % (
1.137 + request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name)))
1.138 +
1.139 + output.append(u'''
1.140 + <input type="hidden" name="identifier" value="%(identifier)s" />
1.141 + <input type="hidden" name="doit" value="1" />
1.142 + <input type="hidden" name="insert_before" value="%(insert_before)s" />
1.143 + <input type="hidden" name="action" value="AddLinkToPage" />''' % {
1.144 + "identifier" : wikiutil.escape(identifier, 1),
1.145 + "insert_before" : insert_before and "true" or ""
1.146 + })
1.147 +
1.148 + output.append(u'''
1.149 + <table>
1.150 + <tr>
1.151 + <th>%(url_label)s</th>
1.152 + <td><input type="text" name="link" value="%(link)s" /></td>
1.153 + </tr>
1.154 + <tr>
1.155 + <th>%(title_label)s</th>
1.156 + <td><input type="text" name="title" value="%(title)s" /></td>
1.157 + </tr>
1.158 + <tr>
1.159 + <th>%(intro_label)s</th>
1.160 + <td><input type="text" name="introduction" value="%(intro)s" /></td>
1.161 + </tr>
1.162 + <tr>
1.163 + <th>%(description_label)s</th>
1.164 + <td><input type="text" name="description" /></td>
1.165 + </tr>
1.166 + <tr>
1.167 + <td colspan="2"><input type="submit" value="%(submit_label)s" /></td>
1.168 + </tr>
1.169 + </table>''' % {
1.170 + "link" : wikiutil.escape(link, 1),
1.171 + "title" : wikiutil.escape(title, 1),
1.172 + "intro" : wikiutil.escape(introduction, 1),
1.173 + "url_label" : wikiutil.escape(_("URL")),
1.174 + "title_label" : wikiutil.escape(_("Title")),
1.175 + "intro_label" : wikiutil.escape(_("Introduction")),
1.176 + "description_label" : wikiutil.escape(_("Description")),
1.177 + "submit_label" : wikiutil.escape(_("Submit link"))
1.178 + })
1.179 +
1.180 + output.append(u'</form>')
1.181 +
1.182 + # Don't show the macro.
1.183 +
1.184 + show_macro = 0
1.185 +
1.186 + # If the macro is to be shown, emit the usual fields.
1.187 +
1.188 + if show_macro:
1.189 + output.append(u'<form class="macro" method="POST" action="%s/%s">' % (
1.190 + request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name)))
1.191 + output.append(u'<input type="hidden" name="add_link_to_page" value="%s" />' %
1.192 + wikiutil.escape(identifier, 1))
1.193 + output.append(u'<div>')
1.194 + output.append(u'<input type="text" name="add_link_to_page_link" />')
1.195 + output.append(u'<input type="submit" value="%s" />' %
1.196 + wikiutil.escape(_("Add link")))
1.197 + output.append(u'</div>')
1.198 + output.append(u'</form>')
1.199 +
1.200 + return formatter.rawHTML('\n'.join(output))
1.201 +
1.202 +# vim: tabstop=4 expandtab shiftwidth=4