1 | """
|
---|
2 | FCKeditor - The text editor for internet
|
---|
3 | Copyright (C) 2003-2006 Frederico Caldeira Knabben
|
---|
4 |
|
---|
5 | Licensed under the terms of the GNU Lesser General Public License:
|
---|
6 | http://www.opensource.org/licenses/lgpl-license.php
|
---|
7 |
|
---|
8 | For further information visit:
|
---|
9 | http://www.fckeditor.net/
|
---|
10 |
|
---|
11 | "Support Open Source software. What about a donation today?"
|
---|
12 |
|
---|
13 | File Name: fckeditor.py
|
---|
14 | This is the integration file for Python.
|
---|
15 |
|
---|
16 | File Authors:
|
---|
17 | Andrew Liu (andrew@liuholdings.com)
|
---|
18 | """
|
---|
19 |
|
---|
20 | import cgi
|
---|
21 | import os
|
---|
22 | import string
|
---|
23 |
|
---|
24 | def escape(text, replace=string.replace):
|
---|
25 | """Converts the special characters '<', '>', and '&'.
|
---|
26 |
|
---|
27 | RFC 1866 specifies that these characters be represented
|
---|
28 | in HTML as < > and & respectively. In Python
|
---|
29 | 1.5 we use the new string.replace() function for speed.
|
---|
30 | """
|
---|
31 | text = replace(text, '&', '&') # must be done 1st
|
---|
32 | text = replace(text, '<', '<')
|
---|
33 | text = replace(text, '>', '>')
|
---|
34 | text = replace(text, '"', '"')
|
---|
35 | text = replace(text, "'", ''')
|
---|
36 | return text
|
---|
37 |
|
---|
38 | # The FCKeditor class
|
---|
39 | class FCKeditor(object):
|
---|
40 | def __init__(self, instanceName):
|
---|
41 | self.InstanceName = instanceName
|
---|
42 | self.BasePath = '/fckeditor/'
|
---|
43 | self.Width = '100%'
|
---|
44 | self.Height = '200'
|
---|
45 | self.ToolbarSet = 'Default'
|
---|
46 | self.Value = '';
|
---|
47 |
|
---|
48 | self.Config = {}
|
---|
49 |
|
---|
50 | def Create(self):
|
---|
51 | return self.CreateHtml()
|
---|
52 |
|
---|
53 | def CreateHtml(self):
|
---|
54 | HtmlValue = escape(self.Value)
|
---|
55 | Html = "<div>"
|
---|
56 |
|
---|
57 | if (self.IsCompatible()):
|
---|
58 | File = "fckeditor.html"
|
---|
59 | Link = "%seditor/%s?InstanceName=%s" % (
|
---|
60 | self.BasePath,
|
---|
61 | File,
|
---|
62 | self.InstanceName
|
---|
63 | )
|
---|
64 | if (self.ToolbarSet is not None):
|
---|
65 | Link += "&ToolBar=%s" % self.ToolbarSet
|
---|
66 |
|
---|
67 | # Render the linked hidden field
|
---|
68 | Html += "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\" style=\"display:none\" />" % (
|
---|
69 | self.InstanceName,
|
---|
70 | self.InstanceName,
|
---|
71 | HtmlValue
|
---|
72 | )
|
---|
73 |
|
---|
74 | # Render the configurations hidden field
|
---|
75 | Html += "<input type=\"hidden\" id=\"%s___Config\" value=\"%s\" style=\"display:none\" />" % (
|
---|
76 | self.InstanceName,
|
---|
77 | self.GetConfigFieldString()
|
---|
78 | )
|
---|
79 |
|
---|
80 | # Render the editor iframe
|
---|
81 | Html += "<iframe id=\"%s\__Frame\" src=\"%s\" width=\"%s\" height=\"%s\" frameborder=\"0\" scrolling=\"no\"></iframe>" % (
|
---|
82 | self.InstanceName,
|
---|
83 | Link,
|
---|
84 | self.Width,
|
---|
85 | self.Height
|
---|
86 | )
|
---|
87 | else:
|
---|
88 | if (self.Width.find("%%") < 0):
|
---|
89 | WidthCSS = "%spx" % self.Width
|
---|
90 | else:
|
---|
91 | WidthCSS = self.Width
|
---|
92 | if (self.Height.find("%%") < 0):
|
---|
93 | HeightCSS = "%spx" % self.Height
|
---|
94 | else:
|
---|
95 | HeightCSS = self.Height
|
---|
96 |
|
---|
97 | Html += "<textarea name=\"%s\" rows=\"4\" cols=\"40\" style=\"width: %s; height: %s;\" wrap=\"virtual\">%s</textarea>" % (
|
---|
98 | self.InstanceName,
|
---|
99 | WidthCSS,
|
---|
100 | HeightCSS,
|
---|
101 | HtmlValue
|
---|
102 | )
|
---|
103 | Html += "</div>"
|
---|
104 | return Html
|
---|
105 |
|
---|
106 | def IsCompatible(self):
|
---|
107 | if (os.environ.has_key("HTTP_USER_AGENT")):
|
---|
108 | sAgent = os.environ.get("HTTP_USER_AGENT", "")
|
---|
109 | else:
|
---|
110 | sAgent = ""
|
---|
111 | if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
|
---|
112 | i = sAgent.find("MSIE")
|
---|
113 | iVersion = float(sAgent[i+5:i+5+3])
|
---|
114 | if (iVersion >= 5.5):
|
---|
115 | return True
|
---|
116 | return False
|
---|
117 | elif (sAgent.find("Gecko/") >= 0):
|
---|
118 | i = sAgent.find("Gecko/")
|
---|
119 | iVersion = int(sAgent[i+6:i+6+8])
|
---|
120 | if (iVersion >= 20030210):
|
---|
121 | return True
|
---|
122 | return False
|
---|
123 | else:
|
---|
124 | return False
|
---|
125 |
|
---|
126 | def GetConfigFieldString(self):
|
---|
127 | sParams = ""
|
---|
128 | bFirst = True
|
---|
129 | for sKey in self.Config.keys():
|
---|
130 | sValue = self.Config[sKey]
|
---|
131 | if (not bFirst):
|
---|
132 | sParams += "&"
|
---|
133 | else:
|
---|
134 | bFirst = False
|
---|
135 | if (sValue):
|
---|
136 | k = escape(sKey)
|
---|
137 | v = escape(sValue)
|
---|
138 | if (sValue == "true"):
|
---|
139 | sParams += "%s=true" % k
|
---|
140 | elif (sValue == "false"):
|
---|
141 | sParams += "%s=false" % k
|
---|
142 | else:
|
---|
143 | sParams += "%s=%s" % (k, v)
|
---|
144 | return sParams
|
---|
145 |
|
---|