single py file for convert rst to html
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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")
Solution 5
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.
linjunhalida
ubuntu, emacs, python, qt, c/c++, MFC, delphi, economics, gore, deathmetal...
Updated on March 22, 2020Comments
-
linjunhalida over 4 years
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? -
Gustavo Vargas about 10 yearsI don't think you need the full sphinx to this. rst2html.py is part of docutils which is a dependency of sphinx.
-
Calaf over 4 years@GustavoVargas What is an example of just one feature that can be achieved with Sphinx that cannot be done with just rst2html.py, while still aiming for just a one-rst-to-on-html workflow? (This is probably way too open-ended to qualify to be a question.)