paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - AddLinkToPage |
paul@0 | 4 | |
paul@0 | 5 | Add a link using a form in the page, getting details of the linked document |
paul@0 | 6 | and inserting them with the link itself. |
paul@0 | 7 | |
paul@0 | 8 | @copyright: 2010 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | @license: GNU GPL, see COPYING for details. |
paul@0 | 10 | """ |
paul@0 | 11 | |
paul@0 | 12 | Dependencies = ['pages'] |
paul@0 | 13 | |
paul@0 | 14 | from MoinMoin.action import ActionBase |
paul@0 | 15 | from MoinMoin.PageEditor import PageEditor |
paul@3 | 16 | from MoinContentSupport import ActionSupport, escape, escattr |
paul@1 | 17 | import urllib |
paul@0 | 18 | import re |
paul@0 | 19 | |
paul@1 | 20 | # Page parsing. |
paul@1 | 21 | |
paul@2 | 22 | macro_pattern = re.compile( |
paul@2 | 23 | ur'^(?P<leading>.*?)' # leading text on the line |
paul@2 | 24 | ur'<<AddLinkToPage\(' # macro prologue |
paul@2 | 25 | ur'(?P<identifier>[^\s,)]+).*?' # identifier |
paul@2 | 26 | ur'\)>>' # macro epilogue |
paul@2 | 27 | ur'(?P<trailing>.*)$', # trailing text on the line |
paul@0 | 28 | re.MULTILINE | re.UNICODE) |
paul@0 | 29 | |
paul@1 | 30 | # Link visiting and parsing. |
paul@1 | 31 | |
paul@2 | 32 | def attr_pattern(agroup, attrname, qgroup, vgroup): |
paul@2 | 33 | return ( |
paul@2 | 34 | ur'''(?P<%s>%s)''' # attribute name |
paul@2 | 35 | ur'''\s*=\s*''' # = |
paul@2 | 36 | ur'''(?P<%s>['"])''' # opening quote |
paul@2 | 37 | ur'''(?P<%s>.*?)''' # value |
paul@2 | 38 | ur'''(?P=%s)''' # closing quote |
paul@2 | 39 | % (agroup, attrname, qgroup, vgroup, qgroup) |
paul@2 | 40 | ) |
paul@2 | 41 | |
paul@2 | 42 | meta_pattern = re.compile( |
paul@2 | 43 | ur'<meta' |
paul@2 | 44 | ur'([^>]*?' |
paul@2 | 45 | ur'(' |
paul@2 | 46 | ur'(' + attr_pattern("nattr", "name", "nquote", "name") + ')' |
paul@2 | 47 | ur'|(' + attr_pattern("cattr", "content", "cquote", "content") + ')' |
paul@2 | 48 | ur')' |
paul@2 | 49 | ur')*' |
paul@2 | 50 | ur'[^>]*?>', |
paul@2 | 51 | re.MULTILINE | re.DOTALL) |
paul@2 | 52 | |
paul@2 | 53 | title_pattern = re.compile( |
paul@2 | 54 | ur'<(?P<tag>title|h\d)(\s.*?)?>' |
paul@2 | 55 | ur'(?P<title>.*?)' |
paul@2 | 56 | ur'</(?P=tag)>', |
paul@2 | 57 | re.MULTILINE | re.DOTALL) |
paul@2 | 58 | |
paul@2 | 59 | paragraph_pattern = re.compile( |
paul@2 | 60 | ur'<p(\s.*?)?>' |
paul@2 | 61 | ur'(?P<text>.*?)' |
paul@2 | 62 | ur'(?=<p(\s.*?)?>|</p>)', |
paul@2 | 63 | re.MULTILINE | re.DOTALL) |
paul@2 | 64 | |
paul@2 | 65 | tag_pattern = re.compile( |
paul@2 | 66 | ur'<.*?>', |
paul@2 | 67 | re.MULTILINE | re.DOTALL) |
paul@2 | 68 | |
paul@2 | 69 | def get_text(s): |
paul@2 | 70 | try: |
paul@2 | 71 | return unicode(s, "utf-8") |
paul@2 | 72 | except UnicodeError: |
paul@2 | 73 | return unicode(s, "iso-8859-1") |
paul@1 | 74 | |
paul@1 | 75 | def get_link_info(link): |
paul@1 | 76 | |
paul@1 | 77 | "Get information from the given 'link'." |
paul@1 | 78 | |
paul@3 | 79 | # Insist on remote URLs! |
paul@3 | 80 | # NOTE: This could probably be done better. |
paul@3 | 81 | |
paul@3 | 82 | if not link.startswith("http:"): |
paul@3 | 83 | return None |
paul@1 | 84 | |
paul@1 | 85 | try: |
paul@1 | 86 | f = urllib.urlopen(link) |
paul@1 | 87 | except IOError: |
paul@1 | 88 | return None |
paul@1 | 89 | |
paul@1 | 90 | try: |
paul@3 | 91 | s = get_text(f.read()) |
paul@2 | 92 | |
paul@2 | 93 | # Look for metadata. |
paul@2 | 94 | |
paul@2 | 95 | title = None |
paul@2 | 96 | intro = None |
paul@2 | 97 | |
paul@2 | 98 | for meta_match in meta_pattern.finditer(s): |
paul@2 | 99 | name = meta_match.group("name") |
paul@2 | 100 | content = meta_match.group("content") |
paul@2 | 101 | if name == "title": |
paul@2 | 102 | title = content |
paul@2 | 103 | elif name == "description": |
paul@2 | 104 | intro = content |
paul@2 | 105 | |
paul@2 | 106 | if title and intro: |
paul@3 | 107 | return title, intro |
paul@2 | 108 | |
paul@2 | 109 | # Look for titles/headings and accompanying text. |
paul@2 | 110 | |
paul@1 | 111 | first_title = "" |
paul@1 | 112 | |
paul@1 | 113 | for title_match in title_pattern.finditer(s): |
paul@1 | 114 | title = title_match.group("title").strip() |
paul@1 | 115 | start, end = title_match.span() |
paul@1 | 116 | |
paul@1 | 117 | if not first_title: |
paul@1 | 118 | first_title = title |
paul@1 | 119 | |
paul@1 | 120 | for intro_match in paragraph_pattern.finditer(s[end:]): |
paul@1 | 121 | intro = get_flattened_content(intro_match.group("text")).strip() |
paul@1 | 122 | if intro: |
paul@3 | 123 | return title, intro |
paul@1 | 124 | finally: |
paul@1 | 125 | f.close() |
paul@1 | 126 | |
paul@3 | 127 | return first_title, u"" |
paul@1 | 128 | |
paul@1 | 129 | def get_flattened_content(s): |
paul@1 | 130 | |
paul@1 | 131 | "Get HTML or XHTML without the tags." |
paul@1 | 132 | |
paul@1 | 133 | l = [] |
paul@1 | 134 | last = 0 |
paul@1 | 135 | for match in tag_pattern.finditer(s): |
paul@1 | 136 | start, end = match.span() |
paul@1 | 137 | l.append(s[last:start]) |
paul@1 | 138 | last = end |
paul@1 | 139 | l.append(s[last:]) |
paul@3 | 140 | return "".join(l).replace("\n", " ") |
paul@1 | 141 | |
paul@0 | 142 | # Action class and supporting functions. |
paul@0 | 143 | |
paul@0 | 144 | class AddLinkToPage(ActionBase, ActionSupport): |
paul@0 | 145 | |
paul@0 | 146 | "An action adding links to pages." |
paul@0 | 147 | |
paul@1 | 148 | def get_form_html(self, buttons_html): |
paul@1 | 149 | _ = self._ |
paul@1 | 150 | request = self.request |
paul@1 | 151 | page = self.page |
paul@1 | 152 | form = self.get_form() |
paul@1 | 153 | |
paul@3 | 154 | identifier = form.get("identifier", [""])[0] |
paul@3 | 155 | identifier_list = [] |
paul@3 | 156 | |
paul@3 | 157 | if not identifier: |
paul@3 | 158 | |
paul@3 | 159 | # Show all macro identifiers by parsing the page. |
paul@3 | 160 | |
paul@3 | 161 | page_body = page.get_raw_body() |
paul@3 | 162 | |
paul@3 | 163 | for match in macro_pattern.finditer(page_body): |
paul@3 | 164 | found_identifier = match.group("identifier") |
paul@3 | 165 | identifier_list.append('<option value="%s">%s</option>' % ( |
paul@3 | 166 | escattr(found_identifier), escape(found_identifier))) |
paul@3 | 167 | |
paul@3 | 168 | link = form.get("link", [""])[0] |
paul@1 | 169 | insert_before = form.get('insert_before', [""])[0] |
paul@1 | 170 | title = "" |
paul@1 | 171 | introduction = "" |
paul@1 | 172 | |
paul@1 | 173 | # Acquire information from the link. |
paul@1 | 174 | |
paul@3 | 175 | if link: |
paul@1 | 176 | link_info = get_link_info(link) |
paul@1 | 177 | |
paul@1 | 178 | # NOTE: Perhaps show a message upon success/failure. |
paul@1 | 179 | |
paul@1 | 180 | if link_info is not None: |
paul@1 | 181 | title, introduction = link_info |
paul@1 | 182 | |
paul@1 | 183 | d = { |
paul@3 | 184 | "buttons_html" : buttons_html, |
paul@3 | 185 | "identifiers_label" : escape(_("Add to position...")), |
paul@3 | 186 | "identifier" : escattr(identifier), |
paul@3 | 187 | "insert_before" : insert_before and "true" or "", |
paul@3 | 188 | "link" : escattr(link), |
paul@3 | 189 | "title" : escattr(title), |
paul@3 | 190 | "intro" : escattr(introduction), |
paul@3 | 191 | "url_label" : escape(_("URL")), |
paul@3 | 192 | "title_label" : escape(_("Title")), |
paul@3 | 193 | "intro_label" : escape(_("Introduction")), |
paul@3 | 194 | "description_label" : escape(_("Description")), |
paul@3 | 195 | "preview_label" : escape(_("Preview link")), |
paul@1 | 196 | } |
paul@1 | 197 | |
paul@3 | 198 | # Given an identifier, preserve the state in a hidden field. |
paul@3 | 199 | |
paul@3 | 200 | if not identifier_list: |
paul@3 | 201 | html = ''' |
paul@3 | 202 | <input type="hidden" name="identifier" value="%(identifier)s" />''' % d |
paul@3 | 203 | else: |
paul@3 | 204 | html = '' |
paul@3 | 205 | |
paul@3 | 206 | # Start the rest of the form. |
paul@3 | 207 | |
paul@3 | 208 | html += u''' |
paul@1 | 209 | <input type="hidden" name="insert_before" value="%(insert_before)s" /> |
paul@1 | 210 | <table> |
paul@1 | 211 | <tr> |
paul@1 | 212 | <td class="label">%(url_label)s</td> |
paul@1 | 213 | <td><input type="text" name="link" value="%(link)s" size="40" /></td> |
paul@3 | 214 | </tr>''' % d |
paul@3 | 215 | |
paul@3 | 216 | # Given no identifier, show the choice of insertion positions. |
paul@3 | 217 | |
paul@3 | 218 | if identifier_list: |
paul@3 | 219 | html += ''' |
paul@3 | 220 | <tr> |
paul@3 | 221 | <td class="label">%(identifiers_label)s</td> |
paul@3 | 222 | <td> |
paul@3 | 223 | <select name="identifier">''' % d |
paul@3 | 224 | |
paul@3 | 225 | for identifier_option in identifier_list: |
paul@3 | 226 | html += ''' |
paul@3 | 227 | %s''' % identifier_option |
paul@3 | 228 | |
paul@3 | 229 | html += ''' |
paul@3 | 230 | </select> |
paul@3 | 231 | </td> |
paul@3 | 232 | </tr>''' |
paul@3 | 233 | |
paul@3 | 234 | # Finish the form. |
paul@3 | 235 | |
paul@3 | 236 | html += ''' |
paul@1 | 237 | <tr> |
paul@1 | 238 | <td class="label">%(title_label)s</td> |
paul@1 | 239 | <td><input type="text" name="title" value="%(title)s" size="40" /></td> |
paul@1 | 240 | </tr> |
paul@1 | 241 | <tr> |
paul@1 | 242 | <td class="label">%(intro_label)s</td> |
paul@1 | 243 | <td><textarea name="introduction" cols="40" rows="3">%(intro)s</textarea></td> |
paul@1 | 244 | </tr> |
paul@1 | 245 | <tr> |
paul@1 | 246 | <td class="label">%(description_label)s</td> |
paul@1 | 247 | <td><input type="text" name="description" size="40" /></td> |
paul@1 | 248 | </tr> |
paul@1 | 249 | <tr> |
paul@3 | 250 | <td></td> |
paul@3 | 251 | <td class="buttons"> |
paul@3 | 252 | <input type="submit" value="%(preview_label)s" />%(buttons_html)s |
paul@3 | 253 | </td> |
paul@1 | 254 | </tr> |
paul@3 | 255 | </table>''' % d |
paul@1 | 256 | |
paul@1 | 257 | return html |
paul@1 | 258 | |
paul@0 | 259 | def do_action(self): |
paul@0 | 260 | |
paul@0 | 261 | "Create the new event." |
paul@0 | 262 | |
paul@0 | 263 | _ = self._ |
paul@0 | 264 | form = self.get_form() |
paul@0 | 265 | |
paul@0 | 266 | # If no title exists in the request, an error message is returned. |
paul@0 | 267 | |
paul@0 | 268 | identifier = form.get("identifier", [None])[0] |
paul@1 | 269 | link = form.get("link", [None])[0] |
paul@0 | 270 | |
paul@0 | 271 | if not identifier: |
paul@0 | 272 | return 0, _("No identifier specified.") |
paul@0 | 273 | |
paul@0 | 274 | if not link: |
paul@0 | 275 | return 0, _("No link specified.") |
paul@0 | 276 | |
paul@0 | 277 | return self.add_link_to_page(identifier, link) |
paul@0 | 278 | |
paul@0 | 279 | def render_success(self, msg, msgtype=None): |
paul@0 | 280 | |
paul@0 | 281 | """ |
paul@0 | 282 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@0 | 283 | instead. |
paul@0 | 284 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@0 | 285 | """ |
paul@0 | 286 | |
paul@0 | 287 | pass |
paul@0 | 288 | |
paul@0 | 289 | def add_link_to_page(self, identifier, link): |
paul@0 | 290 | |
paul@0 | 291 | """ |
paul@0 | 292 | For the macro with the given 'identifier', add 'link' to the current |
paul@0 | 293 | page. |
paul@0 | 294 | """ |
paul@0 | 295 | |
paul@0 | 296 | _ = self._ |
paul@0 | 297 | request = self.request |
paul@0 | 298 | page = self.page |
paul@0 | 299 | formatter = request.formatter |
paul@0 | 300 | form = self.get_form() |
paul@0 | 301 | |
paul@0 | 302 | # Get the link details. |
paul@0 | 303 | |
paul@0 | 304 | title = form.get('title', [link])[0] |
paul@0 | 305 | introduction = form.get('introduction', [""])[0] |
paul@0 | 306 | description = form.get('description', [""])[0] |
paul@0 | 307 | insert_before = form.get('insert_before', [""])[0] |
paul@0 | 308 | |
paul@0 | 309 | # Encode the link details. |
paul@0 | 310 | # NOTE: Should support different formatting options. |
paul@0 | 311 | |
paul@0 | 312 | link_details = "%s[[%s%s]]%s" % ( |
paul@2 | 313 | introduction and ('"%s" ' % get_verbatim(introduction)) or "", |
paul@0 | 314 | link, |
paul@0 | 315 | title and ('|%s' % title) or "", |
paul@0 | 316 | description and (" - ''%s''" % description) or "" |
paul@0 | 317 | ) |
paul@0 | 318 | |
paul@0 | 319 | # Open the page for editing. |
paul@0 | 320 | |
paul@0 | 321 | new_page = PageEditor(request, page.page_name) |
paul@0 | 322 | |
paul@0 | 323 | # Parse the page. |
paul@0 | 324 | |
paul@0 | 325 | page_body = page.get_raw_body() |
paul@0 | 326 | |
paul@0 | 327 | for match in macro_pattern.finditer(page_body): |
paul@0 | 328 | macro_identifier = match.group("identifier") |
paul@0 | 329 | leading_text = match.group("leading") |
paul@0 | 330 | trailing_text = match.group("trailing") |
paul@0 | 331 | start, end = match.span() |
paul@0 | 332 | |
paul@0 | 333 | # Where this identifier matches this macro's identifier, insert the |
paul@0 | 334 | # link details. |
paul@0 | 335 | |
paul@0 | 336 | if macro_identifier == identifier: |
paul@0 | 337 | if insert_before: |
paul@0 | 338 | page_body = page_body[:start] + leading_text + link_details + trailing_text + "\n" + page_body[start:] |
paul@0 | 339 | else: |
paul@0 | 340 | page_body = page_body[:end] + "\n" + leading_text + link_details + trailing_text + page_body[end:] |
paul@0 | 341 | |
paul@0 | 342 | # Save the new version of the page. |
paul@0 | 343 | |
paul@0 | 344 | new_page.saveText(page_body, 0) |
paul@0 | 345 | break |
paul@0 | 346 | |
paul@0 | 347 | # NOTE: Perhaps show a message upon failure. |
paul@0 | 348 | |
paul@0 | 349 | request.http_redirect(page.url(request)) |
paul@0 | 350 | return 1, None |
paul@0 | 351 | |
paul@1 | 352 | def get_verbatim(s): |
paul@1 | 353 | |
paul@1 | 354 | "Return 's' encoded as verbatim text." |
paul@1 | 355 | |
paul@1 | 356 | output = [] |
paul@1 | 357 | |
paul@1 | 358 | for part in s.split('"'): |
paul@1 | 359 | if part: |
paul@1 | 360 | output.append('<<Verbatim("%s")>>' % part) |
paul@1 | 361 | else: |
paul@1 | 362 | output.append('') |
paul@1 | 363 | |
paul@1 | 364 | return '"'.join(output) |
paul@1 | 365 | |
paul@0 | 366 | # Action function. |
paul@0 | 367 | |
paul@0 | 368 | def execute(pagename, request): |
paul@0 | 369 | AddLinkToPage(pagename, request).render() |
paul@0 | 370 | |
paul@0 | 371 | # vim: tabstop=4 expandtab shiftwidth=4 |