Coverage for phml\utils\transform\transform.py: 84%
76 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 11:07 -0600
« prev ^ index » next coverage.py v6.5.0, created at 2022-12-08 11:07 -0600
1"""phml.utils.transform.transform
3Utility methods that revolve around transforming or manipulating the ast.
4"""
6from typing import Callable, Optional
8from phml.nodes import AST, All_Nodes, Element, Root
9from phml.utils.misc import heading_rank
10from phml.utils.travel import walk
11from phml.utils.validate.check import Test, check
13__all__ = [
14 "filter_nodes",
15 "remove_nodes",
16 "map_nodes",
17 "find_and_replace",
18 "shift_heading",
19 "replace_node",
20]
23def filter_nodes(
24 tree: Root | Element | AST,
25 condition: Test,
26 strict: bool = True,
27):
28 """Take a given tree and filter the nodes with the condition.
29 Only nodes passing the condition stay. If the parent node fails,
30 all children are moved up in scope. Depth first
32 Same as remove_nodes but keeps the nodes that match.
34 Args:
35 tree (Root | Element): The tree node to filter.
36 condition (Test): The condition to apply to each node.
38 Returns:
39 Root | Element: The given tree after being filtered.
40 """
42 if tree.__class__.__name__ == "AST":
43 tree = tree.tree
45 def filter_children(node):
46 children = []
47 for i, child in enumerate(node.children):
48 if child.type in ["root", "element"]:
49 node.children[i] = filter_children(node.children[i])
50 if not check(child, condition, strict=strict):
51 for idx, _ in enumerate(child.children):
52 child.children[idx].parent = node
53 children.extend(node.children[i].children)
54 else:
55 children.append(node.children[i])
56 elif check(child, condition, strict=strict):
57 children.append(node.children[i])
59 node.children = children
60 if len(node.children) == 0 and isinstance(node, Element):
61 node.startend = True
62 return node
64 filter_children(tree)
67def remove_nodes(
68 tree: Root | Element | AST,
69 condition: Test,
70 strict: bool = True,
71):
72 """Take a given tree and remove the nodes that match the condition.
73 If a parent node is removed so is all the children.
75 Same as filter_nodes except removes nodes that match.
77 Args:
78 tree (Root | Element): The parent node to start recursively removing from.
79 condition (Test): The condition to apply to each node.
80 """
81 if tree.__class__.__name__ == "AST":
82 tree = tree.tree
84 def filter_children(node):
85 node.children = [n for n in node.children if not check(n, condition, strict=strict)]
86 for child in node.children:
87 if child.type in ["root", "element"]:
88 filter_children(child)
90 if len(node.children) == 0 and isinstance(node, Element):
91 node.startend = True
93 filter_children(tree)
96def map_nodes(tree: Root | Element | AST, transform: Callable):
97 """Takes a tree and a callable that returns a node and maps each node.
99 Signature for the transform function should be as follows:
101 1. Takes a single argument that is the node.
102 2. Returns any type of node that is assigned to the original node.
104 ```python
105 def to_links(node):
106 return Element("a", {}, node.parent, children=node.children)
107 if node.type == "element"
108 else node
109 ```
111 Args:
112 tree (Root | Element): Tree to transform.
113 transform (Callable): The Callable that returns a node that is assigned
114 to each node.
115 """
117 if tree.__class__.__name__ == "AST":
118 tree = tree.tree
120 def recursive_map(node):
121 for i, child in enumerate(node.children):
122 if isinstance(child, Element):
123 recursive_map(node.children[i])
124 node.children[i] = transform(child)
125 else:
126 node.children[i] = transform(child)
128 recursive_map(tree)
131def replace_node(
132 start: Root | Element,
133 condition: Test,
134 replacement: Optional[All_Nodes | list[All_Nodes]],
135 strict: bool = True,
136):
137 """Search for a specific node in the tree and replace it with either
138 a node or list of nodes. If replacement is None the found node is just removed.
140 Args:
141 start (Root | Element): The starting point.
142 condition (test): Test condition to find the correct node.
143 replacement (All_Nodes | list[All_Nodes] | None): What to replace the node with.
144 """
145 for node in walk(start):
146 if check(node, condition, strict=strict):
147 if node.parent is not None:
148 idx = node.parent.children.index(node)
149 if replacement is not None:
150 node.parent.children = (
151 node.parent.children[:idx] + replacement + node.parent.children[idx + 1 :]
152 if isinstance(replacement, list)
153 else node.parent.children[:idx]
154 + [replacement]
155 + node.parent.children[idx + 1 :]
156 )
157 else:
158 node.parent.children.pop(idx)
161def find_and_replace(start: Root | Element, *replacements: tuple[str, str | Callable]) -> int:
162 """Takes a ast, root, or any node and replaces text in `text`
163 nodes with matching replacements.
165 First value in each replacement tuple is the regex to match and
166 the second value is what to replace it with. This can either be
167 a string or a callable that returns a string or a new node. If
168 a new node is returned then the text element will be split.
169 """
170 from re import finditer # pylint: disable=import-outside-toplevel
172 for node in walk(start):
173 if node.type == "text":
174 for replacement in replacements:
175 if isinstance(replacement[1], str):
176 for match in finditer(replacement[0], node.value):
177 node.value = (
178 node.value[: match.start()] + replacement[1] + node.value[match.end() :]
179 )
180 else:
181 raise NotImplementedError(
182 "Callables are not yet supported for find_and_replace operations."
183 )
184 # tada add ability to inject nodes in place of text replacement
185 # elif isinstance(replacement[1], Callable):
186 # for match in finditer(replacement[0], n.value):
187 # result = replacement[1](match.group())
188 # if isinstance(result, str):
189 # n.value = n.value[:match.start()]
190 # + replacement[1]
191 # + n.value[match.end():]
192 # elif isinstance(result, All_Nodes):
193 # pass
194 # elif isinstance(result, list):
195 # pass
198def shift_heading(node: Element, amount: int):
199 """Shift the heading by the amount specified.
201 value is clamped between 1 and 6.
202 """
204 rank = heading_rank(node)
205 rank += amount
207 node.tag = f"h{min(6, max(1, rank))}"
210def modify_children(func):
211 """Function wrapper that when called and passed an
212 AST, Root, or Element will apply the wrapped function
213 to each child. This means that whatever is returned
214 from the wrapped function will be assigned to the child.
216 The wrapped function will be passed the child node,
217 the index in the parents children, and the parent node
218 """
219 from phml.utils import visit_children # pylint: disable=import-outside-toplevel
221 def inner(start: AST | Element | Root):
222 if isinstance(start, AST):
223 start = start.tree
225 for idx, child in enumerate(visit_children(start)):
226 start.children[idx] = func(child, idx, child.parent)
228 return inner