Debian Codegraph @

docutils/transforms/components.py

raw symbols SCIP
1 # $Id: components.py 10136 2025-05-20 15:48:27Z milde $
2 # Author: David Goodger <[email protected]>
3 # Copyright: This module has been placed in the public domain.
4
5 """
6 Docutils component-related transforms.
7 """
8
9 from __future__ import annotations
10
11 __docformat__ = 'reStructuredText'
12
13 from docutils.transforms import Transform
14
15
16 class Filter(Transform):
17
18 """
19 Include or exclude elements which depend on a specific Docutils component.
20
21 For use with `nodes.pending` elements. A "pending" element's dictionary
22 attribute ``details`` must contain the keys "component" and "format". The
23 value of ``details['component']`` must match the type name of the
24 component the elements depend on (e.g. "writer"). The value of
25 ``details['format']`` is the name of a specific format or context of that
26 component (e.g. "html"). If the matching Docutils component supports that
27 format or context, the "pending" element is replaced by the contents of
28 ``details['nodes']`` (a list of nodes); otherwise, the "pending" element
29 is removed.
30
31 For example, up to version 0.17, the reStructuredText "meta"
32 directive created a "pending" element containing a "meta" element
33 (in ``pending.details['nodes']``).
34 Only writers (``pending.details['component'] == 'writer'``)
35 supporting the "html", "latex", or "odf" formats
36 (``pending.details['format'] == 'html,latex,odf'``) included the
37 "meta" element; it was deleted from the output of all other writers.
38
39 This transform is no longer used by Docutils, it may be removed in future.
40 """
41 # TODO: clean up or keep this for 3rd party (or possible future) use?
42 # (GM 2021-05-18)
43
44 default_priority = 780
45
46 def apply(self) -> None:
47 pending = self.startnode
48 component_type = pending.details['component'] # 'reader' or 'writer'
49 formats = (pending.details['format']).split(',')
50 component = self.document.transformer.components[component_type]
51 for format in formats:
52 if component.supports(format):
53 pending.replace_self(pending.details['nodes'])
54 break
55 else:
56 pending.parent.remove(pending)