Skip to content

Commit d79a0a4

Browse files
committed
Improve logic for regex and macro
detection. Throw an error when four or more ```` appear in a row in the search field, which is invalid SPL.
1 parent c627d2e commit d79a0a4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

contentctl/objects/macro.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from contentctl.input.director import DirectorOutputDto
1111
from contentctl.objects.security_content_object import SecurityContentObject
1212

13-
1413
#The following macros are included in commonly-installed apps.
1514
#As such, we will ignore if they are missing from our app.
1615
#Included in
@@ -55,10 +54,15 @@ def get_macros(text_field:str, director:DirectorOutputDto , ignore_macros:set[st
5554
#If a comment ENDS in a macro, for example ```this is a comment with a macro `macro_here````
5655
#then there is a small edge case where the regex below does not work properly. If that is
5756
#the case, we edit the search slightly to insert a space
58-
text_field = re.sub(r"\`\`\`\`", r"` ```", text_field)
59-
text_field = re.sub(r"\`\`\`.*?\`\`\`", " ", text_field)
60-
57+
if re.findall(r"\`\`\`\`", text_field):
58+
raise ValueError("Search contained four or more '`' characters in a row which is invalid SPL"
59+
"This may have occurred when a macro was commented out.\n"
60+
"Please ammend your search to remove the substring '````'")
6161

62+
# replace all the macros with a space
63+
text_field = re.sub(r"\`\`\`[\s\S]*?\`\`\`", " ", text_field)
64+
65+
6266
macros_to_get = re.findall(r'`([^\s]+)`', text_field)
6367
#If macros take arguments, stop at the first argument. We just want the name of the macro
6468
macros_to_get = set([macro[:macro.find('(')] if macro.find('(') != -1 else macro for macro in macros_to_get])
@@ -68,4 +72,3 @@ def get_macros(text_field:str, director:DirectorOutputDto , ignore_macros:set[st
6872
macros_to_get -= macros_to_ignore
6973
return Macro.mapNamesToSecurityContentObjects(list(macros_to_get), director)
7074

71-

0 commit comments

Comments
 (0)