#!/usr/bin/python
# $Id: rst2html.py 4564 2006-05-21 20:44:42Z wiemann $
# Author: David Goodger 
# Copyright: This module has been placed in the public domain.
"""
A minimal front end to the Docutils Publisher, producing HTML.
"""
try:
    import locale
    locale.setlocale(locale.LC_ALL, '')
except:
    pass
import subprocess
import os.path
from docutils.core import publish_cmdline, default_description
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.images import Figure
description = ('Generates (X)HTML documents from standalone reStructuredText '
               'sources.  ' + default_description)
class Graphviz(Figure):
    """ Generate a graphviz image
    """
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True
    has_content = True
    option_spec = Figure.option_spec.copy()
    def run(self):
        text = '\n'.join(self.content)
        extension = 'png'
        imageFile = "tmp/%s.%s" % (hash(text), extension)
        if not os.path.exists(imageFile):
            conversion = subprocess.Popen(['/usr/bin/dot',
                '-T', 'png',
                '-o', imageFile,
                ],
                stdin=subprocess.PIPE
                )
            conversion.communicate(text)
            conversion.wait()
        self.arguments = [imageFile]
        self.options['scale'] = 50
        self.content = None
        return Figure.run(self)
directives.register_directive('graphviz', Graphviz)
publish_cmdline(writer_name='html', description=description)