| 1 |
|
| 2 |
|
| 3 |
|
| 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 __future__ import annotations |
| 15 |
|
| 16 |
|
| 17 |
from docutils import core, io |
| 18 |
|
| 19 |
TYPE_CHECKING = False |
| 20 |
if TYPE_CHECKING: |
| 21 |
from typing import Any, Literal |
| 22 |
|
| 23 |
from docutils import nodes |
| 24 |
from docutils.nodes import StrPath |
| 25 |
from docutils.core import Publisher |
| 26 |
|
| 27 |
|
| 28 |
def html_parts(input_string: str | bytes, |
| 29 |
source_path: StrPath | None = None, |
| 30 |
destination_path: StrPath | None = None, |
| 31 |
input_encoding: Literal['unicode'] | str = 'unicode', |
| 32 |
doctitle: bool = True, |
| 33 |
initial_header_level: int = 1, |
| 34 |
) -> dict[str, str]: |
| 35 |
""" |
| 36 |
Given an input string, returns a dictionary of HTML document parts. |
| 37 |
|
| 38 |
Dictionary keys are the names of parts, and values are Unicode strings; |
| 39 |
encoding is up to the client. |
| 40 |
|
| 41 |
Parameters: |
| 42 |
|
| 43 |
- `input_string`: A multi-line text string; required. |
| 44 |
- `source_path`: Path to the source file or object. Optional, but useful |
| 45 |
for diagnostic output (system messages). |
| 46 |
- `destination_path`: Path to the file or object which will receive the |
| 47 |
output; optional. Used for determining relative paths (stylesheets, |
| 48 |
source links, etc.). |
| 49 |
- `input_encoding`: The encoding of `input_string`. If it is an encoded |
| 50 |
8-bit string, provide the correct encoding. If it is a Unicode string, |
| 51 |
use "unicode", the default. |
| 52 |
- `doctitle`: Disable the promotion of a lone top-level section title to |
| 53 |
document title (and subsequent section title to document subtitle |
| 54 |
promotion); enabled by default. |
| 55 |
- `initial_header_level`: The initial level for header elements (e.g. 1 |
| 56 |
for "<h1>"). |
| 57 |
""" |
| 58 |
overrides = {'input_encoding': input_encoding, |
| 59 |
'doctitle_xform': doctitle, |
| 60 |
'initial_header_level': initial_header_level} |
| 61 |
parts = core.publish_parts( |
| 62 |
source=input_string, source_path=source_path, |
| 63 |
destination_path=destination_path, |
| 64 |
writer='html', settings_overrides=overrides) |
| 65 |
return parts |
| 66 |
|
| 67 |
|
| 68 |
def html_body(input_string: str | bytes, |
| 69 |
source_path: StrPath | None = None, |
| 70 |
destination_path: StrPath | None = None, |
| 71 |
input_encoding: Literal['unicode'] | str = 'unicode', |
| 72 |
output_encoding: Literal['unicode'] | str = 'unicode', |
| 73 |
doctitle: bool = True, |
| 74 |
initial_header_level: int = 1, |
| 75 |
) -> str | bytes: |
| 76 |
""" |
| 77 |
Given an input string, returns an HTML fragment as a string. |
| 78 |
|
| 79 |
The return value is the contents of the <body> element. |
| 80 |
|
| 81 |
Parameters (see `html_parts()` for the remainder): |
| 82 |
|
| 83 |
- `output_encoding`: The desired encoding of the output. If a Unicode |
| 84 |
string is desired, use the default value of "unicode" . |
| 85 |
""" |
| 86 |
parts = html_parts( |
| 87 |
input_string=input_string, source_path=source_path, |
| 88 |
destination_path=destination_path, |
| 89 |
input_encoding=input_encoding, doctitle=doctitle, |
| 90 |
initial_header_level=initial_header_level) |
| 91 |
fragment = parts['html_body'] |
| 92 |
if output_encoding != 'unicode': |
| 93 |
fragment = fragment.encode(output_encoding) |
| 94 |
return fragment |
| 95 |
|
| 96 |
|
| 97 |
def internals(source: str, |
| 98 |
source_path: StrPath | None = None, |
| 99 |
input_encoding: Literal['unicode'] | str = 'unicode', |
| 100 |
settings_overrides: dict[str, Any] | None = None, |
| 101 |
) -> tuple[nodes.document, Publisher]: |
| 102 |
""" |
| 103 |
Return the document tree and publisher, for exploring Docutils internals. |
| 104 |
|
| 105 |
Parameters: see `html_parts()`. |
| 106 |
""" |
| 107 |
if settings_overrides is None: |
| 108 |
settings_overrides = {} |
| 109 |
overrides = settings_overrides | {'input_encoding': input_encoding} |
| 110 |
|
| 111 |
publisher = core.Publisher('standalone', 'rst', 'null', |
| 112 |
source_class=io.StringInput, |
| 113 |
destination_class=io.NullOutput) |
| 114 |
publisher.process_programmatic_settings(settings_spec=None, |
| 115 |
settings_overrides=overrides, |
| 116 |
config_section=None) |
| 117 |
publisher.set_source(source, source_path) |
| 118 |
publisher.publish() |
| 119 |
return publisher.document, publisher |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == '__main__': |
| 123 |
print(internals('test')[0]) |