Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single py file for convert rst to html

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?

like image 969
linjunhalida Avatar asked Sep 29 '10 08:09

linjunhalida


4 Answers

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

  • http://docutils.sourceforge.net/docs/user/tools.html#rst2html-py

This is a stand alone tool that can be used.

Most converters will exploit the docutils library for this.

like image 132
pyfunc Avatar answered Oct 07 '22 16:10

pyfunc


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
like image 23
Blaker Avatar answered Oct 07 '22 16:10

Blaker


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.

like image 6
Matti Pastell Avatar answered Oct 07 '22 16:10

Matti Pastell


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")
like image 4
bunkus Avatar answered Oct 07 '22 16:10

bunkus