# HG changeset patch # User Paul Boddie # Date 1368566079 -7200 # Node ID 41468208408e943089bcc0b7ca79fadbe3784b95 # Parent 503e7de192db1e9561a71718f1cafa20ab85d980 Normalised media/content types and made use of existing parser/formatter mechanisms for rendering update content. diff -r 503e7de192db -r 41468208408e MoinShare.py --- a/MoinShare.py Tue May 14 17:16:56 2013 +0200 +++ b/MoinShare.py Tue May 14 23:14:39 2013 +0200 @@ -180,7 +180,7 @@ fmt.setPage(page) update.content = formatText(body, request, fmt, parser_cls) - update.content_type = "html" + update.content_type = "text/html" update.link = page.url(request) update.updated = updated diff -r 503e7de192db -r 41468208408e macros/SharedContent.py --- a/macros/SharedContent.py Tue May 14 17:16:56 2013 +0200 +++ b/macros/SharedContent.py Tue May 14 23:14:39 2013 +0200 @@ -9,7 +9,7 @@ from DateSupport import getDateTimeFromISO8601, DateTime from MoinMoin.Page import Page from MoinRemoteSupport import * -from MoinSupport import parseMacroArguments +from MoinSupport import parseMacroArguments, getParsersForContentType, formatText from MoinShare import getUpdateSources, getUpdatesFromPage, Update from email.utils import parsedate import xml.dom.pulldom @@ -33,6 +33,12 @@ nodes.append(node.nodeValue) return "".join(nodes) +def children(element): + nodes = [] + for node in element.childNodes: + nodes.append(node.toxml()) + return "".join(nodes) + def unescape(text): return text.replace("<", "<").replace(">", ">").replace("&", "&") @@ -152,12 +158,21 @@ if update and (need_content(show_content, tagname) or tagname == "summary" and not update.content): if feed_type == "atom": update.content_type = value.getAttribute("type") or "text" + + # Normalise the content types and extract the + # content. + if update.content_type in ("xhtml", "application/xhtml+xml", "application/xml"): - update.content = value.toxml() + update.content = children(value) + update.content_type = "application/xhtml+xml" + elif update.content_type in ("html", "text/html"): + update.content = text(value) + update.content_type = "text/html" else: update.content = text(value) + update.content_type = "text/plain" else: - update.content_type = "html" + update.content_type = "text/html" update.content = text(value) elif feed_type == "atom" and tagname == "updated" or \ @@ -296,18 +311,22 @@ for update in updates: # Emit content where appropriate. - # NOTE: HTML and XHTML should be sanitised. + # NOTE: Some control over the HTML and XHTML should be exercised. if show_content: append(fmt.div(on=1, css_class="moinshare-update")) append(fmt.div(on=1, css_class="moinshare-content")) + if update.content: - if update.content_type in ("html", "text/html"): - append(fmt.rawHTML(unescape(update.content))) - elif update.content_type in ("xhtml", "application/xhtml+xml"): - append(fmt.rawHTML(update.content)) - elif update.content_type in ("text", "text/plain"): - append(fmt.text(update.content)) + parsers = getParsersForContentType(request.cfg, update.content_type) + + if parsers: + for parser_cls in parsers: + append(formatText(update.content, request, fmt, parser_cls=parser_cls)) + break + else: + append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type)) + append(fmt.div(on=0)) append(fmt.div(on=1, css_class="moinshare-date")) append(fmt.text(str(update.updated)))