Python 用于将 rst 转换为 html 的单个 py 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3819917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
single py file for convert rst to html
提问by linjunhalida
I have a blog written in reStructuredText which I currently have to manually convert to HTML when I make a new post.
我有一个用 reStructuredText 编写的博客,当我发表新文章时,我目前必须手动将其转换为 HTML。
I'm writing a new blog system using Google App Engine and need a simple way of converting rst to HTML.
我正在使用 Google App Engine 编写一个新的博客系统,需要一种将 rst 转换为 HTML 的简单方法。
I don't want to use docutilsbecause it is too big and complex. Is there a simpler (ideally single python file) way I can do this?
我不想使用,docutils因为它太大太复杂了。有没有更简单的(最好是单个 python 文件)方法可以做到这一点?
采纳答案by Matti Pastell
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.
看看黑客 docutils的说明。您不需要整个 docutils 从 rst 生成 html,但您确实需要阅读器、解析器、转换器和编写器。通过一些努力,您可以将所有这些组合到现有 docutils 文件中的单个文件中。
回答by pyfunc
docutils is a library that you can install. It also installs front end tools to convert from rest to various formats including html.
docutils 是一个可以安装的库。它还安装了前端工具以将其余格式转换为包括 html 在内的各种格式。
This is a stand alone tool that can be used.
这是一个可以使用的独立工具。
Most converters will exploit the docutils library for this.
大多数转换器会为此利用 docutils 库。
回答by AndrewF
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.
如果 Pyfunc 的答案不符合您的需求,您可以考虑改用 Markdown 语言。语法和 rst 类似,markdown.py 比较小,好用。它仍然不是单个文件,但您可以将其作为模块导入到您可能拥有的任何现有脚本中。
回答by Blaker
The Sphinx documentation generator Python library includes many restructured text (RST) command-line converters.
Sphinx 文档生成器 Python 库包括许多重组文本 (RST) 命令行转换器。
Install Sphinx:
安装狮身人面像:
$ pip install sphinx
Then use one of the many rst2*.py helpers:
然后使用众多 rst2*.py 助手之一:
$ rst2html.py in_file.rst out_file.html
回答by bunkus
Well you could try it with the following piece of code, usage would be:
好吧,您可以使用以下代码进行尝试,用法是:
compile_rst.py yourtext.rst
compile_rst.py yourtext.rst
or
或者
compile_rst.py yourtext.rst desiredname.html
compile_rst.py yourtext.rst 所需名称.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")
回答by maxlogo
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.

