Back to index

plone3  3.1.7
text_to_html.py
Go to the documentation of this file.
00001 from Products.PortalTransforms.interfaces import itransform
00002 from DocumentTemplate.DT_Util import html_quote
00003 
00004 __revision__ = '$Id: text_to_html.py 4787 2005-08-19 21:43:41Z dreamcatcher $'
00005 
00006 class TextToHTML:
00007     """simple transform which wrap raw text in a verbatim environment"""
00008 
00009     __implements__ = itransform
00010 
00011     __name__ = "text_to_html"
00012     output = "text/html"
00013 
00014     def __init__(self, name=None, inputs=('text/plain',)):
00015         self.config = { 'inputs' : inputs, }
00016         self.config_metadata = {
00017             'inputs' : ('list', 'Inputs', 'Input(s) MIME type. Change with care.'),
00018             }
00019         if name:
00020             self.__name__ = name
00021 
00022     def name(self):
00023         return self.__name__
00024 
00025     def __getattr__(self, attr):
00026         if attr == 'inputs':
00027             return self.config['inputs']
00028         if attr == 'output':
00029             return self.config['output']
00030         raise AttributeError(attr)
00031 
00032     def convert(self, orig, data, **kwargs):
00033         # Replaces all line breaks with a br tag, and wraps it in a p tag.
00034         data.setData('<p>%s</p>' % html_quote(orig.strip()).replace('\n', '<br />'))
00035         return data
00036 
00037 def register():
00038     return TextToHTML()