Don't emit separate open and close tags for empty void elements using htmltmpl

This commit is contained in:
Sam Ruby 2007-05-10 20:25:25 -04:00
parent 82753d09a1
commit 9aba1dbfc7
2 changed files with 23 additions and 4 deletions

View File

@ -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)[^>]*)></\2>" % '|'.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]

View File

@ -0,0 +1,13 @@
<!--
Description: xhtml content
Expect: Items[0]['content'] == '<img src="x.jpg" />'
-->
<feed xmlns="http://www.w3.org/2005/Atom">
<entry>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml"><img src="x.jpg"/></div>
</content>
</entry>
</feed>