source: trunk/LeMillTool.py @ 581

Revision 581, 5.3 KB checked in by vahur, 13 years ago (diff)

re #613 spent 1h
tuning

Line 
1# Copyright 2006 by the LeMill Team (see AUTHORS)
2#
3# This file is part of LeMill.
4#
5# LeMill is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# LeMill is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with LeMill; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19from OFS.SimpleItem import SimpleItem
20from OFS.PropertyManager import PropertyManager
21from Products.CMFCore.utils import UniqueObject
22from Globals import InitializeClass
23from AccessControl import ClassSecurityInfo
24from Products.CMFCore.utils import getToolByName
25from config import SEARCHABLE_TYPES
26import re
27
28
29class LeMillTool(PropertyManager, UniqueObject, SimpleItem):
30    """ Tool for miscallenous methods """
31
32    id = 'lemill_tool'
33    meta_type = 'LeMillTool'
34    security = ClassSecurityInfo()
35    plone_tool = 1
36    toolicon = 'skins/lemill/tool.gif'
37    __allow_access_to_unprotected_subobjects__ = 1
38
39    security.declarePublic('createUniqueGroupId')
40    def createUniqueGroupId(self, basename):
41        """ Groups should always use titles for display, but urls use ids so lets make ids understandable & unique.  """
42        # basename should be user's id or material's id, we add _group+numbers after that.
43        basename=str(basename)
44        grouptool=getToolByName(self, 'portal_groups')
45        folder=grouptool.getGroupWorkspacesFolder()
46        name=basename+'_group'
47        number=1
48        while name in folder.objectIds():
49            name=basename+'_group'+str(number)
50            number=number+1
51        return name
52   
53    security.declarePublic('render')
54    def render(self,body):
55        body="\n\n%s\n\n" % str(body)
56       
57        lf1 = re.compile('\r\n')
58        lf2 = re.compile('\r')
59        body = lf1.sub('\n',body)
60        body = lf2.sub('\n',body)
61
62        body = re.sub(r'\n\n\*\s', r'\n\n<ul><li>', body)
63        body = re.sub(r'\n\*\s(.*)\n\n', r'</li><li>\1</li></ul>\n\n', body)
64        body = re.sub(r'\n\*\s', r'</li><li>', body)
65
66        body = re.sub(r'\n\n\#\s', r'\n\n<ol><li>', body)
67        body = re.sub(r'\n\#\s(.*)\n\n', r'</li><li>\1</li></ol>\n\n', body)
68        body = re.sub(r'\n\#\s', r'</li><li>', body)
69
70        body = re.sub('\n\n<ul>',"</p><ul>",body)
71        body = re.sub('</ul>\n\n',"</ul><p>",body)
72        body = re.sub('\n\n<ol>',"</p><ol>",body)
73        body = re.sub('</ol>\n\n',"</ol><p>",body)
74        body = re.sub('\n\n',"</p><p>",body)
75        body = re.sub('\n',"<br/>",body)
76   
77        body = re.sub(r'\A</p>', '', body)
78        body = re.sub(r'<p>\Z', '', body)
79
80        body = re.sub(r'\[(\S+?)\s(.*?)\]', r'<a href="\1">\2</a>', body)
81       
82        #raise 'FOO', str(body)
83        body=re.sub(r'([\W>])\*(\S.*?\S)\*([\W<])', r'\1<b>\2</b>\3',body)
84        body=re.sub(r'([\W>])_(\S.*?\S)_([\W<])', r'\1<i>\2</i>\3',body)
85        body = re.sub(r'\A\s(.*)\s\Z',r'<p>\1</p>',body)
86        return body
87
88    security.declarePublic('listrender')
89    def listrender(self, body):
90        # like render, but without all of the stuff related to new lines
91        body = re.sub(r'\[(\S+?)\s(.*?)\]', r'<a href="\1">\2</a>', body)       
92        body=re.sub(r'([\W>])\*(\S.*?\S)\*([\W<])', r'\1<b>\2</b>\3',body)
93        body=re.sub(r'([\W>])_(\S.*?\S)_([\W<])', r'\1<i>\2</i>\3',body)
94        body = re.sub(r'\A\s(.*)\s\Z',r'<p>\1</p>',body)
95        body = re.sub(r'<?p>','', body)
96        return body
97
98    security.declarePublic('searchable_types')
99    def searchable_types(self):
100        return list(SEARCHABLE_TYPES)
101
102    def resize_image(self, image, to_width=160, to_height=120):
103        """ resize image to given width and height. StringIO, im will be returned"""
104        try:
105            from PIL import Image
106        except ImportError:
107            return None, image
108        import cStringIO
109        val = None
110        # attempt to get pure binary data
111        try:
112            val = image.read()
113        except AttributeError: # no read method
114            val = image
115        if hasattr(image, 'data'):
116            val = image.data
117            if not isinstance(val, type('')):
118                val = val.aq_base
119                from OFS.Image import Pdata
120                if isinstance(val, Pdata):
121                    #print "this is Pdata"
122                    val = str(val)
123        s = cStringIO.StringIO(val)
124        im = Image.open(s)
125        (width, height) = im.size
126        if width > to_width:
127            mod = float(to_width)/float(width)
128            width = width*mod
129            height = height*mod
130            im = im.resize((int(width),int(height)))
131        if height > to_height:
132            u_w = float(to_height)/float(heigth)
133            width = width*u_w
134            height = height*u_w
135            im = im.resize((int(width),int(height)))
136        s = cStringIO.StringIO()
137        if im.mode == 'P':
138            im.save(s, "PNG")
139        else:
140            im.save(s, "JPEG", quality=100)
141        s.seek(0)
142        return s, im
143
144
145InitializeClass(LeMillTool)
Note: See TracBrowser for help on using the repository browser.