From 0b72d86a8bdee84e14ced120aff050c5db55a94d Mon Sep 17 00:00:00 2001 From: RickieL Date: Mon, 10 Sep 2018 22:11:40 +0800 Subject: [PATCH 1/3] fixed rewrite regex Curly Bracket case. fix the case that "Curly Bracket" in the rewrite case below: `rewrite "^/a/([\d]{2})_(\d+).html$" /a/0$1/$1$2.html last;` --- nginxfmt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nginxfmt.py b/nginxfmt.py index 0ed26d1..418b348 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -66,6 +66,8 @@ def clean_lines(orig_lines) -> list: else: if line.startswith("#"): cleaned_lines.append(strip_variable_template_tags(line)) + elif 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 != ""]) From 93748ca7a8df3bb112e7ae8390b2be518fad4895 Mon Sep 17 00:00:00 2001 From: RickieL Date: Mon, 10 Sep 2018 23:28:22 +0800 Subject: [PATCH 2/3] fix multiline `;` in one line case and rewrite regex curly bracket case. fix multiline `;` in one line case and rewrite regex curly bracket case. --- nginxfmt.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nginxfmt.py b/nginxfmt.py index 418b348..3244b85 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -66,12 +66,16 @@ def clean_lines(orig_lines) -> list: else: if line.startswith("#"): cleaned_lines.append(strip_variable_template_tags(line)) - elif 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 != ""]) - + 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 From 70a0198ecb1e7784101163d4681a109e7f75350d Mon Sep 17 00:00:00 2001 From: yongfu Date: Tue, 25 Sep 2018 20:56:37 +0800 Subject: [PATCH 3/3] add test for multi_semicolon and rewrite reg --- test_nginxfmt.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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)