diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..3d998ea --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] diff --git a/nodes.py b/nodes.py new file mode 100644 index 0000000..6e956ce --- /dev/null +++ b/nodes.py @@ -0,0 +1,26 @@ +from .remove_comments import remove_comments + +class CDXOO_TextNodeWithComments: + RETURN_TYPES = ("STRING",) + CATEGORY = "primitives" + FUNCTION = "get_sanitized_string" + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "string": ("STRING", { "default": "", "multiline": True }) + } + } + + def get_sanitized_string(self, string): + return (remove_comments(string),) + +NODE_CLASS_MAPPINGS = { + "@cdxoo/text-node-with-comments": CDXOO_TextNodeWithComments +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "@cdxoo/text-node-with-comments": "Text Node With Comments (@cdxoo)" +} + diff --git a/remove_comments.py b/remove_comments.py new file mode 100644 index 0000000..f95e8d0 --- /dev/null +++ b/remove_comments.py @@ -0,0 +1,18 @@ +import re + +def remove_comments(text): + def replacer(match): + s = match.group(0) + if s.startswith('/'): + #return " " # note: a space and not an empty string + return "" # NOTE: an empty string instead of a space + # since we dont care about wandom c expressions + # like int/**/x=5 + else: + return s + pattern = re.compile( + r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', + re.DOTALL | re.MULTILINE + ) + return re.sub(pattern, replacer, text) + diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_get_sanitized_string.py b/test/test_get_sanitized_string.py new file mode 100644 index 0000000..04d6dbb --- /dev/null +++ b/test/test_get_sanitized_string.py @@ -0,0 +1,25 @@ +import unittest +from textwrap import dedent +from remove_comments import remove_comments + +class TestRemoveComments(unittest.TestCase): + def test_ok(self): + str = dedent("""\ + foo,// bar, + /* + herp, + derp*/ + quux""") + + print(str); + print('---------------') + actual = remove_comments(str) + print(actual) + expected = dedent("""\ + foo, + + quux""") + self.assertEqual(actual, expected) + +if __name__ == "__main__": + unittest.main()