core.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. # $Id: core.py 9089 2022-06-22 08:51:16Z milde $
  2. # Author: David Goodger <goodger@python.org>
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. Calling the ``publish_*`` convenience functions (or instantiating a
  6. `Publisher` object) with component names will result in default
  7. behavior. For custom behavior (setting component options), create
  8. custom component objects first, and pass *them* to
  9. ``publish_*``/`Publisher`. See `The Docutils Publisher`_.
  10. .. _The Docutils Publisher:
  11. https://docutils.sourceforge.io/docs/api/publisher.html
  12. """
  13. __docformat__ = 'reStructuredText'
  14. import pprint
  15. import os
  16. import sys
  17. import warnings
  18. from docutils import (__version__, __version_details__, SettingsSpec,
  19. io, utils, readers, writers)
  20. from docutils.frontend import OptionParser
  21. from docutils.readers import doctree
  22. class Publisher:
  23. """
  24. A facade encapsulating the high-level logic of a Docutils system.
  25. """
  26. def __init__(self, reader=None, parser=None, writer=None,
  27. source=None, source_class=io.FileInput,
  28. destination=None, destination_class=io.FileOutput,
  29. settings=None):
  30. """
  31. Initial setup. If any of `reader`, `parser`, or `writer` are not
  32. specified, the corresponding ``set_...`` method should be called with
  33. a component name (`set_reader` sets the parser as well).
  34. """
  35. self.document = None
  36. """The document tree (`docutils.nodes` objects)."""
  37. self.reader = reader
  38. """A `docutils.readers.Reader` instance."""
  39. self.parser = parser
  40. """A `docutils.parsers.Parser` instance."""
  41. self.writer = writer
  42. """A `docutils.writers.Writer` instance."""
  43. for component in 'reader', 'parser', 'writer':
  44. assert not isinstance(getattr(self, component), str), (
  45. 'passed string "%s" as "%s" parameter; pass an instance, '
  46. 'or use the "%s_name" parameter instead (in '
  47. 'docutils.core.publish_* convenience functions).'
  48. % (getattr(self, component), component, component))
  49. self.source = source
  50. """The source of input data, a `docutils.io.Input` instance."""
  51. self.source_class = source_class
  52. """The class for dynamically created source objects."""
  53. self.destination = destination
  54. """The destination for docutils output, a `docutils.io.Output`
  55. instance."""
  56. self.destination_class = destination_class
  57. """The class for dynamically created destination objects."""
  58. self.settings = settings
  59. """An object containing Docutils settings as instance attributes.
  60. Set by `self.process_command_line()` or `self.get_settings()`."""
  61. self._stderr = io.ErrorOutput()
  62. def set_reader(self, reader_name, parser, parser_name):
  63. """Set `self.reader` by name."""
  64. reader_class = readers.get_reader_class(reader_name)
  65. self.reader = reader_class(parser, parser_name)
  66. self.parser = self.reader.parser
  67. def set_writer(self, writer_name):
  68. """Set `self.writer` by name."""
  69. writer_class = writers.get_writer_class(writer_name)
  70. self.writer = writer_class()
  71. def set_components(self, reader_name, parser_name, writer_name):
  72. if self.reader is None:
  73. self.set_reader(reader_name, self.parser, parser_name)
  74. if self.parser is None:
  75. if self.reader.parser is None:
  76. self.reader.set_parser(parser_name)
  77. self.parser = self.reader.parser
  78. if self.writer is None:
  79. self.set_writer(writer_name)
  80. def setup_option_parser(self, usage=None, description=None,
  81. settings_spec=None, config_section=None,
  82. **defaults):
  83. warnings.warn('Publisher.setup_option_parser is deprecated, '
  84. 'and will be removed in Docutils 0.21.',
  85. DeprecationWarning, stacklevel=2)
  86. if config_section:
  87. if not settings_spec:
  88. settings_spec = SettingsSpec()
  89. settings_spec.config_section = config_section
  90. parts = config_section.split()
  91. if len(parts) > 1 and parts[-1] == 'application':
  92. settings_spec.config_section_dependencies = ['applications']
  93. # @@@ Add self.source & self.destination to components in future?
  94. return OptionParser(
  95. components=(self.parser, self.reader, self.writer, settings_spec),
  96. defaults=defaults, read_config_files=True,
  97. usage=usage, description=description)
  98. def _setup_settings_parser(self, *args, **kwargs):
  99. # Provisional: will change (docutils.frontend.OptionParser will
  100. # be replaced by a parser based on arparse.ArgumentParser)
  101. # and may be removed later.
  102. with warnings.catch_warnings():
  103. warnings.filterwarnings('ignore', category=DeprecationWarning)
  104. return self.setup_option_parser(*args, **kwargs)
  105. def get_settings(self, usage=None, description=None,
  106. settings_spec=None, config_section=None, **defaults):
  107. """
  108. Return settings from components and config files.
  109. Please set components first (`self.set_reader` & `self.set_writer`).
  110. Use keyword arguments to override component defaults
  111. (before updating from configuration files).
  112. Calling this function also sets `self.settings` which makes
  113. `self.publish()` skip parsing command line options.
  114. """
  115. option_parser = self._setup_settings_parser(
  116. usage, description, settings_spec, config_section, **defaults)
  117. self.settings = option_parser.get_default_values()
  118. return self.settings
  119. def process_programmatic_settings(self, settings_spec,
  120. settings_overrides,
  121. config_section):
  122. if self.settings is None:
  123. defaults = settings_overrides.copy() if settings_overrides else {}
  124. # Propagate exceptions by default when used programmatically:
  125. defaults.setdefault('traceback', True)
  126. self.get_settings(settings_spec=settings_spec,
  127. config_section=config_section,
  128. **defaults)
  129. def process_command_line(self, argv=None, usage=None, description=None,
  130. settings_spec=None, config_section=None,
  131. **defaults):
  132. """
  133. Parse command line arguments and set ``self.settings``.
  134. Pass an empty sequence to `argv` to avoid reading `sys.argv`
  135. (the default behaviour).
  136. Set components first (`self.set_reader` & `self.set_writer`).
  137. """
  138. option_parser = self._setup_settings_parser(
  139. usage, description, settings_spec, config_section, **defaults)
  140. if argv is None:
  141. argv = sys.argv[1:]
  142. self.settings = option_parser.parse_args(argv)
  143. def set_io(self, source_path=None, destination_path=None):
  144. if self.source is None:
  145. self.set_source(source_path=source_path)
  146. if self.destination is None:
  147. self.set_destination(destination_path=destination_path)
  148. def set_source(self, source=None, source_path=None):
  149. if source_path is None:
  150. source_path = self.settings._source
  151. else:
  152. self.settings._source = source_path
  153. self.source = self.source_class(
  154. source=source, source_path=source_path,
  155. encoding=self.settings.input_encoding,
  156. error_handler=self.settings.input_encoding_error_handler)
  157. def set_destination(self, destination=None, destination_path=None):
  158. if destination_path is None:
  159. destination_path = self.settings._destination
  160. else:
  161. self.settings._destination = destination_path
  162. self.destination = self.destination_class(
  163. destination=destination, destination_path=destination_path,
  164. encoding=self.settings.output_encoding,
  165. error_handler=self.settings.output_encoding_error_handler)
  166. def apply_transforms(self):
  167. self.document.transformer.populate_from_components(
  168. (self.source, self.reader, self.reader.parser, self.writer,
  169. self.destination))
  170. self.document.transformer.apply_transforms()
  171. def publish(self, argv=None, usage=None, description=None,
  172. settings_spec=None, settings_overrides=None,
  173. config_section=None, enable_exit_status=False):
  174. """
  175. Process command line options and arguments (if `self.settings` not
  176. already set), run `self.reader` and then `self.writer`. Return
  177. `self.writer`'s output.
  178. """
  179. exit = None
  180. try:
  181. if self.settings is None:
  182. self.process_command_line(
  183. argv, usage, description, settings_spec, config_section,
  184. **(settings_overrides or {}))
  185. self.set_io()
  186. self.prompt()
  187. self.document = self.reader.read(self.source, self.parser,
  188. self.settings)
  189. self.apply_transforms()
  190. output = self.writer.write(self.document, self.destination)
  191. self.writer.assemble_parts()
  192. except SystemExit as error:
  193. exit = 1
  194. exit_status = error.code
  195. except Exception as error:
  196. if not self.settings: # exception too early to report nicely
  197. raise
  198. if self.settings.traceback: # Propagate exceptions?
  199. self.debugging_dumps()
  200. raise
  201. self.report_Exception(error)
  202. exit = True
  203. exit_status = 1
  204. self.debugging_dumps()
  205. if (enable_exit_status and self.document
  206. and (self.document.reporter.max_level
  207. >= self.settings.exit_status_level)):
  208. sys.exit(self.document.reporter.max_level + 10)
  209. elif exit:
  210. sys.exit(exit_status)
  211. return output
  212. def debugging_dumps(self):
  213. if not self.document:
  214. return
  215. if self.settings.dump_settings:
  216. print('\n::: Runtime settings:', file=self._stderr)
  217. print(pprint.pformat(self.settings.__dict__), file=self._stderr)
  218. if self.settings.dump_internals:
  219. print('\n::: Document internals:', file=self._stderr)
  220. print(pprint.pformat(self.document.__dict__), file=self._stderr)
  221. if self.settings.dump_transforms:
  222. print('\n::: Transforms applied:', file=self._stderr)
  223. print(' (priority, transform class, pending node details, '
  224. 'keyword args)', file=self._stderr)
  225. print(pprint.pformat(
  226. [(priority, '%s.%s' % (xclass.__module__, xclass.__name__),
  227. pending and pending.details, kwargs)
  228. for priority, xclass, pending, kwargs
  229. in self.document.transformer.applied]), file=self._stderr)
  230. if self.settings.dump_pseudo_xml:
  231. print('\n::: Pseudo-XML:', file=self._stderr)
  232. print(self.document.pformat().encode(
  233. 'raw_unicode_escape'), file=self._stderr)
  234. def prompt(self):
  235. """Print info and prompt when waiting for input from a terminal."""
  236. try:
  237. if not (self.source.isatty() and self._stderr.isatty()):
  238. return
  239. except AttributeError:
  240. return
  241. eot_key = 'Ctrl+Z' if os.name == 'nt' else 'Ctrl+D on an empty line'
  242. in_format = ''
  243. out_format = 'useful formats'
  244. try:
  245. in_format = self.parser.supported[0]
  246. out_format = self.writer.supported[0]
  247. except (AttributeError, IndexError):
  248. pass
  249. print(f'Docutils {__version__} <https://docutils.sourceforge.io>\n'
  250. f'converting "{in_format}" into "{out_format}".\n'
  251. f'Call with option "--help" for more info.\n'
  252. f'.. Waiting for source text (finish with {eot_key}):',
  253. file=self._stderr)
  254. def report_Exception(self, error):
  255. if isinstance(error, utils.SystemMessage):
  256. self.report_SystemMessage(error)
  257. elif isinstance(error, UnicodeEncodeError):
  258. self.report_UnicodeError(error)
  259. elif isinstance(error, io.InputError):
  260. self._stderr.write('Unable to open source file for reading:\n'
  261. ' %s\n' % io.error_string(error))
  262. elif isinstance(error, io.OutputError):
  263. self._stderr.write(
  264. 'Unable to open destination file for writing:\n'
  265. ' %s\n' % io.error_string(error))
  266. else:
  267. print('%s' % io.error_string(error), file=self._stderr)
  268. print(f"""\
  269. Exiting due to error. Use "--traceback" to diagnose.
  270. Please report errors to <docutils-users@lists.sourceforge.net>.
  271. Include "--traceback" output, Docutils version ({__version__}\
  272. {f' [{__version_details__}]' if __version_details__ else ''}),
  273. Python version ({sys.version.split()[0]}), your OS type & version, \
  274. and the command line used.""", file=self._stderr)
  275. def report_SystemMessage(self, error):
  276. print('Exiting due to level-%s (%s) system message.' % (
  277. error.level, utils.Reporter.levels[error.level]),
  278. file=self._stderr)
  279. def report_UnicodeError(self, error):
  280. data = error.object[error.start:error.end]
  281. self._stderr.write(
  282. '%s\n'
  283. '\n'
  284. 'The specified output encoding (%s) cannot\n'
  285. 'handle all of the output.\n'
  286. 'Try setting "--output-encoding-error-handler" to\n'
  287. '\n'
  288. '* "xmlcharrefreplace" (for HTML & XML output);\n'
  289. ' the output will contain "%s" and should be usable.\n'
  290. '* "backslashreplace" (for other output formats);\n'
  291. ' look for "%s" in the output.\n'
  292. '* "replace"; look for "?" in the output.\n'
  293. '\n'
  294. '"--output-encoding-error-handler" is currently set to "%s".\n'
  295. '\n'
  296. 'Exiting due to error. Use "--traceback" to diagnose.\n'
  297. 'If the advice above doesn\'t eliminate the error,\n'
  298. 'please report it to <docutils-users@lists.sourceforge.net>.\n'
  299. 'Include "--traceback" output, Docutils version (%s),\n'
  300. 'Python version (%s), your OS type & version, and the\n'
  301. 'command line used.\n'
  302. % (io.error_string(error),
  303. self.settings.output_encoding,
  304. data.encode('ascii', 'xmlcharrefreplace'),
  305. data.encode('ascii', 'backslashreplace'),
  306. self.settings.output_encoding_error_handler,
  307. __version__, sys.version.split()[0]))
  308. default_usage = '%prog [options] [<source> [<destination>]]'
  309. default_description = (
  310. 'Reads from <source> (default is stdin) '
  311. 'and writes to <destination> (default is stdout). '
  312. 'See https://docutils.sourceforge.io/docs/user/config.html '
  313. 'for a detailed settings reference.')
  314. # TODO: or not to do? cf. https://clig.dev/#help
  315. #
  316. # Display output on success, but keep it brief.
  317. #
  318. # Provide a -q option to suppress all non-essential output.
  319. # Chain several args as input and use --output or redirection for output:
  320. # argparser.add_argument('source', nargs='+')
  321. #
  322. def publish_cmdline(reader=None, reader_name='standalone',
  323. parser=None, parser_name='restructuredtext',
  324. writer=None, writer_name='pseudoxml',
  325. settings=None, settings_spec=None,
  326. settings_overrides=None, config_section=None,
  327. enable_exit_status=True, argv=None,
  328. usage=default_usage, description=default_description):
  329. """
  330. Set up & run a `Publisher` for command-line-based file I/O (input and
  331. output file paths taken automatically from the command line). Return the
  332. encoded string output also.
  333. Parameters: see `publish_programmatically` for the remainder.
  334. - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``.
  335. - `usage`: Usage string, output if there's a problem parsing the command
  336. line.
  337. - `description`: Program description, output for the "--help" option
  338. (along with command-line option descriptions).
  339. """
  340. pub = Publisher(reader, parser, writer, settings=settings)
  341. pub.set_components(reader_name, parser_name, writer_name)
  342. output = pub.publish(
  343. argv, usage, description, settings_spec, settings_overrides,
  344. config_section=config_section, enable_exit_status=enable_exit_status)
  345. return output
  346. def publish_file(source=None, source_path=None,
  347. destination=None, destination_path=None,
  348. reader=None, reader_name='standalone',
  349. parser=None, parser_name='restructuredtext',
  350. writer=None, writer_name='pseudoxml',
  351. settings=None, settings_spec=None, settings_overrides=None,
  352. config_section=None, enable_exit_status=False):
  353. """
  354. Set up & run a `Publisher` for programmatic use with file-like I/O.
  355. Return the encoded string output also.
  356. Parameters: see `publish_programmatically`.
  357. """
  358. output, pub = publish_programmatically(
  359. source_class=io.FileInput, source=source, source_path=source_path,
  360. destination_class=io.FileOutput,
  361. destination=destination, destination_path=destination_path,
  362. reader=reader, reader_name=reader_name,
  363. parser=parser, parser_name=parser_name,
  364. writer=writer, writer_name=writer_name,
  365. settings=settings, settings_spec=settings_spec,
  366. settings_overrides=settings_overrides,
  367. config_section=config_section,
  368. enable_exit_status=enable_exit_status)
  369. return output
  370. def publish_string(source, source_path=None, destination_path=None,
  371. reader=None, reader_name='standalone',
  372. parser=None, parser_name='restructuredtext',
  373. writer=None, writer_name='pseudoxml',
  374. settings=None, settings_spec=None,
  375. settings_overrides=None, config_section=None,
  376. enable_exit_status=False):
  377. """
  378. Set up & run a `Publisher` for programmatic use with string I/O. Return
  379. the encoded string or Unicode string output.
  380. For encoded string output, be sure to set the 'output_encoding' setting to
  381. the desired encoding. Set it to 'unicode' for unencoded Unicode string
  382. output. Here's one way::
  383. publish_string(..., settings_overrides={'output_encoding': 'unicode'})
  384. Similarly for Unicode string input (`source`)::
  385. publish_string(..., settings_overrides={'input_encoding': 'unicode'})
  386. Parameters: see `publish_programmatically`.
  387. """
  388. output, pub = publish_programmatically(
  389. source_class=io.StringInput, source=source, source_path=source_path,
  390. destination_class=io.StringOutput,
  391. destination=None, destination_path=destination_path,
  392. reader=reader, reader_name=reader_name,
  393. parser=parser, parser_name=parser_name,
  394. writer=writer, writer_name=writer_name,
  395. settings=settings, settings_spec=settings_spec,
  396. settings_overrides=settings_overrides,
  397. config_section=config_section,
  398. enable_exit_status=enable_exit_status)
  399. return output
  400. def publish_parts(source, source_path=None, source_class=io.StringInput,
  401. destination_path=None,
  402. reader=None, reader_name='standalone',
  403. parser=None, parser_name='restructuredtext',
  404. writer=None, writer_name='pseudoxml',
  405. settings=None, settings_spec=None,
  406. settings_overrides=None, config_section=None,
  407. enable_exit_status=False):
  408. """
  409. Set up & run a `Publisher`, and return a dictionary of document parts.
  410. Dictionary keys are the names of parts, and values are Unicode strings;
  411. encoding is up to the client. For programmatic use with string I/O.
  412. For encoded string input, be sure to set the 'input_encoding' setting to
  413. the desired encoding. Set it to 'unicode' for unencoded Unicode string
  414. input. Here's how::
  415. publish_parts(..., settings_overrides={'input_encoding': 'unicode'})
  416. Parameters: see `publish_programmatically`.
  417. """
  418. output, pub = publish_programmatically(
  419. source=source, source_path=source_path, source_class=source_class,
  420. destination_class=io.StringOutput,
  421. destination=None, destination_path=destination_path,
  422. reader=reader, reader_name=reader_name,
  423. parser=parser, parser_name=parser_name,
  424. writer=writer, writer_name=writer_name,
  425. settings=settings, settings_spec=settings_spec,
  426. settings_overrides=settings_overrides,
  427. config_section=config_section,
  428. enable_exit_status=enable_exit_status)
  429. return pub.writer.parts
  430. def publish_doctree(source, source_path=None,
  431. source_class=io.StringInput,
  432. reader=None, reader_name='standalone',
  433. parser=None, parser_name='restructuredtext',
  434. settings=None, settings_spec=None,
  435. settings_overrides=None, config_section=None,
  436. enable_exit_status=False):
  437. """
  438. Set up & run a `Publisher` for programmatic use with string I/O.
  439. Return the document tree.
  440. For encoded string input, be sure to set the 'input_encoding' setting to
  441. the desired encoding. Set it to 'unicode' for unencoded Unicode string
  442. input. Here's one way::
  443. publish_doctree(..., settings_overrides={'input_encoding': 'unicode'})
  444. Parameters: see `publish_programmatically`.
  445. """
  446. pub = Publisher(reader=reader, parser=parser, writer=None,
  447. settings=settings,
  448. source_class=source_class,
  449. destination_class=io.NullOutput)
  450. pub.set_components(reader_name, parser_name, 'null')
  451. pub.process_programmatic_settings(
  452. settings_spec, settings_overrides, config_section)
  453. pub.set_source(source, source_path)
  454. pub.set_destination(None, None)
  455. pub.publish(enable_exit_status=enable_exit_status)
  456. return pub.document
  457. def publish_from_doctree(document, destination_path=None,
  458. writer=None, writer_name='pseudoxml',
  459. settings=None, settings_spec=None,
  460. settings_overrides=None, config_section=None,
  461. enable_exit_status=False):
  462. """
  463. Set up & run a `Publisher` to render from an existing document
  464. tree data structure, for programmatic use with string I/O. Return
  465. the encoded string output.
  466. Note that document.settings is overridden; if you want to use the settings
  467. of the original `document`, pass settings=document.settings.
  468. Also, new document.transformer and document.reporter objects are
  469. generated.
  470. For encoded string output, be sure to set the 'output_encoding' setting to
  471. the desired encoding. Set it to 'unicode' for unencoded Unicode string
  472. output. Here's one way::
  473. publish_from_doctree(
  474. ..., settings_overrides={'output_encoding': 'unicode'})
  475. Parameters: `document` is a `docutils.nodes.document` object, an existing
  476. document tree.
  477. Other parameters: see `publish_programmatically`.
  478. """
  479. reader = doctree.Reader(parser_name='null')
  480. pub = Publisher(reader, None, writer,
  481. source=io.DocTreeInput(document),
  482. destination_class=io.StringOutput, settings=settings)
  483. if not writer and writer_name:
  484. pub.set_writer(writer_name)
  485. pub.process_programmatic_settings(
  486. settings_spec, settings_overrides, config_section)
  487. pub.set_destination(None, destination_path)
  488. return pub.publish(enable_exit_status=enable_exit_status)
  489. def publish_cmdline_to_binary(reader=None, reader_name='standalone',
  490. parser=None, parser_name='restructuredtext',
  491. writer=None, writer_name='pseudoxml',
  492. settings=None,
  493. settings_spec=None,
  494. settings_overrides=None,
  495. config_section=None,
  496. enable_exit_status=True,
  497. argv=None,
  498. usage=default_usage,
  499. description=default_description,
  500. destination=None,
  501. destination_class=io.BinaryFileOutput):
  502. """
  503. Set up & run a `Publisher` for command-line-based file I/O (input and
  504. output file paths taken automatically from the command line). Return the
  505. encoded string output also.
  506. This is just like publish_cmdline, except that it uses
  507. io.BinaryFileOutput instead of io.FileOutput.
  508. Parameters: see `publish_programmatically` for the remainder.
  509. - `argv`: Command-line argument list to use instead of ``sys.argv[1:]``.
  510. - `usage`: Usage string, output if there's a problem parsing the command
  511. line.
  512. - `description`: Program description, output for the "--help" option
  513. (along with command-line option descriptions).
  514. """
  515. pub = Publisher(reader, parser, writer, settings=settings,
  516. destination_class=destination_class)
  517. pub.set_components(reader_name, parser_name, writer_name)
  518. output = pub.publish(
  519. argv, usage, description, settings_spec, settings_overrides,
  520. config_section=config_section, enable_exit_status=enable_exit_status)
  521. return output
  522. def publish_programmatically(source_class, source, source_path,
  523. destination_class, destination, destination_path,
  524. reader, reader_name,
  525. parser, parser_name,
  526. writer, writer_name,
  527. settings, settings_spec,
  528. settings_overrides, config_section,
  529. enable_exit_status):
  530. """
  531. Set up & run a `Publisher` for custom programmatic use. Return the
  532. encoded string output and the Publisher object.
  533. Applications should not need to call this function directly. If it does
  534. seem to be necessary to call this function directly, please write to the
  535. Docutils-develop mailing list
  536. <https://docutils.sourceforge.io/docs/user/mailing-lists.html#docutils-develop>.
  537. Parameters:
  538. * `source_class` **required**: The class for dynamically created source
  539. objects. Typically `io.FileInput` or `io.StringInput`.
  540. * `source`: Type depends on `source_class`:
  541. - If `source_class` is `io.FileInput`: Either a file-like object
  542. (must have 'read' and 'close' methods), or ``None``
  543. (`source_path` is opened). If neither `source` nor
  544. `source_path` are supplied, `sys.stdin` is used.
  545. - If `source_class` is `io.StringInput` **required**: The input
  546. string, either an encoded 8-bit string (set the
  547. 'input_encoding' setting to the correct encoding) or a Unicode
  548. string (set the 'input_encoding' setting to 'unicode').
  549. * `source_path`: Type depends on `source_class`:
  550. - `io.FileInput`: Path to the input file, opened if no `source`
  551. supplied.
  552. - `io.StringInput`: Optional. Path to the file or object that produced
  553. `source`. Only used for diagnostic output.
  554. * `destination_class` **required**: The class for dynamically created
  555. destination objects. Typically `io.FileOutput` or `io.StringOutput`.
  556. * `destination`: Type depends on `destination_class`:
  557. - `io.FileOutput`: Either a file-like object (must have 'write' and
  558. 'close' methods), or ``None`` (`destination_path` is opened). If
  559. neither `destination` nor `destination_path` are supplied,
  560. `sys.stdout` is used.
  561. - `io.StringOutput`: Not used; pass ``None``.
  562. * `destination_path`: Type depends on `destination_class`:
  563. - `io.FileOutput`: Path to the output file. Opened if no `destination`
  564. supplied.
  565. - `io.StringOutput`: Path to the file or object which will receive the
  566. output; optional. Used for determining relative paths (stylesheets,
  567. source links, etc.).
  568. * `reader`: A `docutils.readers.Reader` object.
  569. * `reader_name`: Name or alias of the Reader class to be instantiated if
  570. no `reader` supplied.
  571. * `parser`: A `docutils.parsers.Parser` object.
  572. * `parser_name`: Name or alias of the Parser class to be instantiated if
  573. no `parser` supplied.
  574. * `writer`: A `docutils.writers.Writer` object.
  575. * `writer_name`: Name or alias of the Writer class to be instantiated if
  576. no `writer` supplied.
  577. * `settings`: A runtime settings (`docutils.frontend.Values`) object, for
  578. dotted-attribute access to runtime settings. It's the end result of the
  579. `SettingsSpec`, config file, and option processing. If `settings` is
  580. passed, it's assumed to be complete and no further setting/config/option
  581. processing is done.
  582. * `settings_spec`: A `docutils.SettingsSpec` subclass or object. Provides
  583. extra application-specific settings definitions independently of
  584. components. In other words, the application becomes a component, and
  585. its settings data is processed along with that of the other components.
  586. Used only if no `settings` specified.
  587. * `settings_overrides`: A dictionary containing application-specific
  588. settings defaults that override the defaults of other components.
  589. Used only if no `settings` specified.
  590. * `config_section`: A string, the name of the configuration file section
  591. for this application. Overrides the ``config_section`` attribute
  592. defined by `settings_spec`. Used only if no `settings` specified.
  593. * `enable_exit_status`: Boolean; enable exit status at end of processing?
  594. """
  595. pub = Publisher(reader, parser, writer, settings=settings,
  596. source_class=source_class,
  597. destination_class=destination_class)
  598. pub.set_components(reader_name, parser_name, writer_name)
  599. pub.process_programmatic_settings(
  600. settings_spec, settings_overrides, config_section)
  601. pub.set_source(source, source_path)
  602. pub.set_destination(destination, destination_path)
  603. output = pub.publish(enable_exit_status=enable_exit_status)
  604. return output, pub