From 9aba1dbfc7ad273cc57a1f56143ab31fa2f48a87 Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Thu, 10 May 2007 20:25:25 -0400 Subject: [PATCH] Don't emit separate open and close tags for empty void elements using htmltmpl --- planet/shell/tmpl.py | 14 ++++++++++---- tests/data/filter/tmpl/content_xhtml2.xml | 13 +++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/data/filter/tmpl/content_xhtml2.xml diff --git a/planet/shell/tmpl.py b/planet/shell/tmpl.py index 4f4d822..99ae27d 100644 --- a/planet/shell/tmpl.py +++ b/planet/shell/tmpl.py @@ -1,7 +1,10 @@ from xml.sax.saxutils import escape -import sgmllib, time, os, sys, new, urlparse +import sgmllib, time, os, sys, new, urlparse, re from planet import config, feedparser, htmltmpl +voids=feedparser._BaseHTMLProcessor.elements_no_end_tag +empty=re.compile(r"<((%s)[^>]*)>" % '|'.join(voids)) + class stripHtml(sgmllib.SGMLParser): "remove all tags from the data" def __init__(self, data): @@ -130,9 +133,12 @@ def tmpl_mapper(source, rules): node = source for path in rule[2:]: if isinstance(path, str) and path in node: - if path == 'value' and node.get('type','')=='text/plain': - node['value'] = escape(node['value']) - node['type'] = 'text/html' + if path == 'value': + if node.get('type','')=='text/plain': + node['value'] = escape(node['value']) + node['type'] = 'text/html' + elif node.get('type','')=='application/xhtml+xml': + node['value'] = empty.sub(r"<\1 />", node['value']) node = node[path] elif isinstance(path, int): node = node[path] diff --git a/tests/data/filter/tmpl/content_xhtml2.xml b/tests/data/filter/tmpl/content_xhtml2.xml new file mode 100644 index 0000000..d43256d --- /dev/null +++ b/tests/data/filter/tmpl/content_xhtml2.xml @@ -0,0 +1,13 @@ + + + + + +
+
+
+
+