Skip to content

Commit 2629743

Browse files
committed
Parse capture groups in if-blocks. Fixes #50.
1 parent c25bc91 commit 2629743

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

gixy/directives/block.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def needs_anchor(self):
149149
class IfBlock(Block):
150150
nginx_name = "if"
151151
self_context = False
152+
provide_variables = True
152153

153154
def __init__(self, name, args):
154155
super(IfBlock, self).__init__(name, args)
@@ -168,6 +169,25 @@ def __init__(self, name, args):
168169
else:
169170
raise Exception('Unknown "if" definition, args: {0!r}'.format(args))
170171

172+
@property
173+
def is_regex(self):
174+
return self.operand and self.operand in ("~", "~*", '!~', '!~*')
175+
176+
@cached_property
177+
def variables(self):
178+
if not self.is_regex:
179+
return []
180+
181+
regexp = Regexp(self.value, case_sensitive=self.operand in ["~", '!~'])
182+
result = []
183+
for name, group in regexp.groups.items():
184+
result.append(
185+
Variable(name=name, value=group, boundary=None, provider=self)
186+
)
187+
return result
188+
189+
190+
171191
def __str__(self):
172192
return "{name} ({args}) {{".format(name=self.name, args=" ".join(self.args))
173193

0 commit comments

Comments
 (0)