Maarten Kling
Maarten Kling
5 februari 2014

web_intelligent_plain_text_to_html

When using ‘web_intelligent_plain_text_to_html’ from portal_transforms, make sure your input is decoded to utf-8 and the output is unicode.

Creating the option for users to create text in a textarea and save that on a object will always encounter encoding problems you did not test. Users will use charaters like é or ö in some kind of sentences. When displaying this message using web_intelligent_plain_text_to_html, to auto convert url’s or newlines you have to make sure you can handel them all.

Here we will have a tal snippet to display contect.getMessage:

<span tal:content="structure python:
          unicode(portal_transforms.convert(
                  'web_intelligent_plain_text_to_html',
                   context.getMessage().decode('utf-8')).getData(),
                   'utf-8')">
  The message
</span>

This way getMessage may return éëê like characters and will still convert all urls to clickable url’s. web_intelligent_plain_text_to_html works pretty nice by default, and will convert all links to email or urls to clickable links and convert newlines to html br tags.

Nothing worse than getting:

UnicodeDecodeError: 'ascii' codec can't decode
byte 0xc3 in position 284: ordinal not in range(128)
We love code