pep2html.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pep-drafts 作者: fedora-python 项目源码 文件源码
def fix_rst_pep(input_lines, outfile, inpath, pepnum):

    class XXXDirective(rst.Directive):
        has_content = True
        def run(self):
            # Raise an error if the directive does not have contents.
            self.assert_has_content()
            text = '\n'.join(self.content)
            # Create the admonition node, to be populated by `nested_parse`.
            admonition_node = nodes.admonition(rawsource=text)
            # Parse the directive contents.
            self.state.nested_parse(self.content, self.content_offset,
                                    admonition_node)
            title = nodes.title('', 'XXX')
            admonition_node.insert(0, title)
            return [admonition_node]
    directives.register_directive('xxx', XXXDirective)

    handle, template_file_name = tempfile.mkstemp(text=True)
    try:
        orig_template_name = pep_html.Writer.default_template_path
        with open(orig_template_name) as inf, open(handle, 'w') as outf:
            content = inf.read()
            content = content.replace('%(pepnum)s.txt', '%(pepnum)s.rst')
            content = content.replace("%(pepindex)s/", "%(pepindex)s")
            outf.write(content)

        output = core.publish_string(
            source=''.join(input_lines),
            source_path=inpath,
            destination_path=outfile.name,
            reader=Reader(),
            parser_name='restructuredtext',
            writer=Writer(pepnum),
            settings=None,
            # Allow Docutils traceback if there's an exception:
            settings_overrides={
                'traceback': 1,
                'template': template_file_name,
            })
        outfile.write(output.decode('utf-8'))
    finally:
        os.unlink(template_file_name)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号