summaryrefslogtreecommitdiff
path: root/content/resources/rst_graphviz/rst2latex.py
blob: 9185405b19e2f096909a602ab04a4c25643bd0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/python

# $Id: rst2latex.py 5905 2009-04-16 12:04:49Z milde $
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.

"""
A minimal front end to the Docutils Publisher, producing LaTeX.
"""

try:
    import locale
    locale.setlocale(locale.LC_ALL, '')
except:
    pass

import subprocess
import os.path

from docutils.core import publish_cmdline
from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.images import Figure

description = ('Generates LaTeX documents from standalone reStructuredText '
               'sources. '
               'Reads from <source> (default is stdin) and writes to '
               '<destination> (default is stdout).  See '
               '<http://docutils.sourceforge.net/docs/user/latex.html> for '
               'the full reference.')

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='latex', description=description)