source: sandbox/filemanager/tp/fckeditor/fckeditor.py @ 1575

Revision 1575, 4.3 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

  • Property svn:executable set to *
Line 
1"""
2FCKeditor - The text editor for Internet - http://www.fckeditor.net
3Copyright (C) 2003-2009 Frederico Caldeira Knabben
4
5== BEGIN LICENSE ==
6
7Licensed under the terms of any of the following licenses at your
8choice:
9
10 - GNU General Public License Version 2 or later (the "GPL")
11   http://www.gnu.org/licenses/gpl.html
12
13 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   http://www.gnu.org/licenses/lgpl.html
15
16 - Mozilla Public License Version 1.1 or later (the "MPL")
17   http://www.mozilla.org/MPL/MPL-1.1.html
18
19== END LICENSE ==
20
21This is the integration file for Python.
22"""
23
24import cgi
25import os
26import re
27import string
28
29def escape(text, replace=string.replace):
30    """Converts the special characters '<', '>', and '&'.
31
32    RFC 1866 specifies that these characters be represented
33    in HTML as &lt; &gt; and &amp; respectively. In Python
34    1.5 we use the new string.replace() function for speed.
35    """
36    text = replace(text, '&', '&amp;') # must be done 1st
37    text = replace(text, '<', '&lt;')
38    text = replace(text, '>', '&gt;')
39    text = replace(text, '"', '&quot;')
40    text = replace(text, "'", '&#39;')
41    return text
42
43# The FCKeditor class
44class FCKeditor(object):
45        def __init__(self, instanceName):
46                self.InstanceName = instanceName
47                self.BasePath = '/fckeditor/'
48                self.Width = '100%'
49                self.Height = '200'
50                self.ToolbarSet = 'Default'
51                self.Value = '';
52
53                self.Config = {}
54
55        def Create(self):
56                return self.CreateHtml()
57
58        def CreateHtml(self):
59                HtmlValue = escape(self.Value)
60                Html = ""
61
62                if (self.IsCompatible()):
63                        File = "fckeditor.html"
64                        Link = "%seditor/%s?InstanceName=%s" % (
65                                        self.BasePath,
66                                        File,
67                                        self.InstanceName
68                                        )
69                        if (self.ToolbarSet is not None):
70                                Link += "&amp;Toolbar=%s" % self.ToolbarSet
71
72                        # Render the linked hidden field
73                        Html += "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\" style=\"display:none\" />" % (
74                                        self.InstanceName,
75                                        self.InstanceName,
76                                        HtmlValue
77                                        )
78
79                        # Render the configurations hidden field
80                        Html += "<input type=\"hidden\" id=\"%s___Config\" value=\"%s\" style=\"display:none\" />" % (
81                                        self.InstanceName,
82                                        self.GetConfigFieldString()
83                                        )
84
85                        # Render the editor iframe
86                        Html += "<iframe id=\"%s\__Frame\" src=\"%s\" width=\"%s\" height=\"%s\" frameborder=\"0\" scrolling=\"no\"></iframe>" % (
87                                        self.InstanceName,
88                                        Link,
89                                        self.Width,
90                                        self.Height
91                                        )
92                else:
93                        if (self.Width.find("%%") < 0):
94                                WidthCSS = "%spx" % self.Width
95                        else:
96                                WidthCSS = self.Width
97                        if (self.Height.find("%%") < 0):
98                                HeightCSS = "%spx" % self.Height
99                        else:
100                                HeightCSS = self.Height
101
102                        Html += "<textarea name=\"%s\" rows=\"4\" cols=\"40\" style=\"width: %s; height: %s;\" wrap=\"virtual\">%s</textarea>" % (
103                                        self.InstanceName,
104                                        WidthCSS,
105                                        HeightCSS,
106                                        HtmlValue
107                                        )
108                return Html
109
110        def IsCompatible(self):
111                if (os.environ.has_key("HTTP_USER_AGENT")):
112                        sAgent = os.environ.get("HTTP_USER_AGENT", "")
113                else:
114                        sAgent = ""
115                if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
116                        i = sAgent.find("MSIE")
117                        iVersion = float(sAgent[i+5:i+5+3])
118                        if (iVersion >= 5.5):
119                                return True
120                        return False
121                elif (sAgent.find("Gecko/") >= 0):
122                        i = sAgent.find("Gecko/")
123                        iVersion = int(sAgent[i+6:i+6+8])
124                        if (iVersion >= 20030210):
125                                return True
126                        return False
127                elif (sAgent.find("Opera/") >= 0):
128                        i = sAgent.find("Opera/")
129                        iVersion = float(sAgent[i+6:i+6+4])
130                        if (iVersion >= 9.5):
131                                return True
132                        return False
133                elif (sAgent.find("AppleWebKit/") >= 0):
134                        p = re.compile('AppleWebKit\/(\d+)', re.IGNORECASE)
135                        m = p.search(sAgent)
136                        if (m.group(1) >= 522):
137                                return True
138                        return False
139                else:
140                        return False
141
142        def GetConfigFieldString(self):
143                sParams = ""
144                bFirst = True
145                for sKey in self.Config.keys():
146                        sValue = self.Config[sKey]
147                        if (not bFirst):
148                                sParams += "&amp;"
149                        else:
150                                bFirst = False
151                        if (sValue):
152                                k = escape(sKey)
153                                v = escape(sValue)
154                                if (sValue == "true"):
155                                        sParams += "%s=true" % k
156                                elif (sValue == "false"):
157                                        sParams += "%s=false" % k
158                                else:
159                                        sParams += "%s=%s" % (k, v)
160                return sParams
Note: See TracBrowser for help on using the repository browser.