用于将 rst 转换为 html 的单个 py 文件

发布于 2024-09-25 08:22:21 字数 212 浏览 5 评论 0原文

我有一个用 reStructuredText 编写的博客,目前我在发表新帖子时必须手动将其转换为 HTML。

我正在使用 Google App Engine 编写一个新的博客系统,需要一种将 rst 转换为 HTML 的简单方法。

我不想使用 docutils 因为它太大而且太复杂。有没有更简单的(最好是单个 python 文件)方法可以做到这一点?

I have a blog written in reStructuredText which I currently have to manually convert to HTML when I make a new post.

I'm writing a new blog system using Google App Engine and need a simple way of converting rst to HTML.

I don't want to use docutils because it is too big and complex. Is there a simpler (ideally single python file) way I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

忆依然 2024-10-02 08:22:21

docutils 是一个可以安装的库。它还安装前端工具,将静态格式转换为各种格式,包括 html。

这是一个可以使用的独立工具。

大多数转换器将利用 docutils 库来实现此目的。

docutils is a library that you can install. It also installs front end tools to convert from rest to various formats including html.

This is a stand alone tool that can be used.

Most converters will exploit the docutils library for this.

可可 2024-10-02 08:22:21

Sphinx 文档生成器 Python 库包含许多重构文本 (RST) 命令行转换器。

安装 Sphinx:

$ pip install sphinx

然后使用众多 rst2*.py 帮助程序之一:

$ rst2html.py in_file.rst out_file.html

The Sphinx documentation generator Python library includes many restructured text (RST) command-line converters.

Install Sphinx:

$ pip install sphinx

Then use one of the many rst2*.py helpers:

$ rst2html.py in_file.rst out_file.html
花开半夏魅人心 2024-10-02 08:22:21

查看黑客 docutils 的说明。您不需要整个文档来从一开始生成 html,但您确实需要读取器、解析器、转换器和写入器。经过一些努力,您可以将所有这些内容从现有的 docutils 文件合并到一个文件中。

Have a look at the instructions for hacking docutils. You don't need the whole docutils to produce a html from rst, but you do need a reader, parser, transformer and writer. With some effort you could combine all of these to a single file from the existing docutils files.

傲世九天 2024-10-02 08:22:21

那么你可以用下面的代码尝试一下,用法是:

compile_rst.py yourtext.rst

compile_rst.py yourtext.rstdesiredname.html

# compile_rst.py

from __future__ import print_function
from docutils import core
from docutils.writers.html4css1 import Writer,HTMLTranslator
import sys, os

class HTMLFragmentTranslator( HTMLTranslator ):
    def __init__( self, document ):
        HTMLTranslator.__init__( self, document )
        self.head_prefix = ['','','','','']
        self.body_prefix = []
        self.body_suffix = []
        self.stylesheet = []
    def astext(self):
        return ''.join(self.body)

html_fragment_writer = Writer()
html_fragment_writer.translator_class = HTMLFragmentTranslator

def reST_to_html( s ):
    return core.publish_string( s, writer = html_fragment_writer )

if __name__ == '__main__':
    if len(sys.argv)>1:
        if sys.argv[1] != "":
            rstfile = open(sys.argv[1])
            text = rstfile.read()
            rstfile.close()
            if len(sys.argv)>2:
                if sys.argv[2] != "":
                    htmlfile = sys.argv[2]
            else:
                htmlfile = os.path.splitext(os.path.basename(sys.argv[1]))[0]+".html"
            result = reST_to_html(text)
            print(result)
            output = open(htmlfile, "wb")
            output.write(result)
            output.close()  
    else:
        print("Usage:\ncompile_rst.py docname.rst\nwhich results in => docname.html\ncompile_rst.py docname.rst desiredname.html\nwhich results in => desiredname.html")

Well you could try it with the following piece of code, usage would be:

compile_rst.py yourtext.rst

or

compile_rst.py yourtext.rst desiredname.html

# compile_rst.py

from __future__ import print_function
from docutils import core
from docutils.writers.html4css1 import Writer,HTMLTranslator
import sys, os

class HTMLFragmentTranslator( HTMLTranslator ):
    def __init__( self, document ):
        HTMLTranslator.__init__( self, document )
        self.head_prefix = ['','','','','']
        self.body_prefix = []
        self.body_suffix = []
        self.stylesheet = []
    def astext(self):
        return ''.join(self.body)

html_fragment_writer = Writer()
html_fragment_writer.translator_class = HTMLFragmentTranslator

def reST_to_html( s ):
    return core.publish_string( s, writer = html_fragment_writer )

if __name__ == '__main__':
    if len(sys.argv)>1:
        if sys.argv[1] != "":
            rstfile = open(sys.argv[1])
            text = rstfile.read()
            rstfile.close()
            if len(sys.argv)>2:
                if sys.argv[2] != "":
                    htmlfile = sys.argv[2]
            else:
                htmlfile = os.path.splitext(os.path.basename(sys.argv[1]))[0]+".html"
            result = reST_to_html(text)
            print(result)
            output = open(htmlfile, "wb")
            output.write(result)
            output.close()  
    else:
        print("Usage:\ncompile_rst.py docname.rst\nwhich results in => docname.html\ncompile_rst.py docname.rst desiredname.html\nwhich results in => desiredname.html")
随梦而飞# 2024-10-02 08:22:21

在本地构建文档

Install Python.
Clone the forked repository to your computer.
Open the folder that contains the repository.
Execute: pip install -r requirements.txt --ignore-installed
Execute: sphinx-build -b html docs build
The rendered documentation is now in the build directory as HTML.

Building the doc locally

Install Python.
Clone the forked repository to your computer.
Open the folder that contains the repository.
Execute: pip install -r requirements.txt --ignore-installed
Execute: sphinx-build -b html docs build
The rendered documentation is now in the build directory as HTML.
北城孤痞 2024-10-02 08:22:21

如果 Pyfunc 的答案不能满足您的需求,您可以考虑使用 Markdown 语言。语法与 rst 类似,并且 markdown.py 相当小且易于使用。它仍然不是单个文件,但您可以将其作为模块导入到您可能拥有的任何现有脚本中。

http://www.freewisdom.org/projects/python-markdown/

If Pyfunc's answer doesn't fit your needs, you could consider using the Markdown language instead. The syntax is similar to rst, and markdown.py is fairly small and easy to use. It's still not a single file, but you can import it as a module into any existing scripts you may have.

http://www.freewisdom.org/projects/python-markdown/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文