1.1 --- a/actions/AddLinkToPage.py Mon Oct 04 01:03:07 2010 +0200
1.2 +++ b/actions/AddLinkToPage.py Sun Feb 20 21:15:05 2011 +0100
1.3 @@ -11,10 +11,9 @@
1.4
1.5 Dependencies = ['pages']
1.6
1.7 -from MoinMoin import wikiutil
1.8 from MoinMoin.action import ActionBase
1.9 from MoinMoin.PageEditor import PageEditor
1.10 -from MoinContentSupport import ActionSupport
1.11 +from MoinContentSupport import ActionSupport, escape, escattr
1.12 import urllib
1.13 import re
1.14
1.15 @@ -77,7 +76,11 @@
1.16
1.17 "Get information from the given 'link'."
1.18
1.19 - # NOTE: Insist on remote URLs!
1.20 + # Insist on remote URLs!
1.21 + # NOTE: This could probably be done better.
1.22 +
1.23 + if not link.startswith("http:"):
1.24 + return None
1.25
1.26 try:
1.27 f = urllib.urlopen(link)
1.28 @@ -85,7 +88,7 @@
1.29 return None
1.30
1.31 try:
1.32 - s = f.read()
1.33 + s = get_text(f.read())
1.34
1.35 # Look for metadata.
1.36
1.37 @@ -101,7 +104,7 @@
1.38 intro = content
1.39
1.40 if title and intro:
1.41 - return get_text(title), get_text(intro)
1.42 + return title, intro
1.43
1.44 # Look for titles/headings and accompanying text.
1.45
1.46 @@ -117,11 +120,11 @@
1.47 for intro_match in paragraph_pattern.finditer(s[end:]):
1.48 intro = get_flattened_content(intro_match.group("text")).strip()
1.49 if intro:
1.50 - return get_text(title), get_text(intro)
1.51 + return title, intro
1.52 finally:
1.53 f.close()
1.54
1.55 - return get_text(first_title), u""
1.56 + return first_title, u""
1.57
1.58 def get_flattened_content(s):
1.59
1.60 @@ -134,7 +137,7 @@
1.61 l.append(s[last:start])
1.62 last = end
1.63 l.append(s[last:])
1.64 - return get_text("".join(l).replace("\n", " "))
1.65 + return "".join(l).replace("\n", " ")
1.66
1.67 # Action class and supporting functions.
1.68
1.69 @@ -148,15 +151,28 @@
1.70 page = self.page
1.71 form = self.get_form()
1.72
1.73 - identifier = form.get("identifier", [None])[0]
1.74 - link = form.get("link", [None])[0]
1.75 + identifier = form.get("identifier", [""])[0]
1.76 + identifier_list = []
1.77 +
1.78 + if not identifier:
1.79 +
1.80 + # Show all macro identifiers by parsing the page.
1.81 +
1.82 + page_body = page.get_raw_body()
1.83 +
1.84 + for match in macro_pattern.finditer(page_body):
1.85 + found_identifier = match.group("identifier")
1.86 + identifier_list.append('<option value="%s">%s</option>' % (
1.87 + escattr(found_identifier), escape(found_identifier)))
1.88 +
1.89 + link = form.get("link", [""])[0]
1.90 insert_before = form.get('insert_before', [""])[0]
1.91 title = ""
1.92 introduction = ""
1.93
1.94 # Acquire information from the link.
1.95
1.96 - if link is not None:
1.97 + if link:
1.98 link_info = get_link_info(link)
1.99
1.100 # NOTE: Perhaps show a message upon success/failure.
1.101 @@ -165,31 +181,59 @@
1.102 title, introduction = link_info
1.103
1.104 d = {
1.105 - "identifier" : wikiutil.escape(identifier, 1),
1.106 - "insert_before" : insert_before and "true" or "",
1.107 - "link" : wikiutil.escape(link, 1),
1.108 - "title" : wikiutil.escape(title, 1),
1.109 - "intro" : wikiutil.escape(introduction, 1),
1.110 - "url_label" : wikiutil.escape(_("URL")),
1.111 - "title_label" : wikiutil.escape(_("Title")),
1.112 - "intro_label" : wikiutil.escape(_("Introduction")),
1.113 - "description_label" : wikiutil.escape(_("Description")),
1.114 - "submit_label" : wikiutil.escape(_("Submit link")),
1.115 - "script_name" : request.getScriptname(),
1.116 - "page_url" : wikiutil.quoteWikinameURL(page.page_name)
1.117 + "buttons_html" : buttons_html,
1.118 + "identifiers_label" : escape(_("Add to position...")),
1.119 + "identifier" : escattr(identifier),
1.120 + "insert_before" : insert_before and "true" or "",
1.121 + "link" : escattr(link),
1.122 + "title" : escattr(title),
1.123 + "intro" : escattr(introduction),
1.124 + "url_label" : escape(_("URL")),
1.125 + "title_label" : escape(_("Title")),
1.126 + "intro_label" : escape(_("Introduction")),
1.127 + "description_label" : escape(_("Description")),
1.128 + "preview_label" : escape(_("Preview link")),
1.129 }
1.130
1.131 - html = u'''
1.132 -<form class="macro" method="POST" action="%(script_name)s/%(page_url)s">
1.133 - <input type="hidden" name="identifier" value="%(identifier)s" />
1.134 - <input type="hidden" name="doit" value="1" />
1.135 + # Given an identifier, preserve the state in a hidden field.
1.136 +
1.137 + if not identifier_list:
1.138 + html = '''
1.139 + <input type="hidden" name="identifier" value="%(identifier)s" />''' % d
1.140 + else:
1.141 + html = ''
1.142 +
1.143 + # Start the rest of the form.
1.144 +
1.145 + html += u'''
1.146 <input type="hidden" name="insert_before" value="%(insert_before)s" />
1.147 - <input type="hidden" name="action" value="AddLinkToPage" />
1.148 <table>
1.149 <tr>
1.150 <td class="label">%(url_label)s</td>
1.151 <td><input type="text" name="link" value="%(link)s" size="40" /></td>
1.152 - </tr>
1.153 + </tr>''' % d
1.154 +
1.155 + # Given no identifier, show the choice of insertion positions.
1.156 +
1.157 + if identifier_list:
1.158 + html += '''
1.159 + <tr>
1.160 + <td class="label">%(identifiers_label)s</td>
1.161 + <td>
1.162 + <select name="identifier">''' % d
1.163 +
1.164 + for identifier_option in identifier_list:
1.165 + html += '''
1.166 + %s''' % identifier_option
1.167 +
1.168 + html += '''
1.169 + </select>
1.170 + </td>
1.171 + </tr>'''
1.172 +
1.173 + # Finish the form.
1.174 +
1.175 + html += '''
1.176 <tr>
1.177 <td class="label">%(title_label)s</td>
1.178 <td><input type="text" name="title" value="%(title)s" size="40" /></td>
1.179 @@ -203,10 +247,12 @@
1.180 <td><input type="text" name="description" size="40" /></td>
1.181 </tr>
1.182 <tr>
1.183 - <td colspan="2"><input type="submit" value="%(submit_label)s" /></td>
1.184 + <td></td>
1.185 + <td class="buttons">
1.186 + <input type="submit" value="%(preview_label)s" />%(buttons_html)s
1.187 + </td>
1.188 </tr>
1.189 - </table>
1.190 -</form>''' % d
1.191 + </table>''' % d
1.192
1.193 return html
1.194