comparison env/lib/python3.7/site-packages/docutils/examples.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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 """
6 This module contains practical examples of Docutils client code.
7
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 """
13
14 from docutils import core, io
15
16
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.
22
23 Dictionary keys are the names of parts, and values are Unicode strings;
24 encoding is up to the client.
25
26 Parameters:
27
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
51
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.
57
58 The return value is the contents of the <body> element.
59
60 Parameters (see `html_parts()` for the remainder):
61
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
74
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.
79
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