summaryrefslogtreecommitdiff
path: root/content/resources/rstodt/rst2odt.py
blob: f1f107f5a4edfb551e8e94122356c37bdc49673a (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
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python

# $Id: rst2odt.py 5839 2009-01-07 19:09:28Z dkuhlman $
# Author: Dave Kuhlman <dkuhlman@rexx.com>
# Copyright: This module has been placed in the public domain.

"""
A front end to the Docutils Publisher, producing OpenOffice documents.
"""

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

from docutils.core import publish_cmdline_to_binary, default_description
from docutils.writers.odf_odt import Writer, Reader

from docutils.parsers.rst.roles import set_classes

from docutils import nodes
from docutils.parsers.rst import directives, Directive


description = ('Generates OpenDocument/OpenOffice/ODF documents from '
               'standalone reStructuredText sources.  ' + default_description)

class Codeblock(Directive):
    """ Source code syntax highlighting.
    """
    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = True

    has_content = True
    linenos=1

    directives = {":include:": lambda self, *args, **kwargs : self.include(*args, **kwargs)}

    def include(self, param):
        """ Include a file and return it as a content.
        """
        try:
            self.content = unicode(open(param, 'r').read(), "utf-8").split("\n")
        except IOError:
            self.content = ("FileNotFound",)

    def run(self):
        for argument in self.arguments:
            directives = argument.split(" ")
            processor = self.directives.get(directives[0], None)
            if processor is None:
                continue
            processor(self, directives[1])

        set_classes(self.options)
        self.assert_has_content()
        text = '\n'.join(self.content)
        text_nodes, messages = self.state.inline_text(text, self.lineno)
        node = nodes.literal_block(text, '', *text_nodes, **self.options)
        node.line = self.content_offset + 1
        self.add_name(node)
        node.attributes['language'] = self.arguments[0]

        return [node] + messages


directives.register_directive('code-block', Codeblock)


writer = Writer()
reader = Reader()
output = publish_cmdline_to_binary(reader=reader, writer=writer,
    description=description)