Skip to content

Commit 8b18122

Browse files
committed
Support of different types of content
Now the content element now supports the type attribute and the content is encoded accordingly: - text and html are escaped - xhtml is surrounded with a div and then included as XML - XML is directly included - base64 support was *not* added. Please file a bug if you need it
1 parent 30b6194 commit 8b18122

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

feedgen/entry.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,28 @@ def atom_entry(self, feed, extensions=True):
9595

9696
if self.__atom_content:
9797
content = etree.SubElement(entry, 'content')
98+
type = self.__atom_content.get('type')
9899
if self.__atom_content.get('src'):
99100
content.attrib['src'] = self.__atom_content['src']
100101
elif self.__atom_content.get('content'):
101-
content.text = self.__atom_content.get('content')
102+
# Surround xhtml with a div tag, parse it and embed it
103+
if type == 'xhtml':
104+
content.append(etree.fromstring('''<div
105+
xmlns="http://www.w3.org/1999/xhtml">%s</div>''' % \
106+
self.__atom_content.get('content')))
107+
# Parse XML and embed it
108+
elif type.endswith('/xml') or type.endswith('+xml'):
109+
content.append(etree.fromstring(self.__atom_content['content']))
110+
# Emed the text in escaped form
111+
elif not type or type.startswith('text') or type == 'html':
112+
content.text = self.__atom_content.get('content')
113+
# Everything else should be included base64 encoded
114+
else:
115+
raise ValueError('base64 encoded content is not supported at the moment.'
116+
+ 'If you are interested , please file a bug report.')
117+
# Add type description of the content
118+
if type:
119+
content.attrib['type'] = type
102120

103121
for l in self.__atom_link or []:
104122
link = etree.SubElement(entry, 'link', href=l['href'])
@@ -311,7 +329,7 @@ def author(self, author=None, replace=False, **kwargs):
311329
return self.__atom_author
312330

313331

314-
def content(self, content=None, src=None):
332+
def content(self, content=None, src=None, type=None):
315333
'''Get or set the cntent of the entry which contains or links to the
316334
complete content of the entry. Content must be provided for ATOM entries
317335
if there is no alternate link, and should be provided if there is no
@@ -326,6 +344,8 @@ def content(self, content=None, src=None):
326344
self.__atom_content = {'src':src}
327345
elif not content is None:
328346
self.__atom_content = {'content':content}
347+
if not type is None:
348+
self.__atom_content['type'] = type
329349
self.__rss_description = content
330350
return self.__atom_content
331351

0 commit comments

Comments
 (0)