fix regular expression with { } like in server_name,rewrite,location and if.
This commit is contained in:
36
nginxfmt.py
36
nginxfmt.py
@@ -20,6 +20,8 @@ INDENTATION = ' ' * 4
|
|||||||
TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___'
|
TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___'
|
||||||
TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
|
TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
|
||||||
|
|
||||||
|
TEMPLATE_REG_OPENING_TAG = '___TEMPLATE_REG_OPENING_TAG___'
|
||||||
|
TEMPLATE_REG_CLOSING_TAG = '___TEMPLATE_REG_CLOSING_TAG___'
|
||||||
|
|
||||||
def strip_line(single_line):
|
def strip_line(single_line):
|
||||||
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
|
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
|
||||||
@@ -45,7 +47,7 @@ def multi_semicolon(single_line):
|
|||||||
return single_line, 0
|
return single_line, 0
|
||||||
|
|
||||||
m1 = re.match(r"^([^;#]*;)([\s]*#.*)?$", single_line)
|
m1 = re.match(r"^([^;#]*;)([\s]*#.*)?$", single_line)
|
||||||
m2 = re.match(r"^([^#]+)(;[\s]*)(#.*)?$", single_line)
|
m2 = re.match(r"^([^#]+)([;][\s]*)(#.*)?$", single_line)
|
||||||
|
|
||||||
if m1 is not None:
|
if m1 is not None:
|
||||||
return single_line, 0
|
return single_line, 0
|
||||||
@@ -74,6 +76,30 @@ def multi_semicolon(single_line):
|
|||||||
return single_line, 0
|
return single_line, 0
|
||||||
|
|
||||||
|
|
||||||
|
def apply_reg_template_tags(line: str) -> str:
|
||||||
|
"""Replaces rewrite/server_name/if/location regular expression have { } in quotes with tags"""
|
||||||
|
parts = []
|
||||||
|
within_quotes = False
|
||||||
|
for part in re.split('"', line):
|
||||||
|
if within_quotes:
|
||||||
|
part = part.replace("{", TEMPLATE_REG_OPENING_TAG)
|
||||||
|
part = part.replace("}", TEMPLATE_REG_CLOSING_TAG)
|
||||||
|
parts.append(part)
|
||||||
|
else:
|
||||||
|
parts.append(part)
|
||||||
|
within_quotes = not within_quotes
|
||||||
|
|
||||||
|
line = '"'.join(parts)
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
def strip_reg_template_tags(line: str) -> str:
|
||||||
|
"""Replaces rewrite/server_name/if/location regular expression have { } in quotes with tags"""
|
||||||
|
line = line.replace(TEMPLATE_REG_OPENING_TAG, "{")
|
||||||
|
line = line.replace(TEMPLATE_REG_CLOSING_TAG, "}")
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
def apply_variable_template_tags(line: str) -> str:
|
def apply_variable_template_tags(line: str) -> str:
|
||||||
"""Replaces variable indicators ${ and } with tags, so subsequent formatting is easier."""
|
"""Replaces variable indicators ${ and } with tags, so subsequent formatting is easier."""
|
||||||
return re.sub(r'\${\s*(\w+)\s*}',
|
return re.sub(r'\${\s*(\w+)\s*}',
|
||||||
@@ -96,22 +122,20 @@ def clean_lines(orig_lines) -> list:
|
|||||||
for line in orig_lines:
|
for line in orig_lines:
|
||||||
line = strip_line(line)
|
line = strip_line(line)
|
||||||
line = apply_variable_template_tags(line)
|
line = apply_variable_template_tags(line)
|
||||||
|
line = apply_reg_template_tags(line)
|
||||||
if line == "":
|
if line == "":
|
||||||
cleaned_lines.append("")
|
cleaned_lines.append("")
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
if line.startswith("#"):
|
if line.startswith("#"):
|
||||||
cleaned_lines.append(strip_variable_template_tags(line))
|
cleaned_lines.append(strip_reg_template_tags(strip_variable_template_tags(line)))
|
||||||
else:
|
else:
|
||||||
mline, c = multi_semicolon(line)
|
mline, c = multi_semicolon(line)
|
||||||
if c > 0:
|
if c > 0:
|
||||||
cleaned_lines.extend(clean_lines(mline.splitlines()))
|
cleaned_lines.extend(clean_lines(mline.splitlines()))
|
||||||
else:
|
|
||||||
if mline.startswith("rewrite"):
|
|
||||||
cleaned_lines.append(strip_variable_template_tags(mline))
|
|
||||||
else:
|
else:
|
||||||
cleaned_lines.extend(
|
cleaned_lines.extend(
|
||||||
[strip_variable_template_tags(l).strip() for l in re.split(r"([{}])", mline) if l != ""])
|
[strip_reg_template_tags(strip_variable_template_tags(l)).strip() for l in re.split(r"([{}])", mline) if l != ""])
|
||||||
return cleaned_lines
|
return cleaned_lines
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ class TestFormatter(unittest.TestCase):
|
|||||||
self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"],
|
self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"],
|
||||||
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
|
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
|
||||||
|
|
||||||
self.assertEqual(["{", "ala", "# ma {{", "rewrite /([\d]{2}) /up/$1.html last;", "}", "to", "}"],
|
self.assertEqual(["{", "ala", "# ma {{", "rewrite \"/([\d]{2})\" /up/$1.html last;", "}", "to", "}"],
|
||||||
clean_lines(("{", "ala ", "# ma {{", " rewrite /([\d]{2}) /up/$1.html last; ", "}", " to", "}")))
|
clean_lines(("{", "ala ", "# ma {{", " rewrite \"/([\d]{2})\" /up/$1.html last; ", "}", " to", "}")))
|
||||||
|
|
||||||
self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"],
|
self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"],
|
||||||
clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
|
clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}")))
|
||||||
@@ -200,6 +200,10 @@ class TestFormatter(unittest.TestCase):
|
|||||||
' deny all;\n' +
|
' deny all;\n' +
|
||||||
'}\n')
|
'}\n')
|
||||||
|
|
||||||
|
def test_reg_template_tags(self):
|
||||||
|
self.assertEqual('server_name "~^(?<tag>[0-9a-f]___TEMPLATE_REG_OPENING_TAG___8___TEMPLATE_REG_CLOSING_TAG___)\.a\.b\.com$";',
|
||||||
|
apply_reg_template_tags('server_name "~^(?<tag>[0-9a-f]{8})\.a\.b\.com$";'))
|
||||||
|
|
||||||
def test_loading_utf8_file(self):
|
def test_loading_utf8_file(self):
|
||||||
tmp_file = tempfile.mkstemp('utf-8')[1]
|
tmp_file = tempfile.mkstemp('utf-8')[1]
|
||||||
shutil.copy('test-files/umlaut-utf8.conf', tmp_file)
|
shutil.copy('test-files/umlaut-utf8.conf', tmp_file)
|
||||||
|
|||||||
Reference in New Issue
Block a user