paul@13 | 1 | # -*- coding: iso-8859-1 -*- |
paul@13 | 2 | """ |
paul@13 | 3 | MoinMoin - SharedContent macro, based on the FeedReader macro |
paul@13 | 4 | |
paul@18 | 5 | @copyright: 2008, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@13 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@13 | 7 | """ |
paul@13 | 8 | |
paul@13 | 9 | from MoinMoin.Page import Page |
paul@14 | 10 | from MoinRemoteSupport import * |
paul@20 | 11 | from MoinSupport import parseMacroArguments |
paul@13 | 12 | import xml.dom.pulldom |
paul@13 | 13 | |
paul@14 | 14 | try: |
paul@14 | 15 | from cStringIO import StringIO |
paul@14 | 16 | except ImportError: |
paul@14 | 17 | from StringIO import StringIO |
paul@14 | 18 | |
paul@13 | 19 | Dependencies = ["time"] |
paul@13 | 20 | |
paul@13 | 21 | MAX_ENTRIES = 5 |
paul@13 | 22 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@13 | 23 | |
paul@13 | 24 | def text(element): |
paul@13 | 25 | nodes = [] |
paul@13 | 26 | for node in element.childNodes: |
paul@13 | 27 | if node.nodeType == node.TEXT_NODE: |
paul@13 | 28 | nodes.append(node.nodeValue) |
paul@13 | 29 | return "".join(nodes) |
paul@13 | 30 | |
paul@20 | 31 | def unescape(text): |
paul@20 | 32 | return text.replace("<", "<").replace(">", ">").replace("&", "&") |
paul@20 | 33 | |
paul@13 | 34 | def linktext(element, feed_type): |
paul@13 | 35 | if feed_type == "rss": |
paul@13 | 36 | return text(element) |
paul@13 | 37 | else: |
paul@13 | 38 | return element.getAttribute("href") |
paul@13 | 39 | |
paul@13 | 40 | def execute(macro, args): |
paul@13 | 41 | request = macro.request |
paul@13 | 42 | fmt = macro.formatter |
paul@20 | 43 | _ = request.getText |
paul@13 | 44 | |
paul@20 | 45 | feed_url = None |
paul@20 | 46 | show_content = None |
paul@20 | 47 | max_entries = None |
paul@20 | 48 | |
paul@20 | 49 | for arg, value in parseMacroArguments(args): |
paul@20 | 50 | if arg == "url": |
paul@20 | 51 | feed_url = value |
paul@20 | 52 | elif arg == "show": |
paul@20 | 53 | show_content = value in ("true", "True", "yes") |
paul@20 | 54 | elif arg == "limit": |
paul@20 | 55 | try: |
paul@20 | 56 | max_entries = int(value) |
paul@20 | 57 | except ValueError: |
paul@20 | 58 | return fmt.text(_("SharedContent: limit must be set to the maximum number of entries to be shown")) |
paul@20 | 59 | |
paul@20 | 60 | if not feed_url: |
paul@20 | 61 | return fmt.text(_("SharedContent: a feed URL must be specified")) |
paul@20 | 62 | |
paul@20 | 63 | show_content = show_content or False |
paul@20 | 64 | max_entries = max_entries or MAX_ENTRIES |
paul@13 | 65 | |
paul@14 | 66 | # Obtain the resource, using a cached version if appropriate. |
paul@13 | 67 | |
paul@14 | 68 | max_cache_age = int(getattr(request.cfg, "moin_share_max_cache_age", "300")) |
paul@14 | 69 | data = getCachedResource(request, feed_url, "MoinShare", "wiki", max_cache_age) |
paul@14 | 70 | if not data: |
paul@14 | 71 | return fmt.text(_("SharedContent: updates could not be retrieved for %s") % feed_url) |
paul@14 | 72 | |
paul@14 | 73 | feed = StringIO(data) |
paul@18 | 74 | |
paul@20 | 75 | _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed) |
paul@20 | 76 | |
paul@20 | 77 | if content_type not in ("application/atom+xml", "application/rss+xml"): |
paul@20 | 78 | return fmt.text(_("SharedContent: updates for %s were not provided in Atom or RSS format") % feed_url) |
paul@13 | 79 | |
paul@13 | 80 | try: |
paul@13 | 81 | # Parse each node from the feed. |
paul@13 | 82 | |
paul@20 | 83 | title = link = content = content_type = None |
paul@13 | 84 | channel_title = channel_link = None |
paul@13 | 85 | |
paul@13 | 86 | output = [] |
paul@13 | 87 | append = output.append |
paul@13 | 88 | |
paul@13 | 89 | feed_type = None |
paul@13 | 90 | in_item = False |
paul@13 | 91 | nentries = 0 |
paul@13 | 92 | |
paul@13 | 93 | events = xml.dom.pulldom.parse(feed) |
paul@13 | 94 | |
paul@20 | 95 | if not show_content: |
paul@20 | 96 | append(fmt.bullet_list(on=1)) |
paul@20 | 97 | |
paul@13 | 98 | for event, value in events: |
paul@13 | 99 | |
paul@13 | 100 | if event == xml.dom.pulldom.START_ELEMENT: |
paul@13 | 101 | tagname = value.localName |
paul@13 | 102 | |
paul@13 | 103 | # Detect the feed type and items. |
paul@13 | 104 | |
paul@13 | 105 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@13 | 106 | feed_type = "atom" |
paul@13 | 107 | |
paul@13 | 108 | elif tagname == "rss": |
paul@13 | 109 | feed_type = "rss" |
paul@13 | 110 | |
paul@13 | 111 | # Detect items. |
paul@13 | 112 | |
paul@13 | 113 | elif feed_type == "rss" and tagname == "item" or \ |
paul@13 | 114 | feed_type == "atom" and tagname == "entry": |
paul@13 | 115 | |
paul@13 | 116 | in_item = True |
paul@13 | 117 | |
paul@13 | 118 | elif tagname == "title": |
paul@13 | 119 | events.expandNode(value) |
paul@13 | 120 | if in_item: |
paul@13 | 121 | title = value |
paul@13 | 122 | else: |
paul@13 | 123 | channel_title = value |
paul@13 | 124 | |
paul@13 | 125 | elif tagname == "link": |
paul@13 | 126 | events.expandNode(value) |
paul@13 | 127 | if in_item: |
paul@13 | 128 | link = value |
paul@13 | 129 | else: |
paul@13 | 130 | channel_link = value |
paul@13 | 131 | |
paul@20 | 132 | elif feed_type == "atom" and tagname == "content": |
paul@20 | 133 | events.expandNode(value) |
paul@20 | 134 | if in_item: |
paul@20 | 135 | content = value |
paul@20 | 136 | content_type = value.getAttribute("type") |
paul@20 | 137 | |
paul@13 | 138 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@13 | 139 | tagname = value.localName |
paul@13 | 140 | |
paul@13 | 141 | if feed_type == "rss" and tagname == "item" or \ |
paul@13 | 142 | feed_type == "atom" and tagname == "entry": |
paul@13 | 143 | |
paul@13 | 144 | in_item = False |
paul@13 | 145 | |
paul@20 | 146 | # Emit content where appropriate. |
paul@20 | 147 | # NOTE: HTML should be sanitised. |
paul@13 | 148 | |
paul@20 | 149 | if show_content: |
paul@20 | 150 | if content and content_type == "html": |
paul@20 | 151 | append(fmt.rawHTML(unescape(text(content)))) |
paul@20 | 152 | |
paul@20 | 153 | # Or emit title and link information for items. |
paul@20 | 154 | |
paul@20 | 155 | elif title and link and nentries < max_entries: |
paul@13 | 156 | link_text = linktext(link, feed_type) |
paul@13 | 157 | |
paul@13 | 158 | append(fmt.listitem(on=1)) |
paul@13 | 159 | append(fmt.url(on=1, href=link_text)) |
paul@13 | 160 | append(fmt.icon('www')) |
paul@13 | 161 | append(fmt.text(" " + text(title))) |
paul@13 | 162 | append(fmt.url(on=0)) |
paul@13 | 163 | append(fmt.listitem(on=0)) |
paul@13 | 164 | |
paul@20 | 165 | title = link = content = content_type = None |
paul@13 | 166 | nentries += 1 |
paul@13 | 167 | |
paul@20 | 168 | if not show_content: |
paul@20 | 169 | append(fmt.bullet_list(on=0)) |
paul@13 | 170 | |
paul@13 | 171 | if channel_title and channel_link: |
paul@13 | 172 | channel_link_text = linktext(channel_link, feed_type) |
paul@13 | 173 | |
paul@13 | 174 | append(fmt.paragraph(on=1)) |
paul@13 | 175 | append(fmt.url(on=1, href=channel_link_text)) |
paul@13 | 176 | append(fmt.text(text(channel_title))) |
paul@13 | 177 | append(fmt.url(on=0)) |
paul@13 | 178 | append(fmt.text(" ")) |
paul@13 | 179 | append(fmt.url(on=1, href=feed_url)) |
paul@13 | 180 | append(fmt.icon('rss')) |
paul@13 | 181 | append(fmt.url(on=0)) |
paul@13 | 182 | append(fmt.paragraph(on=0)) |
paul@13 | 183 | |
paul@13 | 184 | finally: |
paul@13 | 185 | feed.close() |
paul@13 | 186 | |
paul@13 | 187 | return ''.join(output) |
paul@13 | 188 | |
paul@13 | 189 | # vim: tabstop=4 expandtab shiftwidth=4 |