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