HTML和XHTML编辑支持
zope.html的Python项目详细描述
详细文档
HTML文件编辑支持
这个包支持在web中编辑html和xhtml 使用fckeditor作为小部件的页面。这相当简单 应用fckeditor,并简单地实例化一个预先配置的 每个小部件的编辑器。没有控制编辑器的选项 个别地。
在创建这个时,我们遇到了编辑器的一些限制 值得注意。注意到这些限制并不意味着 其他的编辑做得更好;可用的似乎是一个混合包。
- 编辑器只处理可以包含在"正文"中的内容。 元素;任何超出此范围的内容,包括<;body>;和 <;/body>;标记,丢失或损坏。如果有任何配置方法 fckeditor处理此类材料时,没有记录。
- 没有对html源代码的真正控制;空白不是 像程序员所期望的那样保存。很多人都能接受 用例,但不是全部。应用程序应避免使用此小部件 如果必须保留原始空白。
实现问题
这些是用于集成fckeditor的小部件的问题 而不是fckeditor本身的问题。应该处理这些问题。
- 编辑器的宽度是硬编码的;这应该是 可配置或编辑器应拉伸以填充可用的 空间。fckeditor的示例用法似乎没有显示这一点 问题,所以它可以比现在更好。
- 编辑器的高度应该以类似于 基本文本区域小部件的配置。
未来发展思路
这些想法可能很有趣,但没有具体的 此时计划这样做:
对编辑器的应用程序进行分类并提供备用 这些应用程序的工具栏配置。有很多 编辑器本身的可配置性,因此可以 不同的东西。
添加对其他一些奇特的客户端HTML编辑器的支持, 并允许用户首选项选择用于 应用程序,包括在 需要对html进行详细控制(或者对luddite用户 不喜欢gui编辑器)。
xinha(http://xinha.python-hosting.com/" rel="nofollow">http://xinha.python hosting.com/)似乎是 还有一个有趣的选择,可能对应用程序更有用 需要的不仅仅是编辑小的html片段,特别是如果 用户相当精通HTML。
htmlarea(http://www.dynarch.com/projects/htmlarea/" rel="nofollow">http://www.dynarch.com/projects/htmlarea/)可能变成 有些时候很有趣,但现在只是粗略的阅读 表明新哈可能是一条更合理的路线。
补充信息的管理
zope.html 包提供了有关包含 允许通过Web编辑文件的HTML和XHTML数据。这个 文件可能包含完整的文档,也可能包含 组成更大的文件。还提供预览视图。
编辑和预览视图依赖于获取补充信息 关于使用 ieditablehtmlinformation编辑的文件 文件的适配器。适配器在内容上使用注释 对象来存储需要持久化的信息。
ieditablehtmlinformation接口非常简单;只有 定义了一个字段,它是一个简单的布尔值:文件 是否应视为碎片。让我们创建一个简单的内容 可用于测试的对象:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()
让我们创建一个文件和相应的 ieditablehtmlinformation 对象:
>>> import zope.html.docinfo >>> file = File("This is a <em>fragment</em>.") >>> info = zope.html.docinfo.EditableHtmlInformation(file)
我们现在可以检查 属性的初始值是否为fragment 计算合理:
>>> info.isFragment True
用户可以从ui中切换 isfragment 标志,因此 它应该记住标志的当前状态:
>>> info.isFragment = False >>> info.isFragment False
ieditablehtmlinformation实例的新实例还应记住设置的最后一个值:
>>> zope.html.docinfo.EditableHtmlInformation(file).isFragment False
(x)HTML片段编辑器小部件
这个包中包含的小部件是 fckeditor控件。它只适用于碎片,不适用于 任意文件。让我们创建一个字段和一个小部件:
>>> from zope.html import field >>> from zope.html import widget >>> from zope.publisher import browser >>> class Context(object): ... sample = u"" >>> myfield = field.XhtmlFragment( ... __name__="sample", ... title=u"Sample Field", ... ).bind(Context()) >>> request = browser.TestRequest() >>> mywidget = widget.FckeditorWidget(myfield, request) >>> mywidget.setPrefix("form") >>> mywidget.configurationPath = "/myconfig.js" >>> mywidget.editorWidth = 360 >>> mywidget.editorHeight = 200 >>> mywidget.toolbarConfiguration = "mytoolbars" >>> print mywidget() <textarea...></textarea> <script... "form.sample", 360, 200, "mytoolbars"); ...Config["CustomConfigurationsPath"] = "/myconfig.js"; ... </script> <BLANKLINE>
我们还应该测试ckeditorwidget。
< Buff行情>>>> ckwidget = widget.CkeditorWidget(myfield, request) >>> ckwidget.configurationPath = "/myconfig.js" >>> ckwidget.editorHeight = 200
"fckversion"属性保存ckeditor库的版本。
< Buff行情>>>> ckwidget.fckVersion '3.6.2'
>>> print ckwidget() <textarea...></textarea> <script... ...height: 200... ...customConfig : "/myconfig.js"... </script> <BLANKLINE>
可编辑HTML上的视图
我们先上传一些html来创建一个文件对象:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()0
我们可以看到,mime处理程序已将此标记为html内容:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()1
"edit"视图可用于检查和修改"is fragment"?" 字段,由视图存储在对象的批注中。 我们上传到这里的片段应该被看作是一个片段 默认情况下:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()2
通过取消选中复选框并单击 "保存":
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()3
如果文档 可以解码。如果文档的编码未知,则 提示用户选择 应该使用的编码。
我们的示例文档没有指定的编码,因此我们希望 表示需要编码的表单,并允许用户 选择并编码。让我们重新加载表单以除去 "已更新…"消息,以便我们可以看到用户被告知的内容:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()4
然后,用户可以选择编码:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()5
因为我们刚刚选择了一种与拉丁语-1不兼容的编码 我们为文件上传的数据,我们被告知编码不是 可接受:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()6
我们需要选择一种对数据真正有意义的编码 我们已经上传了:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()7
既然已经保存了编码,就可以对文档进行编码并 已编辑,编码选择将不再在 表格:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()8
由于我们选择的编码不支持所有Unicode字符, 有一个选项允许重新编码文档,如果 编辑后保存的内容不能在 文档的原始编码。此选项的默认值 为false,因为用户需要知道文档编码 可以修改:
>>> import zope.file.file >>> import zope.interface >>> import zope.annotation >>> class File(zope.file.file.File): ... zope.interface.implements( ... zope.annotation.IAttributeAnnotatable) ... ... def __init__(self, text=None): ... super(File, self).__init__("text/html", {"charset": "utf-8"}) ... f = self.open("w") ... f.write(text) ... f.close()9
如果我们编辑文本时包含的字符不能 以当前编码进行编码,并尝试在没有 允许重新编码,我们看到一个通知,该文档不能 以原始编码进行编码,需要重新编码:
>>> import zope.html.docinfo >>> file = File("This is a <em>fragment</em>.") >>> info = zope.html.docinfo.EditableHtmlInformation(file)0
此时,我们可以选择"重新编码"选项来允许文本 以非原始编码保存;这将允许 我们将保存任何文本:
>>> import zope.html.docinfo >>> file = File("This is a <em>fragment</em>.") >>> info = zope.html.docinfo.EditableHtmlInformation(file)1
如果我们现在查看文件的"content type"视图,就会看到 编码已更新为utf-8:
>>> import zope.html.docinfo >>> file = File("This is a <em>fragment</em>.") >>> info = zope.html.docinfo.EditableHtmlInformation(file)2
更改
2.4.2(2014-04-17)
- 删除不需要的zope.app.authentication/debugskin/server测试依赖项。
- 支持在zope.testbrowser 4.0.3中更改的测试输出
2.4.1(2012-01-26)
- 修复ckeditor资源的路径。
2.4.0(2012-01-26)
- 使用CKeditor 3.6.2
- 使用python的 doctest 模块而不是弃用 zope.testing.doctest
2.3.0(2011-02-22)
- 使用CKeditor 3.5.2
2.2.0(2010-11-19)
- 明确使用未缩小的ckeditor源代码
2.1.0(2010-05-25)
- 使用CKeditor 3.2.1
- 添加配置以在使用dev时使用未精简版本的ckeditor 模式:
- 修复了导致测试失败的导入。
2.0.0(2009-09-04)
- 添加ckeditor 3.0小部件。
1.2.0(2009-07-06)
- 使用fckeditor 2.6.4.1
- 删除"采样目录"并为其复活设置一个屏障
1.1.0(2008-06-18)
- 使用fckeditor 2.6
- 使用javascript的版本化目录来缓存bust
1.0.1(2007-11-02)
- 包数据更新。
- 已更新代码,可用于Zope 3.4版本中的软件包。
1.0.0(2007-10-29)
- 初始版本。