bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
1 |
#!/usr/bin/env python
|
2 |
# -*- coding: utf8 -*-
|
|
3 |
# $Id: rst2pdf.py 5560 2008-05-20 13:00:31Z milde $
|
|
4 |
||
5 |
# rst2pdf.py
|
|
6 |
# ==========
|
|
7 |
# ::
|
|
8 |
||
9 |
"""
|
|
10 |
A front end to the Docutils Publisher, producing PDF.
|
|
11 |
||
12 |
Produces a latex file with the "latex" writer and converts
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
13 |
it to PDF with the "rubber" building system for LaTeX documents.
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
14 |
"""
|
15 |
||
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
|
|
19 |
# one step.
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
20 |
#
|
21 |
# It is implemented as a combination of docutils' ``rst2latex.py``
|
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
22 |
# by David Goodger and rubber_ by Emmanuel Beffara.
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
23 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
24 |
# Copyright: © 2008 Günter Milde
|
25 |
# Licensed under the `Apache License, Version 2.0`_
|
|
26 |
# Provided WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
27 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
28 |
# Changelog
|
29 |
# ---------
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
30 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
31 |
# ===== ========== =======================================================
|
32 |
# 0.1 2008-05-20 first attempt
|
|
33 |
# ===== ========== =======================================================
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
34 |
#
|
35 |
# ::
|
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
36 |
|
37 |
_version = 0.1 |
|
38 |
||
39 |
||
40 |
# Imports
|
|
41 |
# =======
|
|
42 |
# ::
|
|
43 |
||
44 |
#from pprint import pprint # for debugging
|
|
45 |
import os |
|
46 |
||
47 |
# Docutils::
|
|
48 |
||
49 |
try: |
|
50 |
import locale |
|
51 |
locale.setlocale(locale.LC_ALL, '') |
|
52 |
except: |
|
53 |
pass
|
|
54 |
||
55 |
from docutils.core import default_usage, default_description, Publisher |
|
56 |
||
57 |
# Rubber (rubber is not installed in the PYTHONPATH)::
|
|
58 |
||
59 |
import sys |
|
60 |
sys.path.append("/usr/share/rubber") |
|
61 |
||
62 |
try: |
|
63 |
import rubber.cmdline |
|
64 |
import rubber.cmd_pipe |
|
65 |
except ImportError: |
|
6619.3.3
by Jelmer Vernooij
Apply 2to3 print fix. |
66 |
print("Cannot find the rubber modules, rubber not installed correctly.") |
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
67 |
sys.exit(1) |
68 |
||
69 |
# Generate the latex file
|
|
70 |
# =======================
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
71 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
72 |
# We need to replace the <destination> by a intermediate latex file path.
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
73 |
# The most reliable way to get the value of <destination> is to
|
74 |
# call the Publisher "by hand", and query its settings.
|
|
75 |
#
|
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
76 |
# Modeled on the publish_cmdline() function of docutils.core
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
77 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
78 |
# Default values::
|
79 |
||
7143.16.17
by Jelmer Vernooij
Fix E225. |
80 |
reader = None |
7143.16.18
by Jelmer Vernooij
Fix E226. |
81 |
reader_name = 'standalone' |
7143.16.17
by Jelmer Vernooij
Fix E225. |
82 |
parser = None |
7143.16.18
by Jelmer Vernooij
Fix E226. |
83 |
parser_name = 'restructuredtext' |
7143.16.17
by Jelmer Vernooij
Fix E225. |
84 |
writer = None |
7143.16.18
by Jelmer Vernooij
Fix E226. |
85 |
writer_name = 'pseudoxml' |
7143.16.17
by Jelmer Vernooij
Fix E225. |
86 |
settings = None |
87 |
settings_spec = None |
|
88 |
settings_overrides = None |
|
89 |
config_section = None |
|
90 |
enable_exit_status = 1 |
|
91 |
argv = None |
|
92 |
usage = default_usage |
|
93 |
description = default_description |
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
94 |
|
95 |
# Argument values given to publish_cmdline() in rst2latex.py::
|
|
96 |
||
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' |
|
101 |
||
102 |
# Set up the publisher::
|
|
103 |
||
104 |
pub = Publisher(reader, parser, writer, settings=settings) |
|
105 |
pub.set_components(reader_name, parser_name, writer_name) |
|
106 |
||
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
107 |
# Parse the command line args
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
108 |
# (Publisher.publish does this in a try statement)::
|
109 |
||
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
110 |
pub.process_command_line(argv, usage, description, settings_spec, |
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
111 |
config_section, **(settings_overrides or {})) |
112 |
# pprint(pub.settings.__dict__)
|
|
113 |
||
114 |
# Get source and destination path::
|
|
115 |
||
116 |
source = pub.settings._source |
|
117 |
destination = pub.settings._destination |
|
118 |
# print source, destination
|
|
119 |
||
120 |
# Generate names for the temporary files and set ``destination`` to temporary
|
|
121 |
# latex file:
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
122 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
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. ::
|
|
127 |
||
128 |
tmppath = rubber.cmd_pipe.make_name() |
|
129 |
texpath = tmppath + ".tex" |
|
130 |
pdfpath = tmppath + ".pdf" |
|
131 |
||
132 |
pub.settings._destination = texpath |
|
133 |
||
134 |
# Now do the rst -> latex conversion::
|
|
135 |
||
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
136 |
pub.publish(argv, usage, description, settings_spec, settings_overrides, |
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
137 |
config_section=config_section, enable_exit_status=enable_exit_status) |
138 |
||
139 |
||
140 |
# Generating the PDF document with rubber
|
|
141 |
# =======================================
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
142 |
#
|
143 |
#
|
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
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::
|
|
146 |
||
147 |
rubber_argv = ["--pdf", # use pdflatex to produce PDF |
|
148 |
"--short", # Display LaTeX’s error messages one error per line. |
|
149 |
texpath
|
|
7143.16.6
by Jelmer Vernooij
Fix E124. |
150 |
]
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
151 |
|
152 |
# Get a TeX processing class instance and do the latex->pdf conversion::
|
|
153 |
||
154 |
tex_processor = rubber.cmdline.Main() |
|
155 |
tex_processor(rubber_argv) |
|
156 |
||
157 |
# Rename output to _destination or print to stdout::
|
|
158 |
||
159 |
if destination is None: |
|
6973.7.5
by Jelmer Vernooij
s/file/open. |
160 |
with open(pdfpath) as pdffile: |
6855.4.5
by Jelmer Vernooij
Fix more bees, use with rather than try/finally for some files. |
161 |
print(pdffile.read()) |
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
162 |
else: |
163 |
os.rename(pdfpath, destination) |
|
164 |
||
165 |
# Clean up (remove intermediate files)
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
166 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
167 |
# ::
|
168 |
||
169 |
tex_processor(["--clean"] + rubber_argv) |
|
170 |
os.remove(texpath) |
|
171 |
||
172 |
||
173 |
# .. References
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
174 |
#
|
4000.4.1
by Colin D Bennett
Generate PDF version of the User Guide. |
175 |
# .. _docutils: http://docutils.sourceforge.net/
|
176 |
# .. _rubber: http://www.pps.jussieu.fr/~beffara/soft/rubber/
|
|
177 |
# .. _Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
|
|
7143.15.5
by Jelmer Vernooij
More PEP8 fixes. |
178 |
#
|