| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Auxiliary transforms mainly to be used by Writer components. |
| 7 |
|
| 8 |
This module is called "writer_aux" because otherwise there would be |
| 9 |
conflicting imports like this one:: |
| 10 |
|
| 11 |
from docutils import writers |
| 12 |
from docutils.transforms import writers |
| 13 |
""" |
| 14 |
from __future__ import annotations |
| 15 |
|
| 16 |
__docformat__ = 'reStructuredText' |
| 17 |
|
| 18 |
from docutils import nodes, languages |
| 19 |
from docutils.transforms import Transform |
| 20 |
|
| 21 |
|
| 22 |
class Admonitions(Transform): |
| 23 |
|
| 24 |
""" |
| 25 |
Transform specific admonitions, like this: |
| 26 |
|
| 27 |
<note> |
| 28 |
<paragraph> |
| 29 |
Note contents ... |
| 30 |
|
| 31 |
into generic admonitions, like this:: |
| 32 |
|
| 33 |
<admonition classes="note"> |
| 34 |
<title> |
| 35 |
Note |
| 36 |
<paragraph> |
| 37 |
Note contents ... |
| 38 |
|
| 39 |
The admonition title is localized. |
| 40 |
""" |
| 41 |
|
| 42 |
default_priority = 920 |
| 43 |
|
| 44 |
def apply(self) -> None: |
| 45 |
language = languages.get_language(self.document.settings.language_code, |
| 46 |
self.document.reporter) |
| 47 |
for node in self.document.findall(nodes.Admonition): |
| 48 |
node_name = node.__class__.__name__ |
| 49 |
|
| 50 |
node['classes'].append(node_name) |
| 51 |
if not isinstance(node, nodes.admonition): |
| 52 |
|
| 53 |
admonition = nodes.admonition(node.rawsource, *node.children, |
| 54 |
**node.attributes) |
| 55 |
title = nodes.title('', language.labels[node_name]) |
| 56 |
admonition.insert(0, title) |
| 57 |
node.replace_self(admonition) |