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