source: main/waeup.kofa/backup-trunk/src/waeup/kofa/widgets/restwidget.py @ 9356

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

KOFA -> Kofa

  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1## $Id: restwidget.py 7819 2012-03-08 22:28:46Z henrik $
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.
8##
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.
13##
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##
18"""A widget that renders restructured text.
19"""
20from zope.component import getUtility
21from zope.formlib.widget import renderElement, DisplayWidget
22from waeup.kofa.utils.helpers import ReST2HTML
23from waeup.kofa.interfaces import IKofaUtils
24
25
26class ReSTDisplayWidget(DisplayWidget):
27    """Restructured Text widget.
28    """
29
30    def __call__(self):
31        """The ReSTDisplayWidget transforms a ReST text string into
32        a dictionary.
33
34        Different languages must be separated by `>>xy<<` whereas
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.
38        The latter can be configured in waeup.srp.utils.utils.KofaUtils.
39        """
40        if self._renderedValueSet():
41            value = self._data
42        else:
43            value = self.context.default
44        if value == self.context.missing_value:
45            return {}
46        parts = value.split('>>')
47        elements = {}
48        lang = getUtility(IKofaUtils).PORTAL_LANGUAGE
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.