1.1 --- a/macros/SharedContent.py Mon May 06 19:44:51 2013 +0200
1.2 +++ b/macros/SharedContent.py Tue May 07 00:19:47 2013 +0200
1.3 @@ -8,6 +8,7 @@
1.4
1.5 from MoinMoin.Page import Page
1.6 from MoinRemoteSupport import *
1.7 +from MoinSupport import parseMacroArguments
1.8 import xml.dom.pulldom
1.9
1.10 try:
1.11 @@ -27,6 +28,9 @@
1.12 nodes.append(node.nodeValue)
1.13 return "".join(nodes)
1.14
1.15 +def unescape(text):
1.16 + return text.replace("<", "<").replace(">", ">").replace("&", "&")
1.17 +
1.18 def linktext(element, feed_type):
1.19 if feed_type == "rss":
1.20 return text(element)
1.21 @@ -36,15 +40,28 @@
1.22 def execute(macro, args):
1.23 request = macro.request
1.24 fmt = macro.formatter
1.25 + _ = request.getText
1.26
1.27 - max_entries = MAX_ENTRIES
1.28 - args = args.split(",")
1.29 - if args:
1.30 - try:
1.31 - feed_url = args[0]
1.32 - max_entries = int(args[1])
1.33 - except IndexError:
1.34 - pass
1.35 + feed_url = None
1.36 + show_content = None
1.37 + max_entries = None
1.38 +
1.39 + for arg, value in parseMacroArguments(args):
1.40 + if arg == "url":
1.41 + feed_url = value
1.42 + elif arg == "show":
1.43 + show_content = value in ("true", "True", "yes")
1.44 + elif arg == "limit":
1.45 + try:
1.46 + max_entries = int(value)
1.47 + except ValueError:
1.48 + return fmt.text(_("SharedContent: limit must be set to the maximum number of entries to be shown"))
1.49 +
1.50 + if not feed_url:
1.51 + return fmt.text(_("SharedContent: a feed URL must be specified"))
1.52 +
1.53 + show_content = show_content or False
1.54 + max_entries = max_entries or MAX_ENTRIES
1.55
1.56 # Obtain the resource, using a cached version if appropriate.
1.57
1.58 @@ -55,17 +72,19 @@
1.59
1.60 feed = StringIO(data)
1.61
1.62 - _url, _content_type, _encoding, _metadata = getCachedResourceMetadata(feed)
1.63 + _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed)
1.64 +
1.65 + if content_type not in ("application/atom+xml", "application/rss+xml"):
1.66 + return fmt.text(_("SharedContent: updates for %s were not provided in Atom or RSS format") % feed_url)
1.67
1.68 try:
1.69 # Parse each node from the feed.
1.70
1.71 - title = link = None
1.72 + title = link = content = content_type = None
1.73 channel_title = channel_link = None
1.74
1.75 output = []
1.76 append = output.append
1.77 - append(fmt.bullet_list(on=1))
1.78
1.79 feed_type = None
1.80 in_item = False
1.81 @@ -73,6 +92,9 @@
1.82
1.83 events = xml.dom.pulldom.parse(feed)
1.84
1.85 + if not show_content:
1.86 + append(fmt.bullet_list(on=1))
1.87 +
1.88 for event, value in events:
1.89
1.90 if event == xml.dom.pulldom.START_ELEMENT:
1.91 @@ -107,6 +129,12 @@
1.92 else:
1.93 channel_link = value
1.94
1.95 + elif feed_type == "atom" and tagname == "content":
1.96 + events.expandNode(value)
1.97 + if in_item:
1.98 + content = value
1.99 + content_type = value.getAttribute("type")
1.100 +
1.101 elif event == xml.dom.pulldom.END_ELEMENT:
1.102 tagname = value.localName
1.103
1.104 @@ -115,9 +143,16 @@
1.105
1.106 in_item = False
1.107
1.108 - # Emit title and link information for items.
1.109 + # Emit content where appropriate.
1.110 + # NOTE: HTML should be sanitised.
1.111
1.112 - if title and link and nentries < max_entries:
1.113 + if show_content:
1.114 + if content and content_type == "html":
1.115 + append(fmt.rawHTML(unescape(text(content))))
1.116 +
1.117 + # Or emit title and link information for items.
1.118 +
1.119 + elif title and link and nentries < max_entries:
1.120 link_text = linktext(link, feed_type)
1.121
1.122 append(fmt.listitem(on=1))
1.123 @@ -127,10 +162,11 @@
1.124 append(fmt.url(on=0))
1.125 append(fmt.listitem(on=0))
1.126
1.127 - title = link = None
1.128 + title = link = content = content_type = None
1.129 nentries += 1
1.130
1.131 - append(fmt.bullet_list(on=0))
1.132 + if not show_content:
1.133 + append(fmt.bullet_list(on=0))
1.134
1.135 if channel_title and channel_link:
1.136 channel_link_text = linktext(channel_link, feed_type)