| 1 |
|
|---|
| 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 |
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
info.key.archive_sub_path = 'recipe' |
|---|
| 33 |
|
|---|
| 34 |
if not member.collection.contains(info, as_media=True): |
|---|
| 35 |
return |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
content = member.collection.get_content(info)[0][1] |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 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 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 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 |
|
|---|
| 57 |
public = member.prepare_for_public(content=recipe_html, media_type=u'application/xml', |
|---|
| 58 |
xslt_path=xslt_path) |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 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 |
|
|---|