diff --git a/.gitignore b/.gitignore index 0c9eabd..722e002 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea *.iml +__pycache__ diff --git a/nginxfmt.py b/nginxfmt.py index 57c7c35..01ae020 100755 --- a/nginxfmt.py +++ b/nginxfmt.py @@ -23,6 +23,7 @@ TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___' TEMPLATE_BRACKET_OPENING_TAG = '___TEMPLATE_BRACKET_OPENING_TAG___' TEMPLATE_BRACKET_CLOSING_TAG = '___TEMPLATE_BRACKET_CLOSING_TAG___' + def strip_line(single_line): """Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks).""" single_line = single_line.strip() @@ -39,6 +40,7 @@ def strip_line(single_line): within_quotes = not within_quotes return '"'.join(parts) + def count_multi_semicolon(single_line): """count multi_semicolon (except when within quotation marks).""" single_line = single_line.strip() @@ -56,6 +58,7 @@ def count_multi_semicolon(single_line): within_quotes = not within_quotes return q, c + def multi_semicolon(single_line): """break multi_semicolon into multiline (except when within quotation marks).""" single_line = single_line.strip() @@ -72,6 +75,7 @@ def multi_semicolon(single_line): within_quotes = not within_quotes return '"'.join(parts) + def apply_variable_template_tags(line: str) -> str: """Replaces variable indicators ${ and } with tags, so subsequent formatting is easier.""" return re.sub(r'\${\s*(\w+)\s*}', @@ -87,6 +91,7 @@ def strip_variable_template_tags(line: str) -> str: line, flags=re.UNICODE) + def apply_bracket_template_tags(content: str) -> str: """ Replaces bracket { and } with tags, so subsequent formatting is easier.""" result = "" @@ -108,17 +113,20 @@ def apply_bracket_template_tags(content: str) -> str: last_c = c return result + def reverse_in_quotes_status(status: bool) -> bool: if status: return False return True + def strip_bracket_template_tags(content: str) -> str: """ Replaces tags back with { and } respectively.""" content = content.replace(TEMPLATE_BRACKET_OPENING_TAG, "{", -1) content = content.replace(TEMPLATE_BRACKET_CLOSING_TAG, "}", -1) return content + def clean_lines(orig_lines) -> list: """Strips the lines and splits them if they contain curly brackets.""" cleaned_lines = [] @@ -132,7 +140,7 @@ def clean_lines(orig_lines) -> list: if line.startswith("#"): cleaned_lines.append(strip_variable_template_tags(line)) else: - q , c = count_multi_semicolon(line) + q, c = count_multi_semicolon(line) if q == 1 and c > 1: ml = multi_semicolon(line) cleaned_lines.extend(clean_lines(ml.splitlines())) @@ -141,7 +149,7 @@ def clean_lines(orig_lines) -> list: 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)) + 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 != ""]) diff --git a/test_nginxfmt.py b/test_nginxfmt.py index 69c1f83..f9d8d0e 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -36,15 +36,16 @@ 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", "}"))) + 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; ", "}"))) + clean_lines(("{", "ala ", "# ma {{", " aa last; bb to; ", "}"))) self.assertEqual(["{", "aa;", "b b \"cc; dd; ee \";", "ssss;", "}"], - clean_lines(("{", "aa; b b \"cc; dd; ee \"; ssss;", "}"))) + clean_lines(("{", "aa; b b \"cc; dd; ee \"; ssss;", "}"))) self.assertEqual(["location ~ /\.ht", "{"], clean_lines(["location ~ /\.ht {", ])) @@ -90,12 +91,17 @@ class TestFormatter(unittest.TestCase): strip_line(' lorem ipsum " foo bar zip " or \t " dd aa " mi')) def test_apply_bracket_template_tags(self): - self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"", apply_bracket_template_tags("\"aaa{dd}bbb\"")) - self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"", apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"")) + self.assertEqual("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"", + apply_bracket_template_tags("\"aaa{dd}bbb\"")) + self.assertEqual( + "\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"", + apply_bracket_template_tags("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"")) def test_strip_bracket_template_tags(self): - self.assertEqual("\"aaa{dd}bbb\"", strip_bracket_template_tags("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"")) - self.assertEqual("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"", apply_bracket_template_tags("\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"")) + self.assertEqual("\"aaa{dd}bbb\"", strip_bracket_template_tags( + "\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"")) + self.assertEqual("\"aaa{dd}bbb\"cc{cc}cc\"dddd{eee}fff\"", apply_bracket_template_tags( + "\"aaa___TEMPLATE_BRACKET_OPENING_TAG___dd___TEMPLATE_BRACKET_CLOSING_TAG___bbb\"cc{cc}cc\"dddd___TEMPLATE_BRACKET_OPENING_TAG___eee___TEMPLATE_BRACKET_CLOSING_TAG___fff\"")) def test_variable_template_tags(self): self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",