You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.9 KiB

4 years ago
  1. # $Id: examples.py 7320 2012-01-19 22:33:02Z milde $
  2. # Author: David Goodger <goodger@python.org>
  3. # Copyright: This module has been placed in the public domain.
  4. """
  5. This module contains practical examples of Docutils client code.
  6. Importing this module from client code is not recommended; its contents are
  7. subject to change in future Docutils releases. Instead, it is recommended
  8. that you copy and paste the parts you need into your own code, modifying as
  9. necessary.
  10. """
  11. from docutils import core, io
  12. def html_parts(input_string, source_path=None, destination_path=None,
  13. input_encoding='unicode', doctitle=True,
  14. initial_header_level=1):
  15. """
  16. Given an input string, returns a dictionary of HTML document parts.
  17. Dictionary keys are the names of parts, and values are Unicode strings;
  18. encoding is up to the client.
  19. Parameters:
  20. - `input_string`: A multi-line text string; required.
  21. - `source_path`: Path to the source file or object. Optional, but useful
  22. for diagnostic output (system messages).
  23. - `destination_path`: Path to the file or object which will receive the
  24. output; optional. Used for determining relative paths (stylesheets,
  25. source links, etc.).
  26. - `input_encoding`: The encoding of `input_string`. If it is an encoded
  27. 8-bit string, provide the correct encoding. If it is a Unicode string,
  28. use "unicode", the default.
  29. - `doctitle`: Disable the promotion of a lone top-level section title to
  30. document title (and subsequent section title to document subtitle
  31. promotion); enabled by default.
  32. - `initial_header_level`: The initial level for header elements (e.g. 1
  33. for "<h1>").
  34. """
  35. overrides = {'input_encoding': input_encoding,
  36. 'doctitle_xform': doctitle,
  37. 'initial_header_level': initial_header_level}
  38. parts = core.publish_parts(
  39. source=input_string, source_path=source_path,
  40. destination_path=destination_path,
  41. writer_name='html', settings_overrides=overrides)
  42. return parts
  43. def html_body(input_string, source_path=None, destination_path=None,
  44. input_encoding='unicode', output_encoding='unicode',
  45. doctitle=True, initial_header_level=1):
  46. """
  47. Given an input string, returns an HTML fragment as a string.
  48. The return value is the contents of the <body> element.
  49. Parameters (see `html_parts()` for the remainder):
  50. - `output_encoding`: The desired encoding of the output. If a Unicode
  51. string is desired, use the default value of "unicode" .
  52. """
  53. parts = html_parts(
  54. input_string=input_string, source_path=source_path,
  55. destination_path=destination_path,
  56. input_encoding=input_encoding, doctitle=doctitle,
  57. initial_header_level=initial_header_level)
  58. fragment = parts['html_body']
  59. if output_encoding != 'unicode':
  60. fragment = fragment.encode(output_encoding)
  61. return fragment
  62. def internals(input_string, source_path=None, destination_path=None,
  63. input_encoding='unicode', settings_overrides=None):
  64. """
  65. Return the document tree and publisher, for exploring Docutils internals.
  66. Parameters: see `html_parts()`.
  67. """
  68. if settings_overrides:
  69. overrides = settings_overrides.copy()
  70. else:
  71. overrides = {}
  72. overrides['input_encoding'] = input_encoding
  73. output, pub = core.publish_programmatically(
  74. source_class=io.StringInput, source=input_string,
  75. source_path=source_path,
  76. destination_class=io.NullOutput, destination=None,
  77. destination_path=destination_path,
  78. reader=None, reader_name='standalone',
  79. parser=None, parser_name='restructuredtext',
  80. writer=None, writer_name='null',
  81. settings=None, settings_spec=None, settings_overrides=overrides,
  82. config_section=None, enable_exit_status=None)
  83. return pub.writer.document, pub