diff --git a/README.md b/README.md index d7410dd..b089ee2 100644 --- a/README.md +++ b/README.md @@ -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. +![Added Text Combiner node for combining multiple text inputs into a single output.](docs/images/text_combiner01.png) Features: - Dynamic inputs: automatically adds new input points when connected diff --git a/README_CN.md b/README_CN.md index 777edc2..c8d99a8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -115,6 +115,7 @@ ### 🔗文本组合器(TextCombiner)✨ 新增文本组合器节点,可以将多个文本输入组合成一个文本输出。 +![新增文本组合器节点,可以将多个文本输入组合成一个文本输出。](docs/images/text_combiner01.png) 功能特点: - 支持动态输入:连接输入时自动增加新的输入点 diff --git a/__pycache__/text_combiner.cpython-311.pyc b/__pycache__/text_combiner.cpython-311.pyc index 0435ebe..8cbeaca 100644 Binary files a/__pycache__/text_combiner.cpython-311.pyc and b/__pycache__/text_combiner.cpython-311.pyc differ diff --git a/docs/images/text_combiner01.png b/docs/images/text_combiner01.png new file mode 100644 index 0000000..d38e3ba Binary files /dev/null and b/docs/images/text_combiner01.png differ diff --git a/text_combiner.py b/text_combiner.py index 3eef9f4..71e4052 100644 --- a/text_combiner.py +++ b/text_combiner.py @@ -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 节点注册