-- 211.150.223.154 [2004-09-13 00:11:26]

1. PyTextile

PyTextile是一个Textile的Python实现模块

1.1. PyTextile介绍

Python 下的包最初是由Mark Pilgrim完成的,地址在:Mark pytextile

上面有下载链接。不过现在已经由他人来继续维护了,pytextile。现在最新的版本为2.0.8。

有人在Mark的Blog评论中问:reStructuredText与Textile有什么不同呢?

Mark的回答是:

  • reStructuredText 是输出格式不确定(注:我理解是可以输出多种格式,如HTML,Tex,XML,PDF等),而Textile是为web显示专门设计的。
  • reStructuredText有许多的选项是Textile缺乏的,如字段列表,定义列表,嵌套块引用等。
  • Textile有几种reStructuredText没有的选项,如CODE, CITE, SUP之类的行内速记。可以将某些高位的ASCII码字符映射为HTML的数字实体。
  • reStructured是为正式文档的特殊样式而设计的,它有脚注、参考书目引用、链接系统。而Textile是为象Blog和评论这种短的,不正式的文档而设计的。

Mark原文如下:

  • reStructuredText is designed to be output-format-agnostic; Textile is specifically designed for web display.
  • reStructuredText has many options that Textile lacks, such as field lists, definition lists, nested blockquotes, simple tables, and grid tables.
  • Textile has several options that reStructuredText lacks, such as inline shorthand for CODE, CITE, and SUP. Also, its ability to auto-map high-bit ASCII characters to HTML numeric entities (making it possible to cut-and-paste smart quotes without breaking validation).
  • reStructuredText is designed for a particular style of formal document: it has a system of cataloging footnotes, bibliographic citations, and hyperlinks. Textile is designed for shorter, more informal documents like blog posts and comments.

我并未逐字翻译,还有一些自已的理解请见谅。

1.2. PyTextile 安装及使用

安装很简单,与一般的模块安装方法一样。

python setup.py install

使用示例:

   1 import textile
   2 a = textile.Textiler(Text) #Text为要处理的文本
   3 print a.process()

Textile可以自已将文档输出,将上面的Text设为:"tell me about textile."。然后执行后就是文档的Html文本了。拷贝下来,保存为html(或改下上面的示例,直接存为文件),在浏览器中就可以看了。

我就是这样用的,更复杂的还没有用到。不过,为了支持 NewEdit 使用我还是改了一下,派生了自已的MyTextiler派,以更很好地使用Unicode。

代码片段如下:

   1     class MyTextiler(textile.Textiler):
   2         def process(self, head_offset=textile.HEAD_OFFSET):
   3             self.head_offset = head_offset
   4 
   5             # Process each block.
   6             self.blocks = self.split_text()
   7 
   8             text = []
   9             for [function, captures] in self.blocks:
  10                 text.append(function(**captures))
  11 
  12             text = '\n\n'.join(text)
  13 
  14             # Add titles to footnotes.
  15             text = self.footnotes(text)
  16 
  17 #           # Convert to desired output.
  18 #           if encoding != 'unicode':
  19 #               text = unicode(text, encoding)
  20 #           if output != 'unicode':
  21 #               text = text.encode(output, 'xmlcharrefreplace')
  22 
  23             return text

上面对unicode的判断注释掉了是因为NewEdit中全部是Unicode,这样不再需要进行编码转换了,如果要保存文件的话,是要进行编辑转换的。那么应该把上面的注释去掉。并不存在'unicode'编码,这里只是为了指明是否是unicode编码而使用。

1.3. PyTextile 文档及翻译

现在有英文的文档,是用Textile生成的,翻译一点点来,有兴趣地也贡献啊。

* PyTextileDoc

1.4. 下载

为了方便大家使用,先在这里放一个最新的版本。textile-2.0.8.tar.gz

1.5. 讨论