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@13 | 5 | @copyright: 2008, 2012 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@13 | 11 | import xml.dom.pulldom |
paul@13 | 12 | |
paul@14 | 13 | try: |
paul@14 | 14 | from cStringIO import StringIO |
paul@14 | 15 | except ImportError: |
paul@14 | 16 | from StringIO import StringIO |
paul@14 | 17 | |
paul@13 | 18 | Dependencies = ["time"] |
paul@13 | 19 | |
paul@13 | 20 | MAX_ENTRIES = 5 |
paul@13 | 21 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@13 | 22 | |
paul@13 | 23 | def text(element): |
paul@13 | 24 | nodes = [] |
paul@13 | 25 | for node in element.childNodes: |
paul@13 | 26 | if node.nodeType == node.TEXT_NODE: |
paul@13 | 27 | nodes.append(node.nodeValue) |
paul@13 | 28 | return "".join(nodes) |
paul@13 | 29 | |
paul@13 | 30 | def linktext(element, feed_type): |
paul@13 | 31 | if feed_type == "rss": |
paul@13 | 32 | return text(element) |
paul@13 | 33 | else: |
paul@13 | 34 | return element.getAttribute("href") |
paul@13 | 35 | |
paul@13 | 36 | def execute(macro, args): |
paul@13 | 37 | request = macro.request |
paul@13 | 38 | fmt = macro.formatter |
paul@13 | 39 | |
paul@13 | 40 | max_entries = MAX_ENTRIES |
paul@13 | 41 | args = args.split(",") |
paul@13 | 42 | if args: |
paul@13 | 43 | try: |
paul@13 | 44 | feed_url = args[0] |
paul@13 | 45 | max_entries = int(args[1]) |
paul@13 | 46 | except IndexError: |
paul@13 | 47 | pass |
paul@13 | 48 | |
paul@14 | 49 | # Obtain the resource, using a cached version if appropriate. |
paul@13 | 50 | |
paul@14 | 51 | max_cache_age = int(getattr(request.cfg, "moin_share_max_cache_age", "300")) |
paul@14 | 52 | data = getCachedResource(request, feed_url, "MoinShare", "wiki", max_cache_age) |
paul@14 | 53 | if not data: |
paul@14 | 54 | return fmt.text(_("SharedContent: updates could not be retrieved for %s") % feed_url) |
paul@14 | 55 | |
paul@14 | 56 | feed = StringIO(data) |
paul@14 | 57 | _url = feed.readline() |
paul@14 | 58 | _content_type = feed.readline() |
paul@13 | 59 | |
paul@13 | 60 | try: |
paul@13 | 61 | # Parse each node from the feed. |
paul@13 | 62 | |
paul@13 | 63 | title = link = None |
paul@13 | 64 | channel_title = channel_link = None |
paul@13 | 65 | |
paul@13 | 66 | output = [] |
paul@13 | 67 | append = output.append |
paul@13 | 68 | append(fmt.bullet_list(on=1)) |
paul@13 | 69 | |
paul@13 | 70 | feed_type = None |
paul@13 | 71 | in_item = False |
paul@13 | 72 | nentries = 0 |
paul@13 | 73 | |
paul@13 | 74 | events = xml.dom.pulldom.parse(feed) |
paul@13 | 75 | |
paul@13 | 76 | for event, value in events: |
paul@13 | 77 | |
paul@13 | 78 | if event == xml.dom.pulldom.START_ELEMENT: |
paul@13 | 79 | tagname = value.localName |
paul@13 | 80 | |
paul@13 | 81 | # Detect the feed type and items. |
paul@13 | 82 | |
paul@13 | 83 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@13 | 84 | feed_type = "atom" |
paul@13 | 85 | |
paul@13 | 86 | elif tagname == "rss": |
paul@13 | 87 | feed_type = "rss" |
paul@13 | 88 | |
paul@13 | 89 | # Detect items. |
paul@13 | 90 | |
paul@13 | 91 | elif feed_type == "rss" and tagname == "item" or \ |
paul@13 | 92 | feed_type == "atom" and tagname == "entry": |
paul@13 | 93 | |
paul@13 | 94 | in_item = True |
paul@13 | 95 | |
paul@13 | 96 | elif tagname == "title": |
paul@13 | 97 | events.expandNode(value) |
paul@13 | 98 | if in_item: |
paul@13 | 99 | title = value |
paul@13 | 100 | else: |
paul@13 | 101 | channel_title = value |
paul@13 | 102 | |
paul@13 | 103 | elif tagname == "link": |
paul@13 | 104 | events.expandNode(value) |
paul@13 | 105 | if in_item: |
paul@13 | 106 | link = value |
paul@13 | 107 | else: |
paul@13 | 108 | channel_link = value |
paul@13 | 109 | |
paul@13 | 110 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@13 | 111 | tagname = value.localName |
paul@13 | 112 | |
paul@13 | 113 | if feed_type == "rss" and tagname == "item" or \ |
paul@13 | 114 | feed_type == "atom" and tagname == "entry": |
paul@13 | 115 | |
paul@13 | 116 | in_item = False |
paul@13 | 117 | |
paul@13 | 118 | # Emit title and link information for items. |
paul@13 | 119 | |
paul@13 | 120 | if title and link and nentries < max_entries: |
paul@13 | 121 | link_text = linktext(link, feed_type) |
paul@13 | 122 | |
paul@13 | 123 | append(fmt.listitem(on=1)) |
paul@13 | 124 | append(fmt.url(on=1, href=link_text)) |
paul@13 | 125 | append(fmt.icon('www')) |
paul@13 | 126 | append(fmt.text(" " + text(title))) |
paul@13 | 127 | append(fmt.url(on=0)) |
paul@13 | 128 | append(fmt.listitem(on=0)) |
paul@13 | 129 | |
paul@13 | 130 | title = link = None |
paul@13 | 131 | nentries += 1 |
paul@13 | 132 | |
paul@13 | 133 | append(fmt.bullet_list(on=0)) |
paul@13 | 134 | |
paul@13 | 135 | if channel_title and channel_link: |
paul@13 | 136 | channel_link_text = linktext(channel_link, feed_type) |
paul@13 | 137 | |
paul@13 | 138 | append(fmt.paragraph(on=1)) |
paul@13 | 139 | append(fmt.url(on=1, href=channel_link_text)) |
paul@13 | 140 | append(fmt.text(text(channel_title))) |
paul@13 | 141 | append(fmt.url(on=0)) |
paul@13 | 142 | append(fmt.text(" ")) |
paul@13 | 143 | append(fmt.url(on=1, href=feed_url)) |
paul@13 | 144 | append(fmt.icon('rss')) |
paul@13 | 145 | append(fmt.url(on=0)) |
paul@13 | 146 | append(fmt.paragraph(on=0)) |
paul@13 | 147 | |
paul@13 | 148 | finally: |
paul@13 | 149 | feed.close() |
paul@13 | 150 | |
paul@13 | 151 | return ''.join(output) |
paul@13 | 152 | |
paul@13 | 153 | # vim: tabstop=4 expandtab shiftwidth=4 |