152
179
encoding=self.settings.output_encoding,
153
180
error_handler=self.settings.output_encoding_error_handler)
155
def apply_transforms(self, document):
156
document.transformer.populate_from_components(
182
def apply_transforms(self):
183
self.document.transformer.populate_from_components(
157
184
(self.source, self.reader, self.reader.parser, self.writer,
158
185
self.destination))
159
document.transformer.apply_transforms()
186
self.document.transformer.apply_transforms()
161
188
def publish(self, argv=None, usage=None, description=None,
162
189
settings_spec=None, settings_overrides=None,
163
config_section=None, enable_exit=None):
190
config_section=None, enable_exit_status=None):
165
192
Process command line options and arguments (if `self.settings` not
166
193
already set), run `self.reader` and then `self.writer`. Return
167
194
`self.writer`'s output.
169
if self.settings is None:
170
self.process_command_line(
171
argv, usage, description, settings_spec, config_section,
172
**(settings_overrides or {}))
173
elif settings_overrides:
174
self.settings._update(settings_overrides, 'loose')
179
document = self.reader.read(self.source, self.parser,
181
self.apply_transforms(document)
182
output = self.writer.write(document, self.destination)
198
if self.settings is None:
199
self.process_command_line(
200
argv, usage, description, settings_spec, config_section,
201
**(settings_overrides or {}))
203
self.document = self.reader.read(self.source, self.parser,
205
self.apply_transforms()
206
output = self.writer.write(self.document, self.destination)
183
207
self.writer.assemble_parts()
184
except utils.SystemMessage, error:
185
if self.settings.traceback:
187
print >>sys.stderr, ('Exiting due to level-%s (%s) system message.'
189
utils.Reporter.levels[error.level]))
208
except SystemExit, error:
210
exit_status = error.code
191
211
except Exception, error:
192
if self.settings.traceback:
194
print >>sys.stderr, error
195
print >>sys.stderr, ("""\
196
Exiting due to error. Use "--traceback" to diagnose.
197
Please report errors to <docutils-users@lists.sf.net>.
198
Include "--traceback" output, Docutils version (%s),
199
Python version (%s), your OS type & version, and the
200
command line used.""" % (__version__, sys.version.split()[0]))
212
if not self.settings: # exception too early to report nicely
214
if self.settings.traceback: # Propagate exceptions?
215
self.debugging_dumps()
217
self.report_Exception(error)
220
self.debugging_dumps()
221
if (enable_exit_status and self.document
222
and (self.document.reporter.max_level
223
>= self.settings.exit_status_level)):
224
sys.exit(self.document.reporter.max_level + 10)
226
sys.exit(exit_status)
229
def debugging_dumps(self):
230
if not self.document:
202
232
if self.settings.dump_settings:
203
from pprint import pformat
204
233
print >>sys.stderr, '\n::: Runtime settings:'
205
print >>sys.stderr, pformat(self.settings.__dict__)
206
if self.settings.dump_internals and document:
207
from pprint import pformat
234
print >>sys.stderr, pprint.pformat(self.settings.__dict__)
235
if self.settings.dump_internals:
208
236
print >>sys.stderr, '\n::: Document internals:'
209
print >>sys.stderr, pformat(document.__dict__)
210
if self.settings.dump_transforms and document:
211
from pprint import pformat
237
print >>sys.stderr, pprint.pformat(self.document.__dict__)
238
if self.settings.dump_transforms:
212
239
print >>sys.stderr, '\n::: Transforms applied:'
213
print >>sys.stderr, pformat(document.transformer.applied)
214
if self.settings.dump_pseudo_xml and document:
240
print >>sys.stderr, (' (priority, transform class, '
241
'pending node details, keyword args)')
242
print >>sys.stderr, pprint.pformat(
243
[(priority, '%s.%s' % (xclass.__module__, xclass.__name__),
244
pending and pending.details, kwargs)
245
for priority, xclass, pending, kwargs
246
in self.document.transformer.applied])
247
if self.settings.dump_pseudo_xml:
215
248
print >>sys.stderr, '\n::: Pseudo-XML:'
216
print >>sys.stderr, document.pformat().encode(
249
print >>sys.stderr, self.document.pformat().encode(
217
250
'raw_unicode_escape')
218
if enable_exit and document and (document.reporter.max_level
219
>= self.settings.exit_level):
220
sys.exit(document.reporter.max_level + 10)
252
def report_Exception(self, error):
253
if isinstance(error, utils.SystemMessage):
254
self.report_SystemMessage(error)
255
elif isinstance(error, UnicodeError):
256
self.report_UnicodeError(error)
258
print >>sys.stderr, '%s: %s' % (error.__class__.__name__, error)
259
print >>sys.stderr, ("""\
260
Exiting due to error. Use "--traceback" to diagnose.
261
Please report errors to <docutils-users@lists.sf.net>.
262
Include "--traceback" output, Docutils version (%s [%s]),
263
Python version (%s), your OS type & version, and the
264
command line used.""" % (__version__, __version_details__,
265
sys.version.split()[0]))
267
def report_SystemMessage(self, error):
268
print >>sys.stderr, ('Exiting due to level-%s (%s) system message.'
270
utils.Reporter.levels[error.level]))
272
def report_UnicodeError(self, error):
276
'The specified output encoding (%s) cannot\n'
277
'handle all of the output.\n'
278
'Try setting "--output-encoding-error-handler" to\n'
280
'* "xmlcharrefreplace" (for HTML & XML output);\n'
281
% (error.__class__.__name__, error,
282
self.settings.output_encoding))
284
data = error.object[error.start:error.end]
286
' the output will contain "%s" and should be usable.\n'
287
'* "backslashreplace" (for other output formats, Python 2.3+);\n'
288
' look for "%s" in the output.\n'
289
% (data.encode('ascii', 'xmlcharrefreplace'),
290
data.encode('ascii', 'backslashreplace')))
291
except AttributeError:
292
sys.stderr.write(' the output should be usable as-is.\n')
294
'* "replace"; look for "?" in the output.\n'
296
'"--output-encoding-error-handler" is currently set to "%s".\n'
298
'Exiting due to error. Use "--traceback" to diagnose.\n'
299
'If the advice above doesn\'t eliminate the error,\n'
300
'please report it to <docutils-users@lists.sf.net>.\n'
301
'Include "--traceback" output, Docutils version (%s),\n'
302
'Python version (%s), your OS type & version, and the\n'
303
'command line used.\n'
304
% (self.settings.output_encoding_error_handler,
305
__version__, sys.version.split()[0]))
226
307
default_usage = '%prog [options] [<source> [<destination>]]'
227
308
default_description = ('Reads from <source> (default is stdin) and writes to '
273
341
parser=None, parser_name='restructuredtext',
274
342
writer=None, writer_name='pseudoxml',
275
343
settings=None, settings_spec=None, settings_overrides=None,
276
config_section=None, enable_exit=None):
278
Set up & run a `Publisher`. For programmatic use with file-like I/O.
282
- `source`: A file-like object (must have "read" and "close" methods).
283
- `source_path`: Path to the input file. Opened if no `source` supplied.
284
If neither `source` nor `source_path` are supplied, `sys.stdin` is used.
285
- `destination`: A file-like object (must have "write" and "close"
287
- `destination_path`: Path to the input file. Opened if no `destination`
288
supplied. If neither `destination` nor `destination_path` are supplied,
289
`sys.stdout` is used.
290
- `reader`: A `docutils.readers.Reader` object.
291
- `reader_name`: Name or alias of the Reader class to be instantiated if
292
no `reader` supplied.
293
- `parser`: A `docutils.parsers.Parser` object.
294
- `parser_name`: Name or alias of the Parser class to be instantiated if
295
no `parser` supplied.
296
- `writer`: A `docutils.writers.Writer` object.
297
- `writer_name`: Name or alias of the Writer class to be instantiated if
298
no `writer` supplied.
299
- `settings`: Runtime settings object.
300
- `settings_spec`: Extra settings specification; a `docutils.SettingsSpec`
301
subclass. Used only if no `settings` specified.
302
- `settings_overrides`: A dictionary containing program-specific overrides
303
of component settings.
304
- `config_section`: Name of configuration file section for application.
305
Used only if no `settings` or `settings_spec` specified.
306
- `enable_exit`: Boolean; enable exit status at end of processing?
308
pub = Publisher(reader, parser, writer, settings=settings)
309
pub.set_components(reader_name, parser_name, writer_name)
311
settings = pub.get_settings(settings_spec=settings_spec,
312
config_section=config_section)
313
if settings_overrides:
314
settings._update(settings_overrides, 'loose')
315
pub.set_source(source, source_path)
316
pub.set_destination(destination, destination_path)
317
pub.publish(enable_exit=enable_exit)
319
def publish_string(source, source_path=None, destination_path=None,
344
config_section=None, enable_exit_status=None):
346
Set up & run a `Publisher` for programmatic use with file-like I/O.
347
Return the encoded string output also.
349
Parameters: see `publish_programmatically`.
351
output, pub = publish_programmatically(
352
source_class=io.FileInput, source=source, source_path=source_path,
353
destination_class=io.FileOutput,
354
destination=destination, destination_path=destination_path,
355
reader=reader, reader_name=reader_name,
356
parser=parser, parser_name=parser_name,
357
writer=writer, writer_name=writer_name,
358
settings=settings, settings_spec=settings_spec,
359
settings_overrides=settings_overrides,
360
config_section=config_section,
361
enable_exit_status=enable_exit_status)
364
def publish_string(source, source_path=None, destination_path=None,
320
365
reader=None, reader_name='standalone',
321
366
parser=None, parser_name='restructuredtext',
322
367
writer=None, writer_name='pseudoxml',
323
368
settings=None, settings_spec=None,
324
369
settings_overrides=None, config_section=None,
370
enable_exit_status=None):
327
Set up & run a `Publisher`, and return the string output.
328
For programmatic use with string I/O.
372
Set up & run a `Publisher` for programmatic use with string I/O. Return
373
the encoded string or Unicode string output.
330
For encoded string output, be sure to set the "output_encoding" setting to
331
the desired encoding. Set it to "unicode" for unencoded Unicode string
375
For encoded string output, be sure to set the 'output_encoding' setting to
376
the desired encoding. Set it to 'unicode' for unencoded Unicode string
377
output. Here's one way::
334
379
publish_string(..., settings_overrides={'output_encoding': 'unicode'})
338
383
publish_string(..., settings_overrides={'input_encoding': 'unicode'})
342
- `source`: An input string; required. This can be an encoded 8-bit
343
string (set the "input_encoding" setting to the correct encoding) or a
344
Unicode string (set the "input_encoding" setting to "unicode").
345
- `source_path`: Path to the file or object that produced `source`;
346
optional. Only used for diagnostic output.
347
- `destination_path`: Path to the file or object which will receive the
348
output; optional. Used for determining relative paths (stylesheets,
350
- `reader`: A `docutils.readers.Reader` object.
351
- `reader_name`: Name or alias of the Reader class to be instantiated if
352
no `reader` supplied.
353
- `parser`: A `docutils.parsers.Parser` object.
354
- `parser_name`: Name or alias of the Parser class to be instantiated if
355
no `parser` supplied.
356
- `writer`: A `docutils.writers.Writer` object.
357
- `writer_name`: Name or alias of the Writer class to be instantiated if
358
no `writer` supplied.
359
- `settings`: Runtime settings object.
360
- `settings_spec`: Extra settings specification; a `docutils.SettingsSpec`
361
subclass. Used only if no `settings` specified.
362
- `settings_overrides`: A dictionary containing program-specific overrides
363
of component settings.
364
- `config_section`: Name of configuration file section for application.
365
Used only if no `settings` or `settings_spec` specified.
366
- `enable_exit`: Boolean; enable exit status at end of processing?
385
Parameters: see `publish_programmatically`.
368
pub = Publisher(reader, parser, writer, settings=settings,
369
source_class=io.StringInput,
370
destination_class=io.StringOutput)
371
pub.set_components(reader_name, parser_name, writer_name)
373
settings = pub.get_settings(settings_spec=settings_spec,
374
config_section=config_section)
375
if settings_overrides:
376
settings._update(settings_overrides, 'loose')
377
pub.set_source(source, source_path)
378
pub.set_destination(destination_path=destination_path)
379
return pub.publish(enable_exit=enable_exit)
387
output, pub = publish_programmatically(
388
source_class=io.StringInput, source=source, source_path=source_path,
389
destination_class=io.StringOutput,
390
destination=None, destination_path=destination_path,
391
reader=reader, reader_name=reader_name,
392
parser=parser, parser_name=parser_name,
393
writer=writer, writer_name=writer_name,
394
settings=settings, settings_spec=settings_spec,
395
settings_overrides=settings_overrides,
396
config_section=config_section,
397
enable_exit_status=enable_exit_status)
381
def publish_parts(source, source_path=None, destination_path=None,
400
def publish_parts(source, source_path=None, source_class=io.StringInput,
401
destination_path=None,
382
402
reader=None, reader_name='standalone',
383
403
parser=None, parser_name='restructuredtext',
384
404
writer=None, writer_name='pseudoxml',
385
405
settings=None, settings_spec=None,
386
406
settings_overrides=None, config_section=None,
407
enable_exit_status=None):
389
409
Set up & run a `Publisher`, and return a dictionary of document parts.
390
410
Dictionary keys are the names of parts, and values are Unicode strings;
391
411
encoding is up to the client. For programmatic use with string I/O.
393
For encoded string input, be sure to set the "input_encoding" setting to
394
the desired encoding. Set it to "unicode" for unencoded Unicode string
413
For encoded string input, be sure to set the 'input_encoding' setting to
414
the desired encoding. Set it to 'unicode' for unencoded Unicode string
395
415
input. Here's how::
397
publish_string(..., settings_overrides={'input_encoding': 'unicode'})
401
- `source`: An input string; required. This can be an encoded 8-bit
402
string (set the "input_encoding" setting to the correct encoding) or a
403
Unicode string (set the "input_encoding" setting to "unicode").
404
- `source_path`: Path to the file or object that produced `source`;
405
optional. Only used for diagnostic output.
406
- `destination_path`: Path to the file or object which will receive the
407
output; optional. Used for determining relative paths (stylesheets,
409
- `reader`: A `docutils.readers.Reader` object.
410
- `reader_name`: Name or alias of the Reader class to be instantiated if
411
no `reader` supplied.
412
- `parser`: A `docutils.parsers.Parser` object.
413
- `parser_name`: Name or alias of the Parser class to be instantiated if
414
no `parser` supplied.
415
- `writer`: A `docutils.writers.Writer` object.
416
- `writer_name`: Name or alias of the Writer class to be instantiated if
417
no `writer` supplied.
418
- `settings`: Runtime settings object.
419
- `settings_spec`: Extra settings specification; a `docutils.SettingsSpec`
420
subclass. Used only if no `settings` specified.
421
- `settings_overrides`: A dictionary containing program-specific overrides
422
of component settings.
423
- `config_section`: Name of configuration file section for application.
424
Used only if no `settings` or `settings_spec` specified.
425
- `enable_exit`: Boolean; enable exit status at end of processing?
417
publish_parts(..., settings_overrides={'input_encoding': 'unicode'})
419
Parameters: see `publish_programmatically`.
427
pub = Publisher(reader, parser, writer, settings=settings,
421
output, pub = publish_programmatically(
422
source=source, source_path=source_path, source_class=source_class,
423
destination_class=io.StringOutput,
424
destination=None, destination_path=destination_path,
425
reader=reader, reader_name=reader_name,
426
parser=parser, parser_name=parser_name,
427
writer=writer, writer_name=writer_name,
428
settings=settings, settings_spec=settings_spec,
429
settings_overrides=settings_overrides,
430
config_section=config_section,
431
enable_exit_status=enable_exit_status)
432
return pub.writer.parts
434
def publish_doctree(source, source_path=None,
428
435
source_class=io.StringInput,
436
reader=None, reader_name='standalone',
437
parser=None, parser_name='restructuredtext',
438
settings=None, settings_spec=None,
439
settings_overrides=None, config_section=None,
440
enable_exit_status=None):
442
Set up & run a `Publisher` for programmatic use with string I/O.
443
Return the document tree.
445
For encoded string input, be sure to set the 'input_encoding' setting to
446
the desired encoding. Set it to 'unicode' for unencoded Unicode string
447
input. Here's one way::
449
publish_doctree(..., settings_overrides={'input_encoding': 'unicode'})
451
Parameters: see `publish_programmatically`.
453
pub = Publisher(reader=reader, parser=parser, writer=None,
455
source_class=source_class,
429
456
destination_class=io.NullOutput)
457
pub.set_components(reader_name, parser_name, 'null')
458
pub.process_programmatic_settings(
459
settings_spec, settings_overrides, config_section)
460
pub.set_source(source, source_path)
461
pub.set_destination(None, None)
462
output = pub.publish(enable_exit_status=enable_exit_status)
465
def publish_from_doctree(document, destination_path=None,
466
writer=None, writer_name='pseudoxml',
467
settings=None, settings_spec=None,
468
settings_overrides=None, config_section=None,
469
enable_exit_status=None):
471
Set up & run a `Publisher` to render from an existing document tree data
472
structure, for programmatic use with string I/O. Return a pair of encoded
473
string output and document parts.
475
Note that document.settings is overridden; if you want to use the settings
476
of the original `document`, pass settings=document.settings.
478
Also, new document.transformer and document.reporter objects are
481
For encoded string output, be sure to set the 'output_encoding' setting to
482
the desired encoding. Set it to 'unicode' for unencoded Unicode string
483
output. Here's one way::
485
publish_from_doctree(
486
..., settings_overrides={'output_encoding': 'unicode'})
488
Parameters: `document` is a `docutils.nodes.document` object, an existing
491
Other parameters: see `publish_programmatically`.
493
reader = docutils.readers.doctree.Reader(parser_name='null')
494
pub = Publisher(reader, None, writer,
495
source=io.DocTreeInput(document),
496
destination_class=io.StringOutput, settings=settings)
497
if not writer and writer_name:
498
pub.set_writer(writer_name)
499
pub.process_programmatic_settings(
500
settings_spec, settings_overrides, config_section)
501
pub.set_destination(None, destination_path)
502
return pub.publish(enable_exit_status=enable_exit_status)
504
def publish_programmatically(source_class, source, source_path,
505
destination_class, destination, destination_path,
509
settings, settings_spec,
510
settings_overrides, config_section,
513
Set up & run a `Publisher` for custom programmatic use. Return the
514
encoded string output and the Publisher object.
516
Applications should not need to call this function directly. If it does
517
seem to be necessary to call this function directly, please write to the
518
Docutils-develop mailing list
519
<http://docutils.sf.net/docs/user/mailing-lists.html#docutils-develop>.
523
* `source_class` **required**: The class for dynamically created source
524
objects. Typically `io.FileInput` or `io.StringInput`.
526
* `source`: Type depends on `source_class`:
528
- If `source_class` is `io.FileInput`: Either a file-like object
529
(must have 'read' and 'close' methods), or ``None``
530
(`source_path` is opened). If neither `source` nor
531
`source_path` are supplied, `sys.stdin` is used.
533
- If `source_class` is `io.StringInput` **required**: The input
534
string, either an encoded 8-bit string (set the
535
'input_encoding' setting to the correct encoding) or a Unicode
536
string (set the 'input_encoding' setting to 'unicode').
538
* `source_path`: Type depends on `source_class`:
540
- `io.FileInput`: Path to the input file, opened if no `source`
543
- `io.StringInput`: Optional. Path to the file or object that produced
544
`source`. Only used for diagnostic output.
546
* `destination_class` **required**: The class for dynamically created
547
destination objects. Typically `io.FileOutput` or `io.StringOutput`.
549
* `destination`: Type depends on `destination_class`:
551
- `io.FileOutput`: Either a file-like object (must have 'write' and
552
'close' methods), or ``None`` (`destination_path` is opened). If
553
neither `destination` nor `destination_path` are supplied,
554
`sys.stdout` is used.
556
- `io.StringOutput`: Not used; pass ``None``.
558
* `destination_path`: Type depends on `destination_class`:
560
- `io.FileOutput`: Path to the output file. Opened if no `destination`
563
- `io.StringOutput`: Path to the file or object which will receive the
564
output; optional. Used for determining relative paths (stylesheets,
567
* `reader`: A `docutils.readers.Reader` object.
569
* `reader_name`: Name or alias of the Reader class to be instantiated if
570
no `reader` supplied.
572
* `parser`: A `docutils.parsers.Parser` object.
574
* `parser_name`: Name or alias of the Parser class to be instantiated if
575
no `parser` supplied.
577
* `writer`: A `docutils.writers.Writer` object.
579
* `writer_name`: Name or alias of the Writer class to be instantiated if
580
no `writer` supplied.
582
* `settings`: A runtime settings (`docutils.frontend.Values`) object, for
583
dotted-attribute access to runtime settings. It's the end result of the
584
`SettingsSpec`, config file, and option processing. If `settings` is
585
passed, it's assumed to be complete and no further setting/config/option
588
* `settings_spec`: A `docutils.SettingsSpec` subclass or object. Provides
589
extra application-specific settings definitions independently of
590
components. In other words, the application becomes a component, and
591
its settings data is processed along with that of the other components.
592
Used only if no `settings` specified.
594
* `settings_overrides`: A dictionary containing application-specific
595
settings defaults that override the defaults of other components.
596
Used only if no `settings` specified.
598
* `config_section`: A string, the name of the configuration file section
599
for this application. Overrides the ``config_section`` attribute
600
defined by `settings_spec`. Used only if no `settings` specified.
602
* `enable_exit_status`: Boolean; enable exit status at end of processing?
604
pub = Publisher(reader, parser, writer, settings=settings,
605
source_class=source_class,
606
destination_class=destination_class)
430
607
pub.set_components(reader_name, parser_name, writer_name)
432
settings = pub.get_settings(settings_spec=settings_spec,
433
config_section=config_section)
434
if settings_overrides:
435
settings._update(settings_overrides, 'loose')
608
pub.process_programmatic_settings(
609
settings_spec, settings_overrides, config_section)
436
610
pub.set_source(source, source_path)
437
pub.set_destination(destination_path=destination_path)
438
pub.publish(enable_exit=enable_exit)
439
return pub.writer.parts
611
pub.set_destination(destination, destination_path)
612
output = pub.publish(enable_exit_status=enable_exit_status)