Back to index

plone3  3.1.7
presentation.py
Go to the documentation of this file.
00001 from zope.i18n import translate
00002 
00003 from Products.Five import BrowserView
00004 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00005 from Products.CMFCore.utils import getToolByName
00006 from Products.CMFPlone import PloneMessageFactory as _
00007 
00008 from plone.memoize.view import memoize
00009 from plone.app.layout.viewlets.common import ViewletBase
00010 
00011 class PresentationView(BrowserView):
00012     template = ViewPageTemplateFile('presentation.pt')
00013 
00014     def __call__(self):
00015         return self.template()
00016 
00017     @memoize
00018     def body(self):
00019         return self.context.CookedBody()
00020 
00021     @memoize
00022     def enabled(self):
00023         getPresentation = getattr(self.context.aq_base, "getPresentation", None)
00024         if getPresentation is None or getPresentation() == False:
00025             return False
00026 
00027         body = self.body()
00028 
00029         # this should be a regex too, but hey this works
00030         tags = ["h1", "H1", "h2", "H2"]
00031         tag = None
00032         for t in tags:
00033             if body.find("<%s>" % t) > -1:
00034                 tag = t
00035                 break
00036 
00037         if tag is None:
00038             return False
00039 
00040         return True
00041 
00042     def content(self):
00043         # ugly, ugly, ugly code, that basically changes the way the slide is put
00044         # together this should be a HTML parser or XSLT or even JS
00045 
00046         body = self.body()
00047 
00048         # this should be a regex too, but hey this works
00049         tags = ["h1", "H1", "h2", "H2"]
00050         tag = None
00051         for t in tags:
00052             if body.find("<%s>" % t) > -1:
00053                 tag = t
00054                 break
00055 
00056         num = int(tag[1])
00057         if num > 1:
00058             new = "%s1" % (tag[0])
00059             body = body.replace("<%s>" % tag, "<%s>" % new)
00060             body = body.replace("</%s>" % tag, "</%s>" % new)
00061             tag = new
00062 
00063         body = body.split('<%s>' % tag)
00064         body = ('</div>\n<div class="slide">\n<%s>' % tag).join(body)
00065         body = body.split('</%s>' % tag)
00066         body = ('</%s>\n' % tag).join(body)
00067 
00068         return body
00069 
00070     @memoize
00071     def creator(self):
00072         return self.context.Creator()
00073 
00074     @memoize
00075     def author(self):
00076         membership = getToolByName(self.context, "portal_membership")
00077         return membership.getMemberInfo(self.creator())
00078 
00079     @memoize
00080     def authorname(self):
00081         author = self.author()
00082         return author and author['fullname'] or self.creator()
00083 
00084 
00085 class PresentationViewlet(ViewletBase):
00086     def update(self):
00087         getPresentation = getattr(self.context.aq_base, "getPresentation", None)
00088         self.presentation_enabled = False
00089         if getPresentation is not None:
00090             try:
00091                 self.presentation_enabled = getPresentation()
00092             except KeyError:
00093                 # schema not updated yet
00094                 self.presentation_enabled = False
00095 
00096     def render(self):
00097         if self.presentation_enabled:
00098             url = "%s/presentation_view" % self.context.absolute_url()
00099             msg = _(u'Also available in presentation mode\u2026')
00100             msg = translate(msg, domain='plone', context=self.request)
00101             return u'<p id="link-presentation"><a href="%s" rel="nofollow" class="link-presentation">%s</a></p>' % (url, msg)
00102         return u''