summaryrefslogtreecommitdiff
path: root/sandbox/rst2outline/rst2outline.py
blob: 2fb42b1787c5cb1a84ef4786373d8c4f3efb41f4 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python

# Copyright (C) 2010 Stefan Merten

# rstdiff.py is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

"""
Translates a reStructuredText document to a plain text outline. This
can then be transformed_ to PowerPoint.

.. _transformed: http://www.pptfaq.com/FAQ00246.htm
"""

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

from docutils import frontend, writers, nodes
from docutils.core import publish_cmdline, default_description

description = ('Generates a plain text outline from a standalone '
               'reStructuredText source.  ' + default_description)

class Writer(writers.Writer):

    supported = ('outline',)
    """Formats this writer supports."""

    settings_spec = (
        'Outline Writer Options',
        None,
        ( )
        )

    settings_defaults = { }

    config_section = 'outline writer'
    config_section_dependencies = ( 'writers', )

    output = None
    """Final translated form of `document`."""

    def translate(self):
        settings = self.document.settings
        visitor = OutlineTranslator(self.document)
        self.document.walkabout(visitor)
        self.output = visitor.doc

class OutlineTranslator(nodes.SparseNodeVisitor):

    doc = ""
    """The resulting document."""

    inTitle = False
    inBullet = 0
    inItem = [ False, ]
    inPara = [ ]

    def __init__(self, document):
        nodes.SparseNodeVisitor.__init__(self, document)

    def visit_title(self, node):
        self.addBol(0)
        self.inTitle = True

    def depart_title(self, node):
        self.addEol()
        self.inTitle = False

    def visit_bullet_list(self, node):
        self.inBullet += 1

    def depart_bullet_list(self, node):
        self.inBullet -= 1

    # TODO Other list types need to be included as well

    def visit_list_item(self, node):
        self.inItem = True

    def depart_list_item(self, node):
        self.inItem = False

    def visit_paragraph(self, node):
        if self.inItem:
            self.inPara = True
            self.inItem = False
            self.addBol(self.inBullet)

    def depart_paragraph(self, node):
        if self.inPara:
            self.addEol()
        self.inPara = False

    def visit_Text(self, node):
        if self.inTitle or self.inPara:
            self.addText(node.astext())

    def addBol(self, level):
        self.doc += "\t" * level

    def addText(self, text):
        self.doc += text.replace("\n", " ")

    def addEol(self):
        self.doc += "\r\n"

# TODO References to images should be included somehow

publish_cmdline(writer=Writer(), description=description)