##language:zh -- limodou [<>] <> = 将reStructuredText转成Html = ''这是从Python cookbook中看到的'' == 介绍 == reStructuredText是一种格式化文本,已经广泛应用在Python的文档中了。reStructuredText可以转换成多种格式,不只是HTML,这里我只研究如何转成HTML格式。在NewEdit中有一个plugin已经实现了,将全文转成HTML文件的功能,不过,由于要编辑Blog,这样将不会是全文,因此这里讲的是如何转换代码片段。 下面的代码是从docutils的examples.py中改造过来的。不过在examples.py中不建议导入这个模块,它提供了两个函数示例,我把它们合并在一起了。 == 代码 == {{{#!python from docutils import core def html_fragment(input_string, source_path=None, destination_path=None, input_encoding='unicode', doctitle=1, initial_header_level=1): overrides = {'input_encoding': input_encoding, 'doctitle_xform': doctitle, 'initial_header_level': initial_header_level} parts = core.publish_parts( source=input_string, source_path=source_path, destination_path=destination_path, writer_name='html', settings_overrides=overrides) fragment = parts['fragment'] return fragment }}} == 下载 == 提供docutils下载,现在是0.3.3-alpha [[attachment:docutils-0.3.3-alpha.tar.gz]] == 讨论 ==