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