Deprecated keys are output properly.
[docutils/kirr.git] / sandbox / rst2outline / rst2outline.py
blob2fb42b1787c5cb1a84ef4786373d8c4f3efb41f4
1 #!/usr/bin/env python
3 # Copyright (C) 2010 Stefan Merten
5 # rstdiff.py is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 2 of the License,
8 # or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, USA.
20 """
21 Translates a reStructuredText document to a plain text outline. This
22 can then be transformed_ to PowerPoint.
24 .. _transformed: http://www.pptfaq.com/FAQ00246.htm
25 """
27 try:
28 import locale
29 locale.setlocale(locale.LC_ALL, '')
30 except:
31 pass
33 from docutils import frontend, writers, nodes
34 from docutils.core import publish_cmdline, default_description
36 description = ('Generates a plain text outline from a standalone '
37 'reStructuredText source. ' + default_description)
39 class Writer(writers.Writer):
41 supported = ('outline',)
42 """Formats this writer supports."""
44 settings_spec = (
45 'Outline Writer Options',
46 None,
47 ( )
50 settings_defaults = { }
52 config_section = 'outline writer'
53 config_section_dependencies = ( 'writers', )
55 output = None
56 """Final translated form of `document`."""
58 def translate(self):
59 settings = self.document.settings
60 visitor = OutlineTranslator(self.document)
61 self.document.walkabout(visitor)
62 self.output = visitor.doc
64 class OutlineTranslator(nodes.SparseNodeVisitor):
66 doc = ""
67 """The resulting document."""
69 inTitle = False
70 inBullet = 0
71 inItem = [ False, ]
72 inPara = [ ]
74 def __init__(self, document):
75 nodes.SparseNodeVisitor.__init__(self, document)
77 def visit_title(self, node):
78 self.addBol(0)
79 self.inTitle = True
81 def depart_title(self, node):
82 self.addEol()
83 self.inTitle = False
85 def visit_bullet_list(self, node):
86 self.inBullet += 1
88 def depart_bullet_list(self, node):
89 self.inBullet -= 1
91 # TODO Other list types need to be included as well
93 def visit_list_item(self, node):
94 self.inItem = True
96 def depart_list_item(self, node):
97 self.inItem = False
99 def visit_paragraph(self, node):
100 if self.inItem:
101 self.inPara = True
102 self.inItem = False
103 self.addBol(self.inBullet)
105 def depart_paragraph(self, node):
106 if self.inPara:
107 self.addEol()
108 self.inPara = False
110 def visit_Text(self, node):
111 if self.inTitle or self.inPara:
112 self.addText(node.astext())
114 def addBol(self, level):
115 self.doc += "\t" * level
117 def addText(self, text):
118 self.doc += text.replace("\n", " ")
120 def addEol(self):
121 self.doc += "\r\n"
123 # TODO References to images should be included somehow
125 publish_cmdline(writer=Writer(), description=description)