root/tags/amplee-0.5.0/amplee/examples/cooker/core/utils.py

Revision 428, 2.7 kB (checked in by sylvain, 1 year ago)

Fixed the example to the latest modification in amplee

Line 
1 # -*- coding: utf-8 -*-
2
3 import os.path
4 from StringIO import StringIO
5 import markdown
6 from xml.sax.saxutils import unescape
7 from bridge import Element as E
8
9 __all__ = ['transform_member_resource']
10
11 def transform_member_resource(member, xslt_path=None):
12     """Transforms the member entry content from Markdown to HTML
13     then returns a public version of the member entry.
14
15     This entry can be used for the public feed aggregation for instance.
16
17     You may pass the URI or path of an XSLT resource to be inserted as
18     a processing instruction.
19     """
20     # To generate a public atom entry from the member resource
21     # we first extract the content from the media resource
22     info = member.collection.get_content_info(member.media_id)
23
24     if not member.collection.contains(info, as_media=True):
25         return
26
27     # When using the TarFileStorage the returned path is in fact
28     # a TarFileStoragePathInfo instance
29     # But since we precisely know where to look in the tarball for the content
30     # we specify exactly that we only want the content of the file
31     # called 'recipe' within the tarball
32     info.key.archive_sub_path = 'recipe'
33
34     if not member.collection.contains(info, as_media=True):
35         return
36    
37     # This returns the actual content as a string
38     content = member.collection.get_content(info)[0][1]
39
40     # Nextwe transform the Markdown content into XHTML
41     recipe_html = u''
42     if content:
43         recipe_html = markdown.Markdown(content, safe_mode=True).toString()
44         recipe_html = recipe_html.strip()
45
46     # Now if we also have a image attached to this resource we
47     # append the according Markdown entity so that it will
48     # transform it into a <img /> XHTML tag
49     if os.path.exists(os.path.join('static/photos/%s' % member.media_id)):
50         title = member.atom.get_child('title', member.atom.xml_ns).xml_text
51         recipe_html += '<img alt="%s" src="%s" title="%s" />' % (title, '/static/photos/%s' %
52                                                                  member.media_id, title)
53    
54     recipe_html = unescape(recipe_html)
55        
56     # Finally we create a public atom entry from the member resource entry
57     public = member.prepare_for_public(content=recipe_html, media_type=u'application/xml',
58                                        xslt_path=xslt_path)
59
60     # We indicate where the comments to the recipe will be found
61     # by using RFC 4685
62     attributes = {u'rel': u'replies', u'type': u'application/atom+xml',
63                   u'href': u'%s/comments' % member.public_uri}
64     E(u'link', attributes=attributes, prefix=public.xml_root.xml_prefix,
65       namespace=public.xml_root.xml_ns, parent=public.xml_root)
66
67
68     return public   
69  
Note: See TracBrowser for help on using the browser.