10 A front end to the Docutils Publisher, producing PDF.
12 Produces a latex file with the "latex" writer and converts
13 it to PDF with the "rubber" building system for LaTeX documents.
16 # ``rst2pdf.py`` is a PDF front-end for docutils that is compatible
17 # with the ``rst2*.py`` front ends of the docutils_ suite.
18 # It enables the generation of PDF documents from a reStructuredText source in
21 # It is implemented as a combination of docutils' ``rst2latex.py``
22 # by David Goodger and rubber_ by Emmanuel Beffara.
24 # Copyright: © 2008 Günter Milde
25 # Licensed under the `Apache License, Version 2.0`_
26 # Provided WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
31 # ===== ========== =======================================================
32 # 0.1 2008-05-20 first attempt
33 # ===== ========== =======================================================
44 #from pprint import pprint # for debugging
51 locale
.setlocale(locale
.LC_ALL
, '')
55 from docutils
.core
import default_usage
, default_description
, Publisher
57 # Rubber (rubber is not installed in the PYTHONPATH)::
60 sys
.path
.append("/usr/share/rubber")
64 import rubber
.cmd_pipe
66 print "Cannot find the rubber modules, rubber not installed correctly."
69 # Generate the latex file
70 # =======================
72 # We need to replace the <destination> by a intermediate latex file path.
73 # The most reliable way to get the value of <destination> is to
74 # call the Publisher "by hand", and query its settings.
76 # Modeled on the publish_cmdline() function of docutils.core
81 reader_name
='standalone'
83 parser_name
='restructuredtext'
85 writer_name
='pseudoxml'
88 settings_overrides
=None
93 description
=default_description
95 # Argument values given to publish_cmdline() in rst2latex.py::
97 description
= ('Generates PDF documents from standalone reStructuredText '
98 'sources using the "latex" Writer and the "rubber" '
99 'building system for LaTeX documents. ' + default_description
)
100 writer_name
= 'latex'
102 # Set up the publisher::
104 pub
= Publisher(reader
, parser
, writer
, settings
=settings
)
105 pub
.set_components(reader_name
, parser_name
, writer_name
)
107 # Parse the command line args
108 # (Publisher.publish does this in a try statement)::
110 pub
.process_command_line(argv
, usage
, description
, settings_spec
,
111 config_section
, **(settings_overrides
or {}))
112 # pprint(pub.settings.__dict__)
114 # Get source and destination path::
116 source
= pub
.settings
._source
117 destination
= pub
.settings
._destination
118 # print source, destination
120 # Generate names for the temporary files and set ``destination`` to temporary
123 # make_name() from rubber.cmd_pipe checks that no existing file is
124 # overwritten. If we are going to support rubbers ``--inplace`` and ``--into``
125 # options, the chdir() must occure before this point to have the check in the
126 # right directory. ::
128 tmppath
= rubber
.cmd_pipe
.make_name()
129 texpath
= tmppath
+ ".tex"
130 pdfpath
= tmppath
+ ".pdf"
132 pub
.settings
._destination
= texpath
134 # Now do the rst -> latex conversion::
136 pub
.publish(argv
, usage
, description
, settings_spec
, settings_overrides
,
137 config_section
=config_section
, enable_exit_status
=enable_exit_status
)
140 # Generating the PDF document with rubber
141 # =======================================
144 # rubber_ has no documentet API for programmatic use. We simualate a command
145 # line call and pass command line arguments (see man: rubber-pipe) in an array::
147 rubber_argv
= ["--pdf", # use pdflatex to produce PDF
148 "--short", # Display LaTeX’s error messages one error per line.
152 # Get a TeX processing class instance and do the latex->pdf conversion::
154 tex_processor
= rubber
.cmdline
.Main()
155 tex_processor(rubber_argv
)
157 # Rename output to _destination or print to stdout::
159 if destination
is None:
160 pdffile
= file(pdfpath
)
164 os
.rename(pdfpath
, destination
)
166 # Clean up (remove intermediate files)
170 tex_processor(["--clean"] + rubber_argv
)
176 # .. _docutils: http://docutils.sourceforge.net/
177 # .. _rubber: http://www.pps.jussieu.fr/~beffara/soft/rubber/
178 # .. _Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0