# -*- coding: latin-1 -*-
""" 
   Copyright (C) 2005 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""
import DateTime

def strip_html_bodies(html_text):
	" HTML text without headers "
	if not html_text:
		return ''
	start = html_text.find('<body')
	if start == -1:
		return html_text
	start = html_text.find('>', start)
	end = html_text.find('</body>')
	return html_text[start+1:end]


# You can define this function directly in zope : zopeobject = self
def autostrftime(zopeobject, date, format="%d %b %Y %Hh%M", encoding=None, ifnone="-"):
	" auto string.strftime "
	if not date:
		return ifnone
	date_f = date.strftime(format)
	if encoding:
		return date_f.encode(encoding)
	return date_f

context_special_chars =	{ '#': '\#',
			  '$': '\$',
			  '%': '\%',
			  '&': '\&',
			  '~': '\\textasciitilde{}',
			  '_': '\\textunderscore{}',
			  '^': '\\textasciicircum{}',
			  '\\': '\\textbackslash{}',
			  '{': '\\textbraceleft{}',
			  '}': '\\textbraceright{}',
			  '|': '$|$',
			  '<': '$<$',
			  '>': '$>$',
			  '\n': '\\crlf\n' }

def escape_context(zopeobject, text):
	" Escape special characters in a ConTeXt string "
	if not text:
		return ''
	return ''.join(map(lambda x: context_special_chars.get(x, x), text))

