Update README files to include new Text Combiner node and its features. Added an image to illustrate the functionality. Enhanced TextCombiner class to support regex usage for separators, improving flexibility in text processing.
更新了README文件,以包含新的文本组合器节点及其功能。添加了一张图片以说明其功能。增强了TextCombiner类,以支持正则表达式用作分隔符,提高了文本处理的灵活性。
This commit is contained in:
@@ -103,6 +103,7 @@ Remove empty lines and spaces to generate list:
|
||||
|
||||
### 🔗Text Combiner✨
|
||||
Added Text Combiner node for combining multiple text inputs into a single output.
|
||||

|
||||
|
||||
Features:
|
||||
- Dynamic inputs: automatically adds new input points when connected
|
||||
|
||||
@@ -115,6 +115,7 @@
|
||||
|
||||
### 🔗文本组合器(TextCombiner)✨
|
||||
新增文本组合器节点,可以将多个文本输入组合成一个文本输出。
|
||||

|
||||
|
||||
功能特点:
|
||||
- 支持动态输入:连接输入时自动增加新的输入点
|
||||
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 573 KiB |
+22
-26
@@ -13,8 +13,9 @@ class TextCombiner:
|
||||
"separator": ("STRING", {
|
||||
"default": ",",
|
||||
"multiline": False,
|
||||
"placeholder": "支持正则表达式和转义字符,如: ,|\\n。使用特殊字符请加\\转义"
|
||||
"placeholder": "支持正则表达式和转义字符,如: ,|\\n。使用特殊字符请关闭正则开关"
|
||||
}),
|
||||
"use_regex": ("BOOLEAN", {"default": False}),
|
||||
},
|
||||
"optional": {}
|
||||
}
|
||||
@@ -24,19 +25,8 @@ class TextCombiner:
|
||||
FUNCTION = "combine_text"
|
||||
CATEGORY = "🎨Painting👓Coder/📝Text"
|
||||
|
||||
def combine_text(self, separator=",", **kwargs):
|
||||
def combine_text(self, separator=",", use_regex=False, **kwargs):
|
||||
try:
|
||||
# 如果分隔符不是正则表达式,对特殊字符进行转义
|
||||
if not any(c in separator for c in "[](){}?*+|^$\\"):
|
||||
separator = re.escape(separator)
|
||||
|
||||
# 处理转义字符
|
||||
separator = bytes(separator, "utf-8").decode("unicode_escape")
|
||||
|
||||
# 检查分隔符是否包含换行相关字符
|
||||
is_newline_separator = bool(re.search(r'\\[rn]|[\r\n]', separator))
|
||||
|
||||
# 收集所有非空文本
|
||||
texts = []
|
||||
for i in range(1, len(kwargs) + 1):
|
||||
key = f"text_{i}"
|
||||
@@ -45,29 +35,35 @@ class TextCombiner:
|
||||
# 标准化输入文本的换行符
|
||||
input_text = input_text.replace('\r\n', '\n').replace('\r', '\n')
|
||||
|
||||
# 使用正则表达式分割输入文本
|
||||
split_texts = re.split(separator, input_text) if separator != " " else [input_text]
|
||||
if use_regex:
|
||||
# 正则模式:处理转义字符并使用正则分割
|
||||
try:
|
||||
separator_escaped = bytes(separator, "utf-8").decode("unicode_escape")
|
||||
split_texts = re.split(separator_escaped, input_text)
|
||||
except Exception as e:
|
||||
return (f"正则表达式错误: {str(e)}",)
|
||||
else:
|
||||
# 普通模式:直接使用字符分割
|
||||
if separator.strip():
|
||||
split_texts = input_text.split(separator)
|
||||
else:
|
||||
split_texts = [input_text]
|
||||
|
||||
# 过滤空字符串并添加到结果列表,保留纯空格字符串
|
||||
texts.extend(t.rstrip('\n').rstrip('\r') for t in split_texts if t is not None)
|
||||
texts.extend(t for t in split_texts if t is not None)
|
||||
|
||||
# 根据分隔符类型选择拼接方式
|
||||
if is_newline_separator:
|
||||
# 如果分隔符包含换行,使用换行符拼接
|
||||
# 根据分隔符是否包含换行来决定输出格式
|
||||
if '\n' in (separator if not use_regex else bytes(separator, "utf-8").decode("unicode_escape")):
|
||||
combined_text = '\n'.join(t if t.strip() else '""' for t in texts) if texts else "等待输入文本"
|
||||
elif not separator.strip():
|
||||
# 如果分隔符为空,使用单个空格拼接
|
||||
combined_text = ' '.join(t if t.strip() else '""' for t in texts) if texts else "等待输入文本"
|
||||
else:
|
||||
# 否则使用逗号和空格拼接
|
||||
combined_text = ", ".join(t if t.strip() else '""' for t in texts) if texts else "等待输入文本"
|
||||
combined_text = f"{separator}".join(t if t.strip() else '""' for t in texts) if texts else "等待输入文本"
|
||||
|
||||
return (combined_text,)
|
||||
|
||||
except re.error as e:
|
||||
# 如果正则表达式无效,返回错误信息
|
||||
return (f"Regular expression error: {str(e)}",)
|
||||
except Exception as e:
|
||||
# 处理其他可能的错误
|
||||
# 处理可能的错误
|
||||
return (f"Error: {str(e)}",)
|
||||
|
||||
# 添加到 ComfyUI 节点注册
|
||||
|
||||
Reference in New Issue
Block a user