initial implementation
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from .nodes import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS
|
||||
|
||||
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
|
||||
@@ -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)"
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user