source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/restwidget.py @ 7819

Last change on this file since 7819 was 7819, checked in by Henrik Bettermann, 13 years ago

KOFA -> Kofa

  • Property svn:keywords set to Id
File size: 2.3 KB
RevLine 
[7196]1## $Id: restwidget.py 7819 2012-03-08 22:28:46Z henrik $
[6063]2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
[7196]8##
[6063]9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
[7196]13##
[6063]14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
[7811]18"""A widget that renders restructured text.
[6063]19"""
[7702]20from zope.component import getUtility
[6063]21from zope.formlib.widget import renderElement, DisplayWidget
[7811]22from waeup.kofa.utils.helpers import ReST2HTML
[7819]23from waeup.kofa.interfaces import IKofaUtils
[6063]24
25
[6084]26class ReSTDisplayWidget(DisplayWidget):
[6063]27    """Restructured Text widget.
28    """
29
30    def __call__(self):
[7702]31        """The ReSTDisplayWidget transforms a ReST text string into
32        a dictionary.
33
[7705]34        Different languages must be separated by `>>xy<<` whereas
[7702]35        xy is the language code. Text parts without correct leading
36        language separator - usually the first part has no language
37        descriptor - are interpreted as texts in the portal's language.
[7819]38        The latter can be configured in waeup.srp.utils.utils.KofaUtils.
[7702]39        """
[6063]40        if self._renderedValueSet():
41            value = self._data
42        else:
43            value = self.context.default
44        if value == self.context.missing_value:
[7703]45            return {}
[7702]46        parts = value.split('>>')
47        elements = {}
[7819]48        lang = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7702]49        for part in parts:
50            if part[2:4] == u'<<':
51                lang = part[0:2].lower()
52                text = part[4:]
53                elements[lang] = renderElement(u'div id="rest"',
54                    contents=ReST2HTML(text))
55            else:
56                text = part
57                elements[lang] = renderElement(u'div id="rest"',
58                    contents=ReST2HTML(text))
59        return elements
Note: See TracBrowser for help on using the repository browser.