# HG changeset patch # User Paul Boddie # Date 1367878787 -7200 # Node ID 6338390c5c9c0536f1a6066ad197be8af87c70c0 # Parent 74d75b5146d279aa98edbd20e04b1ffa6b5c65dc Added support for showing any HTML content associated with each update. Adjusted the macro argument processing, requiring explicit argument names. diff -r 74d75b5146d2 -r 6338390c5c9c macros/SharedContent.py --- a/macros/SharedContent.py Mon May 06 19:44:51 2013 +0200 +++ b/macros/SharedContent.py Tue May 07 00:19:47 2013 +0200 @@ -8,6 +8,7 @@ from MoinMoin.Page import Page from MoinRemoteSupport import * +from MoinSupport import parseMacroArguments import xml.dom.pulldom try: @@ -27,6 +28,9 @@ nodes.append(node.nodeValue) return "".join(nodes) +def unescape(text): + return text.replace("<", "<").replace(">", ">").replace("&", "&") + def linktext(element, feed_type): if feed_type == "rss": return text(element) @@ -36,15 +40,28 @@ def execute(macro, args): request = macro.request fmt = macro.formatter + _ = request.getText - max_entries = MAX_ENTRIES - args = args.split(",") - if args: - try: - feed_url = args[0] - max_entries = int(args[1]) - except IndexError: - pass + feed_url = None + show_content = None + max_entries = None + + for arg, value in parseMacroArguments(args): + if arg == "url": + feed_url = value + elif arg == "show": + show_content = value in ("true", "True", "yes") + elif arg == "limit": + try: + max_entries = int(value) + except ValueError: + return fmt.text(_("SharedContent: limit must be set to the maximum number of entries to be shown")) + + if not feed_url: + return fmt.text(_("SharedContent: a feed URL must be specified")) + + show_content = show_content or False + max_entries = max_entries or MAX_ENTRIES # Obtain the resource, using a cached version if appropriate. @@ -55,17 +72,19 @@ feed = StringIO(data) - _url, _content_type, _encoding, _metadata = getCachedResourceMetadata(feed) + _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed) + + if content_type not in ("application/atom+xml", "application/rss+xml"): + return fmt.text(_("SharedContent: updates for %s were not provided in Atom or RSS format") % feed_url) try: # Parse each node from the feed. - title = link = None + title = link = content = content_type = None channel_title = channel_link = None output = [] append = output.append - append(fmt.bullet_list(on=1)) feed_type = None in_item = False @@ -73,6 +92,9 @@ events = xml.dom.pulldom.parse(feed) + if not show_content: + append(fmt.bullet_list(on=1)) + for event, value in events: if event == xml.dom.pulldom.START_ELEMENT: @@ -107,6 +129,12 @@ else: channel_link = value + elif feed_type == "atom" and tagname == "content": + events.expandNode(value) + if in_item: + content = value + content_type = value.getAttribute("type") + elif event == xml.dom.pulldom.END_ELEMENT: tagname = value.localName @@ -115,9 +143,16 @@ in_item = False - # Emit title and link information for items. + # Emit content where appropriate. + # NOTE: HTML should be sanitised. - if title and link and nentries < max_entries: + if show_content: + if content and content_type == "html": + append(fmt.rawHTML(unescape(text(content)))) + + # Or emit title and link information for items. + + elif title and link and nentries < max_entries: link_text = linktext(link, feed_type) append(fmt.listitem(on=1)) @@ -127,10 +162,11 @@ append(fmt.url(on=0)) append(fmt.listitem(on=0)) - title = link = None + title = link = content = content_type = None nentries += 1 - append(fmt.bullet_list(on=0)) + if not show_content: + append(fmt.bullet_list(on=0)) if channel_title and channel_link: channel_link_text = linktext(channel_link, feed_type)