-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathloop_write_text.py
55 lines (45 loc) · 1.55 KB
/
loop_write_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import re
import itertools
class LoopWriteText:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"multiline": True}),
},
"optional": {
"variables": ("STRING", {"forceInput": True}),
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("texts",)
FUNCTION = "loop_write_text"
OUTPUT_NODE = True
OUTPUT_IS_LIST = (True,)
CATEGORY = "Bjornulf"
def loop_write_text(self, text, variables=""):
# Parse variables
var_dict = {}
for line in variables.split('\n'):
if '=' in line:
key, value = line.strip().split('=', 1)
var_dict[key.strip()] = value.strip()
# Replace variables in the text
for key, value in var_dict.items():
text = text.replace(f"<{key}>", value)
pattern = r'\{([^}]+)\}'
matches = re.findall(pattern, text)
if not matches:
return ([text],)
options_list = [opt.split('|') for opt in matches]
combinations = list(itertools.product(*options_list))
results = []
for combo in combinations:
result = text
for i, match in enumerate(matches):
result = result.replace(f"{{{match}}}", combo[i], 1)
results.append(result)
return (results,)
@classmethod
def IS_CHANGED(s, text, variables=""):
return float("nan") # Always re-execute to ensure consistency