22

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?

Lexi
  • 1,670
  • 1
  • 19
  • 35
linjunhalida
  • 4,538
  • 6
  • 44
  • 64

6 Answers6

28

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.

pyfunc
  • 65,343
  • 15
  • 148
  • 136
21

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
Blaker
  • 769
  • 1
  • 8
  • 12
  • 16
    I don't think you need the full sphinx to this. rst2html.py is part of docutils which is a dependency of sphinx. – Gustavo Vargas Jun 13 '14 at 03:27
  • @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.) – Calaf Nov 23 '19 at 18:11
6

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.

Matti Pastell
  • 9,135
  • 3
  • 37
  • 44
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")
bunkus
  • 975
  • 11
  • 19
1

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.
maxlogo
  • 11
  • 1
0

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/

AndrewF
  • 6,852
  • 7
  • 29
  • 27