Added text replacer

This commit is contained in:
exectails
2024-11-24 14:06:18 +01:00
parent f4d308a827
commit f6557d3201
3 changed files with 75 additions and 1 deletions
+38
View File
@@ -37,6 +37,44 @@ Output
"Hello, John! Did you know? 42 is the answer to everything."
```
### Replace Text
Replaces a string in a text with something else, using either plain text
replacement or regular expressions (regex).
**Example 1**
Replace a word in a text.
Input
```text
text: "Hello, John!"
replace: "John"
replacement: "Mr. Smith"
```
Output
```text
"Hello, Mr. Smith!"
```
**Example 2**
Replace a pattern in a text using regex and use a captured part in the
replacement.
Input
```text
text: "Hello, John! Did you know 42 is the answer to everything?"
replace: "([0-9]+) is"
replacement: "\1 is in fact"
```
Output
```text
"Hello, John! Did you know 42 is in fact the answer to everything?"
```
### ATOI/ITOA
Two simple nodes to convert strings to integers and vice versa.
+3 -1
View File
@@ -1,6 +1,7 @@
from .nodes.converter import ETATOI, ETITOA
from .nodes.formatter import ETTextFormatter2Node, ETTextFormatter5Node, ETTextFormatter10Node
from .nodes.scripting import ETPythonTextScript3Node
from .nodes.replacer import ETReplaceTextNode
et_nodes = {
("ATOI", ETATOI),
@@ -10,11 +11,12 @@ et_nodes = {
("Text Formatter (5 Arguments)", ETTextFormatter5Node),
("Text Formatter (10 Arguments)", ETTextFormatter10Node),
("Text Replacer", ETReplaceTextNode),
("Python Text Script (3 Arguments)", ETPythonTextScript3Node),
}
NODE_CLASS_MAPPINGS = {cls.__name__: cls for display_name, cls in et_nodes}
NODE_DISPLAY_NAME_MAPPINGS = {cls.__name__: display_name for display_name, cls in et_nodes}
#WEB_DIRECTORY = "./js"
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
+34
View File
@@ -0,0 +1,34 @@
import re
class ETReplaceTextNode:
"""
A node that replaces text in a string.
"""
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"default": "", "multiline": False, "tooltip": "The text to replace in."}),
"replace": ("STRING", {"default": "", "multiline": False, "tooltip": "The text to replace."}),
"replacement": ("STRING", {"default": "", "multiline": False, "tooltip": "The text to replace with."}),
"mode": (["normal", "regex"], {"default": "normal", "tooltip": "The mode to use for replacing."}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("text",)
CATEGORY = "exectails"
FUNCTION = "process"
def process(self, text: str, replace: str, replacement: str, mode: str) -> tuple:
result = ""
if mode == "normal":
result = text.replace(replace, replacement)
else:
result = re.sub(replace, replacement, text)
return (result,)