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