initial implementation

This commit is contained in:
Jan Schwalbe
2024-07-30 04:33:43 +02:00
parent 17585b01d8
commit 3a3a199059
5 changed files with 72 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
+26
View File
@@ -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)"
}
+18
View File
@@ -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)
View File
+25
View File
@@ -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()