Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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")

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top