diff --git a/nginxfmt.py b/nginxfmt.py index 66ebf69..e1381c8 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -35,7 +35,10 @@ def clean_lines(orig_lines): cleaned_lines.append("") continue else: - cleaned_lines.extend([l.strip() for l in re.split("([\\{\\}])", line) if l != ""]) + if line.startswith("#"): + cleaned_lines.append(line) + else: + cleaned_lines.extend([l.strip() for l in re.split("([\\{\\}])", line) if l != ""]) return cleaned_lines @@ -56,12 +59,12 @@ def perform_indentation(lines): indented_lines = [] current_indent = 0 for line in lines: - if line.endswith('}') and current_indent > 0: + if not line.startswith("#") and line.endswith('}') and current_indent > 0: current_indent -= 1 indented_lines.append(current_indent * INDENTATION + line) - if line.endswith('{'): + if not line.startswith("#") and line.endswith('{'): current_indent += 1 return indented_lines diff --git a/test_nginxfmt.py b/test_nginxfmt.py index b7f3e1a..20c0b70 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -26,6 +26,9 @@ class TestFormatter(unittest.TestCase): self.assertEqual(["{", "ala", "ma", "{", "{", "kota", "}", "to", "}"], clean_lines(("{", "ala ", "ma {{", " kota ", "}", " to} "))) + self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"], + clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }"))) + def test_perform_indentation(self): self.assertEqual([ "foo bar {", @@ -40,6 +43,16 @@ class TestFormatter(unittest.TestCase): " }", "}"], perform_indentation(("foo bar {", "fizz bazz {", "lorem ipsum;", "asdf asdf;", "}", "}"))) + self.assertEqual([ + "foo bar {", + " fizz bazz {", + " lorem ipsum;", + " # }", + " }", + "}", + "}", + "foo {"], perform_indentation(("foo bar {", "fizz bazz {", "lorem ipsum;", "# }", "}", "}", "}", "foo {"))) + self.assertEqual([ "foo bar {", " fizz bazz {",