Skip to content

Instantly share code, notes, and snippets.

@mitchellrj
Forked from enko/teaser.py
Created February 20, 2012 15:42
Show Gist options
  • Save mitchellrj/1869712 to your computer and use it in GitHub Desktop.
Save mitchellrj/1869712 to your computer and use it in GitHub Desktop.
teaser behavior
## Script (Python) "get_teaser"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##
# Put this file in a skins directory, call it by doing context/get_teaser or context.get_teaser()
from teaser.module.name import ITeaser
try:
return ITeaser(context).teaser
except TypeError:
return None
"""Behavior to automaticly create a teaser
Checks the description, if nothing is entered, we will generate something from the body.
"""
from AccessControl import ModuleSecurityInfo
from rwproperty import getproperty, setproperty
from zope.interface import implements, alsoProvides
from zope.component import adapts
from plone.directives import form
from Products.CMFCore.interfaces import IDublinCore
import nltk.data
security = ModuleSecurityInfo('teaser.module.name')
class ITeaser(form.Schema):
pass
security.declarePublic('ITeaser')
class Teaser(object):
implements(ITeaser)
teaser_size = 200
def __init__(self, context):
self.context = context
self.context.teaser = self.teaser
@getproperty
def teaser(self):
if (len(self.context.description) > 0):
return self.context.description
else:
self.context.body._outputMimeType = 'text/plain'
text = self.context.body.output
tokenizer = nltk.data.load('tokenizers/punkt/german.pickle')
tokens = tokenizer.tokenize(text)
retval = ''
for token in tokens:
if (len(retval) == 0):
retval = token
else:
if ((len(retval) + len(token)) < self.teaser_size):
retval = "%s %s" % (retval,token)
else:
break
return retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment