File: fckeditor.py

package info (click to toggle)
moin 1.5.3-1.2etch2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,692 kB
  • ctags: 26,801
  • sloc: python: 32,907; java: 10,704; perl: 1,424; php: 642; makefile: 172; xml: 162; sh: 121; sed: 5
file content (145 lines) | stat: -rw-r--r-- 3,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
FCKeditor - The text editor for internet
Copyright (C) 2003-2005 Frederico Caldeira Knabben

Licensed under the terms of the GNU Lesser General Public License:
		http://www.opensource.org/licenses/lgpl-license.php

For further information visit:
		http://www.fckeditor.net/

"Support Open Source software. What about a donation today?"

File Name: fckeditor.py
	This is the integration file for Python.

File Authors:
		Andrew Liu (andrew@liuholdings.com)
"""

import cgi
import os
import string

def escape(text, replace=string.replace):
    """Converts the special characters '<', '>', and '&'.

    RFC 1866 specifies that these characters be represented
    in HTML as &lt; &gt; and &amp; respectively. In Python
    1.5 we use the new string.replace() function for speed.
    """
    text = replace(text, '&', '&amp;') # must be done 1st
    text = replace(text, '<', '&lt;')
    text = replace(text, '>', '&gt;')
    text = replace(text, '"', '&quot;')
    text = replace(text, "'", '&#39;')
    return text

# The FCKeditor class
class FCKeditor(object):
	def __init__(self, instanceName):
		self.InstanceName = instanceName
		self.BasePath = '/FCKeditor/'
		self.Width = '100%'
		self.Height = '200'
		self.ToolbarSet = 'Default'
		self.Value = '';

		self.Config = {}

	def Create(self):
		return self.CreateHtml()

	def CreateHtml(self):
		HtmlValue = escape(self.Value)
		Html = "<div>"

		if (self.IsCompatible()):
			File = "fckeditor.html"
			Link = "%seditor/%s?InstanceName=%s" % (
					self.BasePath,
					File,
					self.InstanceName
					)
			if (self.ToolbarSet is not None):
				Link += "&amp;ToolBar=%s" % self.ToolbarSet

			# Render the linked hidden field
			Html += "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\" style=\"display:none\" />" % (
					self.InstanceName,
					self.InstanceName,
					HtmlValue
					)

			# Render the configurations hidden field
			Html += "<input type=\"hidden\" id=\"%s___Config\" value=\"%s\" style=\"display:none\" />" % (
					self.InstanceName,
					self.GetConfigFieldString()
					)

			# Render the editor iframe
			Html += "<iframe id=\"%s\__Frame\" src=\"%s\" width=\"%s\" height=\"%s\" frameborder=\"no\" scrolling=\"no\"></iframe>" % (
					self.InstanceName,
					Link,
					self.Width,
					self.Height
					)
		else:
			if (self.Width.find("%%") < 0):
				WidthCSS = "%spx" % self.Width
			else:
				WidthCSS = self.Width
			if (self.Height.find("%%") < 0):
				HeightCSS = "%spx" % self.Height
			else:
				HeightCSS = self.Height

			Html += "<textarea name=\"%s\" rows=\"4\" cols=\"40\" style=\"width: %s; height: %s;\" wrap=\"virtual\">%s</textarea>" % (
					self.InstanceName,
					WidthCSS,
					HeightCSS,
					HtmlValue
					)
		Html += "</div>"
		return Html
	
	def IsCompatible(self):
		if (os.environ.has_key("HTTP_USER_AGENT")):
			sAgent = os.environ.get("HTTP_USER_AGENT", "")
		else:
			sAgent = ""
		if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
			i = sAgent.find("MSIE")
			iVersion = float(sAgent[i+5:i+5+3])
			if (iVersion >= 5.5):
				return True
			return False
		elif (sAgent.find("Gecko/") >= 0):
			i = sAgent.find("Gecko/")
			iVersion = int(sAgent[i+6:i+6+8])
			if (iVersion >= 20030210):
				return True
			return False
		else:
			return False

	def GetConfigFieldString(self):
		sParams = ""
		bFirst = True
		for sKey in self.Config.keys():
			sValue = self.Config[sKey]
			if (not bFirst):
				sParams += "&amp;"
			else:
				bFirst = False
			if (sValue):
				k = escape(sKey)
				v = escape(sValue)
				if (sValue == "true"):
					sParams += "%s=true" % k
				elif (sValue == "false"):
					sParams += "%s=false" % k
				else:
					sParams += "%s=%s" % (k, v)
		return sParams