Back to index

moin  1.9.0~rc2
fckeditor.py
Go to the documentation of this file.
00001 """
00002 FCKeditor - The text editor for Internet - http://www.fckeditor.net
00003 Copyright (C) 2003-2009 Frederico Caldeira Knabben
00004 
00005 == BEGIN LICENSE ==
00006 
00007 Licensed under the terms of any of the following licenses at your
00008 choice:
00009 
00010  - GNU General Public License Version 2 or later (the "GPL")
00011    http://www.gnu.org/licenses/gpl.html
00012 
00013  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00014    http://www.gnu.org/licenses/lgpl.html
00015 
00016  - Mozilla Public License Version 1.1 or later (the "MPL")
00017    http://www.mozilla.org/MPL/MPL-1.1.html
00018 
00019 == END LICENSE ==
00020 
00021 This is the integration file for Python.
00022 """
00023 
00024 import cgi
00025 import os
00026 import re
00027 import string
00028 
00029 def escape(text, replace=string.replace):
00030     """Converts the special characters '<', '>', and '&'.
00031 
00032     RFC 1866 specifies that these characters be represented
00033     in HTML as &lt; &gt; and &amp; respectively. In Python
00034     1.5 we use the new string.replace() function for speed.
00035     """
00036     text = replace(text, '&', '&amp;') # must be done 1st
00037     text = replace(text, '<', '&lt;')
00038     text = replace(text, '>', '&gt;')
00039     text = replace(text, '"', '&quot;')
00040     text = replace(text, "'", '&#39;')
00041     return text
00042 
00043 # The FCKeditor class
00044 class FCKeditor(object):
00045        def __init__(self, instanceName):
00046               self.InstanceName = instanceName
00047               self.BasePath = '/fckeditor/'
00048               self.Width = '100%'
00049               self.Height = '200'
00050               self.ToolbarSet = 'Default'
00051               self.Value = '';
00052 
00053               self.Config = {}
00054 
00055        def Create(self):
00056               return self.CreateHtml()
00057 
00058        def CreateHtml(self):
00059               HtmlValue = escape(self.Value)
00060               Html = ""
00061 
00062               if (self.IsCompatible()):
00063                      File = "fckeditor.html"
00064                      Link = "%seditor/%s?InstanceName=%s" % (
00065                                    self.BasePath,
00066                                    File,
00067                                    self.InstanceName
00068                                    )
00069                      if (self.ToolbarSet is not None):
00070                             Link += "&amp;Toolbar=%s" % self.ToolbarSet
00071 
00072                      # Render the linked hidden field
00073                      Html += "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\" style=\"display:none\" />" % (
00074                                    self.InstanceName,
00075                                    self.InstanceName,
00076                                    HtmlValue
00077                                    )
00078 
00079                      # Render the configurations hidden field
00080                      Html += "<input type=\"hidden\" id=\"%s___Config\" value=\"%s\" style=\"display:none\" />" % (
00081                                    self.InstanceName,
00082                                    self.GetConfigFieldString()
00083                                    )
00084 
00085                      # Render the editor iframe
00086                      Html += "<iframe id=\"%s\__Frame\" src=\"%s\" width=\"%s\" height=\"%s\" frameborder=\"0\" scrolling=\"no\"></iframe>" % (
00087                                    self.InstanceName,
00088                                    Link,
00089                                    self.Width,
00090                                    self.Height
00091                                    )
00092               else:
00093                      if (self.Width.find("%%") < 0):
00094                             WidthCSS = "%spx" % self.Width
00095                      else:
00096                             WidthCSS = self.Width
00097                      if (self.Height.find("%%") < 0):
00098                             HeightCSS = "%spx" % self.Height
00099                      else:
00100                             HeightCSS = self.Height
00101 
00102                      Html += "<textarea name=\"%s\" rows=\"4\" cols=\"40\" style=\"width: %s; height: %s;\" wrap=\"virtual\">%s</textarea>" % (
00103                                    self.InstanceName,
00104                                    WidthCSS,
00105                                    HeightCSS,
00106                                    HtmlValue
00107                                    )
00108               return Html
00109 
00110        def IsCompatible(self):
00111               if (os.environ.has_key("HTTP_USER_AGENT")):
00112                      sAgent = os.environ.get("HTTP_USER_AGENT", "")
00113               else:
00114                      sAgent = ""
00115               if (sAgent.find("MSIE") >= 0) and (sAgent.find("mac") < 0) and (sAgent.find("Opera") < 0):
00116                      i = sAgent.find("MSIE")
00117                      iVersion = float(sAgent[i+5:i+5+3])
00118                      if (iVersion >= 5.5):
00119                             return True
00120                      return False
00121               elif (sAgent.find("Gecko/") >= 0):
00122                      i = sAgent.find("Gecko/")
00123                      iVersion = int(sAgent[i+6:i+6+8])
00124                      if (iVersion >= 20030210):
00125                             return True
00126                      return False
00127               elif (sAgent.find("Opera/") >= 0):
00128                      i = sAgent.find("Opera/")
00129                      iVersion = float(sAgent[i+6:i+6+4])
00130                      if (iVersion >= 9.5):
00131                             return True
00132                      return False
00133               elif (sAgent.find("AppleWebKit/") >= 0):
00134                      p = re.compile('AppleWebKit\/(\d+)', re.IGNORECASE)
00135                      m = p.search(sAgent)
00136                      if (m.group(1) >= 522):
00137                             return True
00138                      return False
00139               else:
00140                      return False
00141 
00142        def GetConfigFieldString(self):
00143               sParams = ""
00144               bFirst = True
00145               for sKey in self.Config.keys():
00146                      sValue = self.Config[sKey]
00147                      if (not bFirst):
00148                             sParams += "&amp;"
00149                      else:
00150                             bFirst = False
00151                      if (sValue):
00152                             k = escape(sKey)
00153                             v = escape(sValue)
00154                             if (sValue == "true"):
00155                                    sParams += "%s=true" % k
00156                             elif (sValue == "false"):
00157                                    sParams += "%s=false" % k
00158                             else:
00159                                    sParams += "%s=%s" % (k, v)
00160               return sParams