initial commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
class TemplateString:
|
||||
"""
|
||||
Replaces {} placeholders in a template with input values, in order.
|
||||
|
||||
Template: "a {subject} on a {surface}, {lighting} lighting, {quality}"
|
||||
input_1: "burger"
|
||||
input_2: "wooden table"
|
||||
input_3: "cinematic"
|
||||
input_4: "8k"
|
||||
Result: "a burger on a wooden table, cinematic lighting, 8k"
|
||||
|
||||
Text inside {} is treated as a comment and ignored during substitution.
|
||||
Unused placeholders are left as-is. Extra inputs are ignored.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"template": ("STRING", {
|
||||
"default": "a {}, {}, {}",
|
||||
"multiline": True,
|
||||
}),
|
||||
},
|
||||
"optional": {
|
||||
"input_1": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_2": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_3": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_4": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_5": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_6": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_7": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_8": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_9": ("STRING", {"default": "", "forceInput": True}),
|
||||
"input_10": ("STRING", {"default": "", "forceInput": True}),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("STRING",)
|
||||
RETURN_NAMES = ("text",)
|
||||
FUNCTION = "render"
|
||||
CATEGORY = "text"
|
||||
|
||||
def render(self, template, **kwargs):
|
||||
values = []
|
||||
for i in range(1, 11):
|
||||
key = f"input_{i}"
|
||||
if key in kwargs and kwargs[key] is not None:
|
||||
values.append(kwargs[key])
|
||||
|
||||
# Replace {anything} placeholders one at a time, left to right
|
||||
# Text inside braces is treated as a comment and discarded
|
||||
import re
|
||||
result = template
|
||||
for val in values:
|
||||
result = re.sub(r"\{[^}]*\}", val, result, count=1)
|
||||
|
||||
return (result,)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"TemplateString": TemplateString,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"TemplateString": "Template String",
|
||||
}
|
||||
Reference in New Issue
Block a user