added feedback
[docutils.git] / sandbox / verse-writer / rst2verse.py
blob4a5eeaa3842ad3d757fee80ca950295cb23e3319
1 #!/usr/bin/python
3 # $Id$
4 # Author: Engelbert Gruber <grubert@users.sourceforge.net>
5 # Copyright: This module has been placed in the public domain.
7 """
8 rst2vers.py -- LaTeX verse writer
9 =================================
11 Usage :
13 rst2vers.py input output
15 simple transformation from a reStructured text into a LaTeX
16 verse poem.
18 reSt input ::
20 All My Exes
21 ---------------
23 | your dogs, your daughters
24 | or the state of your kitchen.
26 | I will live in the secure orbit
28 output ::
30 \poemtitle{All My Exes}
32 \begin{verse}
33 your dogs, your daughters \\
34 or the state of your kitchen. \\
36 I will live in the secure orbit \\
38 \end{verse}
39 """
41 import sys
43 from docutils.core import Publisher
44 from docutils.readers.standalone import Reader as StandaloneReader
45 from docutils.writers.latex2e import Writer as LaTeXWriter, LaTeXTranslator
46 from docutils import nodes
48 class Reader(StandaloneReader):
49 pass
51 class VerseLaTeXWriter(LaTeXWriter):
52 def __init__(self):
53 LaTeXWriter.__init__(self)
54 self.translator_class = VerseLaTeXTranslator
56 class VerseLaTeXTranslator(LaTeXTranslator):
57 remap_title = {
59 roman = (None,None,"ii","iii","iv","v")
61 refuri_override = {
62 "reference" : "reference-guide",
63 "callbacks" : "option-callbacks",
66 def __init__(self, document):
67 LaTeXTranslator.__init__(self, document)
68 self._verse_in = [] # add vin indentation.
69 self._in_stanza = 0
71 def astext(self):
72 return ''.join(self.body)
74 def visit_document(self, node):
75 pass
76 def depart_document(self, node):
77 pass
79 def visit_title(self, node):
80 self.body.append('\\poemtitle{')
81 def depart_title(self, node):
82 self.body.append('}\n')
84 def visit_line_block(self, node):
85 if isinstance(node.parent, nodes.line_block):
86 # BUG only one indentation supported
87 self._verse_in.append('\\vin ')
88 else:
89 self.body.append('\\begin{verse}\n')
90 def depart_line_block(self, node):
91 if len(self._verse_in)>0:
92 self._verse_in.pop()
93 else:
94 self.body.append('\\end{verse}\n')
96 def visit_line(self, node):
97 if len(node.astext().strip())==0:
98 if self._in_stanza != 0:
99 # change last line end to ``\\!\n``
100 self.body[-1] = self.body[-1].rstrip() + '!\n'
101 self.body.append('\n')
102 self._in_stanza = 0
103 raise nodes.SkipNode
104 if len(self._verse_in)>0:
105 # BUG assume only one indentation is needed
106 self.body.append(self._verse_in[0])
107 def depart_line(self, node):
108 self._in_stanza = 1
109 self.body.append(' \\\\\n')
112 def convert(infilename, outfilename):
113 print "converting %s to %s" % (infilename, outfilename)
114 pub = Publisher()
115 pub.set_components('standalone', # reader
116 'restructuredtext', # parser
117 'latex') # writer (arg, will be discarded)
118 pub.reader = StandaloneReader()
119 pub.writer = VerseLaTeXWriter()
120 pub.process_programmatic_settings(None, None, None)
121 pub.set_source(source_path=infilename)
122 pub.set_destination(destination_path=outfilename)
123 pub.publish()
125 def main():
126 convert(sys.argv[1], sys.argv[2])
128 main()