Added option ``--no-create-links`` and made it the default.
[docutils.git] / sandbox / OpenDocument / tools / rst2odt.py
blobf126defe53dedcb7423126cc4742f083287198c1
1 #!/usr/bin/env python
3 # $Id$
4 # Author: Dave Kuhlman <dkuhlman@rexx.com>
5 # Copyright: This module has been placed in the public domain.
7 """
8 A front end to the Docutils Publisher, producing OpenOffice documents.
9 """
11 import sys
12 try:
13 import locale
14 locale.setlocale(locale.LC_ALL, '')
15 except:
16 pass
18 from docutils.core import publish_cmdline, default_description, \
19 Publisher, default_usage
20 from docutils import io
21 from docutils.writers.odtwriter import Writer, Reader
24 description = ('Generates OpenDocument/OpenOffice/ODF documents from '
25 'standalone reStructuredText sources. ' + default_description)
28 class BinaryFileOutput(io.FileOutput):
29 """
30 A version of docutils.io.FileOutput which writes to a binary file.
31 """
32 def open(self):
33 try:
34 self.destination = open(self.destination_path, 'wb')
35 except IOError, error:
36 if not self.handle_io_errors:
37 raise
38 print >>sys.stderr, '%s: %s' % (error.__class__.__name__,
39 error)
40 print >>sys.stderr, ('Unable to open destination file for writing '
41 '(%r). Exiting.' % self.destination_path)
42 sys.exit(1)
43 self.opened = 1
46 def publish_cmdline_to_binary(reader=None, reader_name='standalone',
47 parser=None, parser_name='restructuredtext',
48 writer=None, writer_name='pseudoxml',
49 settings=None, settings_spec=None,
50 settings_overrides=None, config_section=None,
51 enable_exit_status=1, argv=None,
52 usage=default_usage, description=default_description,
53 destination=None, destination_class=BinaryFileOutput
55 """
56 Set up & run a `Publisher` for command-line-based file I/O (input and
57 output file paths taken automatically from the command line). Return the
58 encoded string output also.
60 This is just like publish_cmdline, except that it uses
61 io.BinaryFileOutput instead of io.FileOutput.
63 Parameters: see `publish_programmatically` for the remainder.
65 - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``.
66 - `usage`: Usage string, output if there's a problem parsing the command
67 line.
68 - `description`: Program description, output for the "--help" option
69 (along with command-line option descriptions).
70 """
71 pub = Publisher(reader, parser, writer, settings=settings,
72 destination_class=destination_class)
73 pub.set_components(reader_name, parser_name, writer_name)
74 output = pub.publish(
75 argv, usage, description, settings_spec, settings_overrides,
76 config_section=config_section, enable_exit_status=enable_exit_status)
77 return output
80 writer = Writer()
81 reader = Reader()
82 output = publish_cmdline_to_binary(reader=reader, writer=writer, description=description)