-- limodou [2004-09-13 16:55:32]

1. 将reStructuredText转成Html

这是从Python cookbook中看到的

1.1. 介绍

reStructuredText是一种格式化文本,已经广泛应用在Python的文档中了。reStructuredText可以转换成多种格式,不只是HTML,这里我只研究如何转成HTML格式。在NewEdit中有一个plugin已经实现了,将全文转成HTML文件的功能,不过,由于要编辑Blog,这样将不会是全文,因此这里讲的是如何转换代码片段。

下面的代码是从docutils的examples.py中改造过来的。不过在examples.py中不建议导入这个模块,它提供了两个函数示例,我把它们合并在一起了。

1.2. 代码

   1         from docutils import core
   2         def html_fragment(input_string, source_path=None, destination_path=None,
   3                        input_encoding='unicode', doctitle=1, initial_header_level=1):
   4             overrides = {'input_encoding': input_encoding,
   5                          'doctitle_xform': doctitle,
   6                          'initial_header_level': initial_header_level}
   7             parts = core.publish_parts(
   8                 source=input_string, source_path=source_path,
   9                 destination_path=destination_path,
  10                 writer_name='html', settings_overrides=overrides)
  11             fragment = parts['fragment']
  12             return fragment

1.3. 下载

提供docutils下载,现在是0.3.3-alpha docutils-0.3.3-alpha.tar.gz

1.4. 讨论