4 # Author: Engelbert Gruber <grubert@users.sourceforge.net>
5 # Copyright: This module has been placed in the public domain.
8 rst2vers.py -- LaTeX verse writer
9 =================================
13 rst2vers.py input output
15 simple transformation from a reStructured text into a LaTeX
23 | your dogs, your daughters
24 | or the state of your kitchen.
26 | I will live in the secure orbit
30 \poemtitle{All My Exes}
33 your dogs, your daughters \\
34 or the state of your kitchen. \\
36 I will live in the secure orbit \\
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
):
51 class VerseLaTeXWriter(LaTeXWriter
):
53 LaTeXWriter
.__init
__(self
)
54 self
.translator_class
= VerseLaTeXTranslator
56 class VerseLaTeXTranslator(LaTeXTranslator
):
59 roman
= (None,None,"ii","iii","iv","v")
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.
72 return ''.join(self
.body
)
74 def visit_document(self
, node
):
76 def depart_document(self
, node
):
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 ')
89 self
.body
.append('\\begin{verse}\n')
90 def depart_line_block(self
, node
):
91 if len(self
._verse
_in
)>0:
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')
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
):
109 self
.body
.append(' \\\\\n')
112 def convert(infilename
, outfilename
):
113 print "converting %s to %s" % (infilename
, outfilename
)
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
)
126 convert(sys
.argv
[1], sys
.argv
[2])