diff --git a/nginxfmt.py b/nginxfmt.py index 0ed26d1..3244b85 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -67,9 +67,15 @@ def clean_lines(orig_lines) -> list: if line.startswith("#"): cleaned_lines.append(strip_variable_template_tags(line)) else: - cleaned_lines.extend( - [strip_variable_template_tags(l).strip() for l in re.split(r"([{}])", line) if l != ""]) - + if line.count(";") > 1: + newlines = line.split(";") + cleaned_lines.extend(clean_lines(["".join([ln, ";"]) for ln in newlines if ln != ""])) + else: + if line.startswith("rewrite"): + cleaned_lines.append(strip_variable_template_tags(line)) + else: + cleaned_lines.extend( + [strip_variable_template_tags(l).strip() for l in re.split(r"([{}])", line) if l != ""]) return cleaned_lines diff --git a/test_nginxfmt.py b/test_nginxfmt.py index 9018cd4..17c662d 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -36,6 +36,12 @@ class TestFormatter(unittest.TestCase): self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"], clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " 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", "}"))) + + self.assertEqual(["{", "ala", "# ma {{", "aa last;", "bb to;", "}"], + clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}"))) self.assertEqual(["location ~ /\.ht", "{"], clean_lines(["location ~ /\.ht {", ])) @@ -178,6 +184,16 @@ class TestFormatter(unittest.TestCase): '}\n' + '# in my line\n') + def test_multi_semicolon(self): + self._check_formatting('location /a { \n' + + 'allow 127.0.0.1; allow 10.0.0.0/8; deny all; \n' + + '}\n', + 'location /a {\n' + + ' allow 127.0.0.1;\n' + + ' allow 10.0.0.0/8;\n' + + ' deny all;\n' + + '}\n') + def test_loading_utf8_file(self): tmp_file = tempfile.mkstemp('utf-8')[1] shutil.copy('test-files/umlaut-utf8.conf', tmp_file)