source: trunk/WYSIWYMWidget.py @ 50

Revision 50, 3.8 KB checked in by laszlo, 14 years ago (diff)

closes #89 spent 90m

  • Property svn:eol-style set to LF
Line 
1from AccessControl import ClassSecurityInfo
2from Products.Archetypes.Registry import registerWidget
3from Products.Archetypes.Widget import TypesWidget
4
5import re
6
7class WYSIWYMTranslator:
8   
9    def getBody(self):
10        body="\n\n%s\n\n" % str(self.body)
11       
12        lf1 = re.compile('\r\n')
13        lf2 = re.compile('\r')
14        body = lf1.sub('\n',body)
15        body = lf2.sub('\n',body)
16
17        body = re.sub(r'\n\n\*\s', r'\n\n<ul><li>', body)
18        body = re.sub(r'\n\*\s(.*)\n\n', r'</li><li>\1</li></ul>\n\n', body)
19        body = re.sub(r'\n\*\s', r'</li><li>', body)
20
21        body = re.sub(r'\n\n\#\s', r'\n\n<ol><li>', body)
22        body = re.sub(r'\n\#\s(.*)\n\n', r'</li><li>\1</li></ol>\n\n', body)
23        body = re.sub(r'\n\#\s', r'</li><li>', body)
24
25        body = re.sub('\n\n<ul>',"</p><ul>",body)
26        body = re.sub('</ul>\n\n',"</ul><p>",body)
27        body = re.sub('\n\n<ol>',"</p><ol>",body)
28        body = re.sub('</ol>\n\n',"</ol><p>",body)
29        body = re.sub('\n\n',"</p><p>",body)
30        body = re.sub('\n',"<br/>",body)
31   
32        body = re.sub(r'\A</p>', '', body)
33        body = re.sub(r'<p>\Z', '', body)
34
35        body = re.sub(r'\[(\S+?)\s(.*?)\]', r'<a href="\1">\2</a>', body)
36       
37        #raise 'FOO', str(body)
38        body=re.sub(r'([\s>])\*(\S.*?\S)\*([\s<])', r'\1<b>\2</b>\3',body)
39        body=re.sub(r'([\s>])_(\S.*?\S)_([\s<])', r'\1<i>\2</i>\3',body)
40        body = re.sub(r'\A\s(.*)\s\Z',r'<p>\1</p>',body)
41        return body
42
43class WYSIWYMWidget(TypesWidget):
44    _properties = TypesWidget._properties.copy()
45    _properties.update({
46        # Macro is located by trying all registered skin folders to find a template with this path
47        'macro' : "wysiwym",
48        'rows'  : 5,
49        'cols'  : 40,
50        'format': 1,
51        'allow_file_upload': False,
52        })
53
54    security = ClassSecurityInfo()
55
56    # XXX
57    security.declarePublic('process_form')
58    def process_form(self, instance, field, form, empty_marker=None,
59                     emptyReturnsMarker=False):
60        """complex form processing, includes handling for text
61        formatting and file objects"""
62        # TODO: This is not right
63        # This is basically the old processing chain from base object
64        text_format = None
65        isFile = False
66        value = None
67
68        # text field with formatting
69        if hasattr(field, 'allowable_content_types') and \
70           field.allowable_content_types:
71            # was a mimetype specified
72            format_field = "%s_text_format" % field.getName()
73            text_format = form.get(format_field, empty_marker)
74
75        # or a file?
76        fileobj = form.get('%s_file' % field.getName(), empty_marker)
77
78        if fileobj is not empty_marker:
79
80            filename = getattr(fileobj, 'filename', '') or \
81                       (isinstance(fileobj, FileType) and \
82                        getattr(fileobj, 'name', ''))
83
84            if filename:
85                value = fileobj
86                isFile = True
87
88        kwargs = {}
89        if not value:
90            value = form.get(field.getName(), empty_marker)
91            if text_format is not empty_marker and text_format:
92                kwargs['mimetype'] = text_format
93
94        if value is empty_marker: return empty_marker
95
96        if value and not isFile:
97            # Value filled, no file uploaded
98            if kwargs.get('mimetype') == str(field.getContentType(instance)) \
99                   and instance.isBinary(field.getName()):
100                # Was an uploaded file, same content type
101                del kwargs['mimetype']
102
103        return value, kwargs
104
105registerWidget(WYSIWYMWidget,
106               title='WYSIWYM Widget',
107               description=('WYSIWYM Widget'),
108               used_for=('Products.Archetypes.Field.TextField',)
109               )
Note: See TracBrowser for help on using the repository browser.