linjunhalida
linjunhalida

Reputation: 4655

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?

Upvotes: 23

Views: 29600

Answers (6)

maxlogo
maxlogo

Reputation: 11

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.

Upvotes: 1

bunkus
bunkus

Reputation: 1025

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

Upvotes: 4

Blaker
Blaker

Reputation: 819

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

Upvotes: 23

Matti Pastell
Matti Pastell

Reputation: 9303

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.

Upvotes: 6

AndrewF
AndrewF

Reputation: 6992

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/

Upvotes: 0

pyfunc
pyfunc

Reputation: 66739

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.

Upvotes: 29

Related Questions