Files
Derfuu-Derfuu_ComfyUI_Modde…/pyscripts/Nodes/Functions/Strings.py
T
Derfuu e493cb787d Added string functional nodes:
Concat and Replace.
Removed workflow examples. (Maybe temporary)
Renamed examples folder to nodes_info_examples (WIP) if need to know more
Added string operations into README.md
2024-04-15 06:06:54 +03:00

58 lines
1.5 KiB
Python

import re
from custom_nodes.Derfuu_ComfyUI_ModdedNodes.pyscripts.components.tree import TREE_STRINGS
from custom_nodes.Derfuu_ComfyUI_ModdedNodes.pyscripts.components.fields import Field
class StringConcat:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Prepend": Field.string(),
"Append": Field.string(),
"Delimiter": Field.string(default=", ")
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("TEXT",)
FUNCTION = "concatenate"
CATEGORY = TREE_STRINGS
def concatenate(self, Prepend, Append, Delimiter):
out = f"{Prepend}{Delimiter}{Append}"
return (out,)
class StringReplace:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"Text": Field.string(),
"Pattern": Field.string(),
"Replace_With": Field.string(),
"Mode": Field.combo(["Strict", "RegEx"]),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("TEXT",)
FUNCTION = "replace"
CATEGORY = TREE_STRINGS
def replace(self, Text, Pattern, Replace_With, Mode):
out = Text
match Mode:
case "Strict":
out = Text.replace(Pattern, Replace_With)
case "RegEx":
out = re.sub(Pattern, Replace_With, Text)
return (out,)