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 import wikiutil |
paul@0 | 15 | from MoinMoin.PageEditor import PageEditor |
paul@0 | 16 | from MoinContentSupport import get_form |
paul@0 | 17 | import urllib |
paul@0 | 18 | import re |
paul@0 | 19 | |
paul@0 | 20 | title_pattern = re.compile(ur'<(?P<tag>title|h\d)(\s.*?)?>(?P<title>.*?)</(?P=tag)>', re.MULTILINE | re.UNICODE | re.DOTALL) |
paul@0 | 21 | paragraph_pattern = re.compile(ur'<p(\s.*?)?>(?P<text>.*?)(?=<p(\s.*?)?>|</p>)', re.MULTILINE | re.UNICODE | re.DOTALL) |
paul@0 | 22 | tag_pattern = re.compile(ur'<.*?>', re.MULTILINE | re.UNICODE | re.DOTALL) |
paul@0 | 23 | |
paul@0 | 24 | def get_link_info(link): |
paul@0 | 25 | |
paul@0 | 26 | "Get information from the given 'link'." |
paul@0 | 27 | |
paul@0 | 28 | # NOTE: Insist on remote URLs! |
paul@0 | 29 | |
paul@0 | 30 | try: |
paul@0 | 31 | f = urllib.urlopen(link) |
paul@0 | 32 | except IOError: |
paul@0 | 33 | return None |
paul@0 | 34 | |
paul@0 | 35 | try: |
paul@0 | 36 | s = f.read() |
paul@0 | 37 | first_title = "" |
paul@0 | 38 | |
paul@0 | 39 | for title_match in title_pattern.finditer(s): |
paul@0 | 40 | title = title_match.group("title").strip() |
paul@0 | 41 | start, end = title_match.span() |
paul@0 | 42 | |
paul@0 | 43 | if not first_title: |
paul@0 | 44 | first_title = title |
paul@0 | 45 | |
paul@0 | 46 | for intro_match in paragraph_pattern.finditer(s[end:]): |
paul@0 | 47 | intro = get_flattened_content(intro_match.group("text")).strip() |
paul@0 | 48 | if intro: |
paul@0 | 49 | return title, intro |
paul@0 | 50 | finally: |
paul@0 | 51 | f.close() |
paul@0 | 52 | |
paul@0 | 53 | return first_title, "" |
paul@0 | 54 | |
paul@0 | 55 | def get_flattened_content(s): |
paul@0 | 56 | |
paul@0 | 57 | "Get HTML or XHTML without the tags." |
paul@0 | 58 | |
paul@0 | 59 | l = [] |
paul@0 | 60 | last = 0 |
paul@0 | 61 | for match in tag_pattern.finditer(s): |
paul@0 | 62 | start, end = match.span() |
paul@0 | 63 | l.append(s[last:start]) |
paul@0 | 64 | last = end |
paul@0 | 65 | l.append(s[last:]) |
paul@0 | 66 | return "".join(l).replace("\n", " ") |
paul@0 | 67 | |
paul@0 | 68 | # Macro functions. |
paul@0 | 69 | |
paul@0 | 70 | def execute(macro, args): |
paul@0 | 71 | |
paul@0 | 72 | """ |
paul@0 | 73 | Execute the 'macro' with the given 'args': |
paul@0 | 74 | """ |
paul@0 | 75 | |
paul@0 | 76 | request = macro.request |
paul@0 | 77 | formatter = macro.formatter |
paul@0 | 78 | page = formatter.page |
paul@0 | 79 | form = get_form(request) |
paul@0 | 80 | _ = macro._ |
paul@0 | 81 | |
paul@0 | 82 | output = [] |
paul@0 | 83 | |
paul@0 | 84 | # Interpret the arguments. |
paul@0 | 85 | |
paul@0 | 86 | try: |
paul@0 | 87 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@0 | 88 | except AttributeError: |
paul@0 | 89 | parsed_args = args.split(",") |
paul@0 | 90 | |
paul@0 | 91 | parsed_args = [arg for arg in parsed_args if arg] |
paul@0 | 92 | |
paul@0 | 93 | # The macro's identifier should always appear first. |
paul@0 | 94 | |
paul@0 | 95 | identifier = parsed_args[0] |
paul@0 | 96 | |
paul@0 | 97 | # Look for keywords determining the action of the macro. |
paul@0 | 98 | |
paul@0 | 99 | insert_before = "before" in parsed_args[1:] or not ("after" in parsed_args[1:]) |
paul@0 | 100 | |
paul@0 | 101 | # If the request refers to this macro, another kind of form will be shown. |
paul@0 | 102 | |
paul@0 | 103 | active_identifier = form.get('add_link_to_page', [None])[0] |
paul@0 | 104 | link = form.get('add_link_to_page_link', [None])[0] |
paul@0 | 105 | |
paul@0 | 106 | # Test for usage of this macro. |
paul@0 | 107 | # Where this macro is not active, don't do anything. |
paul@0 | 108 | |
paul@0 | 109 | if identifier != active_identifier: |
paul@0 | 110 | show_macro = 1 |
paul@0 | 111 | |
paul@0 | 112 | # Otherwise, show a form containing link details. |
paul@0 | 113 | |
paul@0 | 114 | else: |
paul@0 | 115 | |
paul@0 | 116 | # Acquire information from the link. |
paul@0 | 117 | |
paul@0 | 118 | link_info = get_link_info(link) |
paul@0 | 119 | |
paul@0 | 120 | # NOTE: Perhaps show a message upon success/failure. |
paul@0 | 121 | |
paul@0 | 122 | if link_info is None: |
paul@0 | 123 | show_macro = 1 |
paul@0 | 124 | |
paul@0 | 125 | # Information was retrieved and is now shown. |
paul@0 | 126 | |
paul@0 | 127 | else: |
paul@0 | 128 | title, introduction = link_info |
paul@0 | 129 | |
paul@0 | 130 | # Show a form containing the retrieved information suitable for the |
paul@0 | 131 | # corresponding action. |
paul@0 | 132 | |
paul@0 | 133 | output.append(u'<form class="macro" method="POST" action="%s/%s">' % ( |
paul@0 | 134 | request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name))) |
paul@0 | 135 | |
paul@0 | 136 | output.append(u''' |
paul@0 | 137 | <input type="hidden" name="identifier" value="%(identifier)s" /> |
paul@0 | 138 | <input type="hidden" name="doit" value="1" /> |
paul@0 | 139 | <input type="hidden" name="insert_before" value="%(insert_before)s" /> |
paul@0 | 140 | <input type="hidden" name="action" value="AddLinkToPage" />''' % { |
paul@0 | 141 | "identifier" : wikiutil.escape(identifier, 1), |
paul@0 | 142 | "insert_before" : insert_before and "true" or "" |
paul@0 | 143 | }) |
paul@0 | 144 | |
paul@0 | 145 | output.append(u''' |
paul@0 | 146 | <table> |
paul@0 | 147 | <tr> |
paul@0 | 148 | <th>%(url_label)s</th> |
paul@0 | 149 | <td><input type="text" name="link" value="%(link)s" /></td> |
paul@0 | 150 | </tr> |
paul@0 | 151 | <tr> |
paul@0 | 152 | <th>%(title_label)s</th> |
paul@0 | 153 | <td><input type="text" name="title" value="%(title)s" /></td> |
paul@0 | 154 | </tr> |
paul@0 | 155 | <tr> |
paul@0 | 156 | <th>%(intro_label)s</th> |
paul@0 | 157 | <td><input type="text" name="introduction" value="%(intro)s" /></td> |
paul@0 | 158 | </tr> |
paul@0 | 159 | <tr> |
paul@0 | 160 | <th>%(description_label)s</th> |
paul@0 | 161 | <td><input type="text" name="description" /></td> |
paul@0 | 162 | </tr> |
paul@0 | 163 | <tr> |
paul@0 | 164 | <td colspan="2"><input type="submit" value="%(submit_label)s" /></td> |
paul@0 | 165 | </tr> |
paul@0 | 166 | </table>''' % { |
paul@0 | 167 | "link" : wikiutil.escape(link, 1), |
paul@0 | 168 | "title" : wikiutil.escape(title, 1), |
paul@0 | 169 | "intro" : wikiutil.escape(introduction, 1), |
paul@0 | 170 | "url_label" : wikiutil.escape(_("URL")), |
paul@0 | 171 | "title_label" : wikiutil.escape(_("Title")), |
paul@0 | 172 | "intro_label" : wikiutil.escape(_("Introduction")), |
paul@0 | 173 | "description_label" : wikiutil.escape(_("Description")), |
paul@0 | 174 | "submit_label" : wikiutil.escape(_("Submit link")) |
paul@0 | 175 | }) |
paul@0 | 176 | |
paul@0 | 177 | output.append(u'</form>') |
paul@0 | 178 | |
paul@0 | 179 | # Don't show the macro. |
paul@0 | 180 | |
paul@0 | 181 | show_macro = 0 |
paul@0 | 182 | |
paul@0 | 183 | # If the macro is to be shown, emit the usual fields. |
paul@0 | 184 | |
paul@0 | 185 | if show_macro: |
paul@0 | 186 | output.append(u'<form class="macro" method="POST" action="%s/%s">' % ( |
paul@0 | 187 | request.getScriptname(), wikiutil.quoteWikinameURL(page.page_name))) |
paul@0 | 188 | output.append(u'<input type="hidden" name="add_link_to_page" value="%s" />' % |
paul@0 | 189 | wikiutil.escape(identifier, 1)) |
paul@0 | 190 | output.append(u'<div>') |
paul@0 | 191 | output.append(u'<input type="text" name="add_link_to_page_link" />') |
paul@0 | 192 | output.append(u'<input type="submit" value="%s" />' % |
paul@0 | 193 | wikiutil.escape(_("Add link"))) |
paul@0 | 194 | output.append(u'</div>') |
paul@0 | 195 | output.append(u'</form>') |
paul@0 | 196 | |
paul@0 | 197 | return formatter.rawHTML('\n'.join(output)) |
paul@0 | 198 | |
paul@0 | 199 | # vim: tabstop=4 expandtab shiftwidth=4 |