scribeengine/scribeengine/lib/helpers.py

102 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# ScribeEngine - Open Source Blog Software #
# --------------------------------------------------------------------------- #
# Copyright (c) 2010 Raoul Snyman #
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
Helper functions
Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to both as 'h'.
"""
import logging
from webhelpers.html import escape, HTML, literal, url_escape
from webhelpers.date import distance_of_time_in_words
from pylons import session, url as url_for
from pylons.controllers.util import redirect_to
log = logging.getLogger(__name__)
class Flash(object):
def set_message(self, message_text, message_type, message_head=None):
session[u'flash.text'] = message_text
session[u'flash.type'] = message_type
session[u'flash.head'] = message_head
session.save()
def has_message(self):
return session.get(u'flash.text')
def has_header(self):
return session.get(u'flash.head')
def get_message_text(self):
message_text = session.pop(u'flash.text', None)
if not message_text:
return None
session.save()
return message_text
def get_message_type(self):
message_type = session.pop(u'flash.type', None)
if not message_type:
return None
session.save()
return message_type
def teaser(text):
position = text.find(u'</p>')
if position > 0:
return text[:position + 4]
elif len(text) > 300:
text = text[:297]
position = len(text) - 1
while position > 0 and text[position] not in [u'<', u'>']:
position -= 1
if position != 0 and text[position + 1] == u'/':
position -= 1
while position > 0 and text[position] not in [u'<']:
position -= 1
if position != 0 and text[position] == u'<':
text = text[:position]
return text
def comment(text):
text = text.replace(u'\r\n', u'\n')
paralist = escape(text).split(u'\n\n')
paragraphs = u'</p><p>'.join([para.strip(u'\n') for para in paralist])
text = paragraphs.replace(u'\n', u'<br />')
return literal(text)
def url_for_post(post):
#TODO: this is hard coded.
return url_for(
controller=u'blog',
action=u'view',
year=post.created.strftime('%Y'),
month=post.created.strftime('%m'),
day=post.created.strftime('%d'),
url=post.url
)
flash = Flash()