fckeditor.py 4.1 KB

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