From cd5eee8dcfbc3df6003ea89067c9df1fcd90a867 Mon Sep 17 00:00:00 2001 From: weijunjie Date: Wed, 24 Jul 2019 19:56:52 +0800 Subject: [PATCH 1/2] fix curly bracket in quotes --- __pycache__/nginxfmt.cpython-37.pyc | Bin 0 -> 7312 bytes nginxfmt.py | 35 ++++++++++++++++++++++++++++ test_nginxfmt.py | 6 +++++ 3 files changed, 41 insertions(+) create mode 100644 __pycache__/nginxfmt.cpython-37.pyc diff --git a/__pycache__/nginxfmt.cpython-37.pyc b/__pycache__/nginxfmt.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..67f9adf876a9a4f5c7803ee82982c31f21a2529e GIT binary patch literal 7312 zcmbVROK%+6b*@`g{VGxvMNw~QriOa4M`BZwZCTMM%1{#J(Ts*tB#t#HQ&TSXy-gO` ztZv?0O;W9DBNz|76XGBX1A!N$7Y6bJvhm*e1<9s>tP%v6P1XT7$#-sbx7ZYA7-^!a z?)z~b-+A!fp+k;=-@iS$^>S>&F#dxoy8Pv!7{b%_()Q^ed{Da>z{vmf!JHbEV z6WHY>KhH1Vdx~G=m+(E!|AJ5Q@^{QTBhK=VSIu8D=?S)SP8j^k8|Hl=K3MsXU*%K0 z!l(JQH#ReL%XNMOEkD@P@qsSqb;}Eh-%?dO9pX>A+@kBlp99WtU?uwD0Vl4R~5&3B14Q1>h)J{v$a?c)w1Aj zE8;@B%W>RPx2LCT>YM zZ{6pA{I;>S+SpX9VcVH$2GwQZ-l$w_=dM?-Rc^Fx7eDX3@B8z!Up{?2Ge7J9bmrNk znR}0C{U=Xn=N`@d#Gjw}3HskN=>Fp;&-VQ9VU+_#&htc!m5!A#`DtQq+ljTq zm=TRr8DnzX<~g+K)`Sfk9kzZI{I$08iMhkT<$QbXc?`}fHwMY-VIvs>k2 zV@gCD)71tS?-9AIxwaNpny=`bIIiCMH1X>=UWQp@4zo?0*=!8YusM#p979i^$vIT$ z(MhO6+Z$7k1c>4H5v&+-on4 z#7wLjDR8mes`pXrc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, flags=re.UNICODE) +def apply_bracket_template_tags(content: str) -> str: + """ Replaces bracket { and } with tags, so subsequent formatting is easier.""" + result = "" + in_quotes = False + last_c = "" + + for c in content: + if (c == "\'" or c == "\"") and last_c != "\\": + in_quotes = reverse_in_quotes_status(in_quotes) + if in_quotes: + if c == "{": + result += TEMPLATE_BRACKET_OPENING_TAG + elif c == "}": + result += TEMPLATE_BRACKET_CLOSING_TAG + else: + result += c + else: + result += c + 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.""" @@ -147,12 +180,14 @@ def perform_indentation(lines): def format_config_contents(contents): """Accepts the string containing nginx configuration and returns formatted one. Adds newline at the end.""" + contents = apply_bracket_template_tags(contents) lines = contents.splitlines() lines = clean_lines(lines) lines = join_opening_bracket(lines) lines = perform_indentation(lines) text = '\n'.join(lines) + text = strip_bracket_template_tags(text) for pattern, substitute in ((r'\n{3,}', '\n\n\n'), (r'^\n', ''), (r'\n$', '')): text = re.sub(pattern, substitute, text, re.MULTILINE) diff --git a/test_nginxfmt.py b/test_nginxfmt.py index 6eb9da7..0de6c8f 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -89,6 +89,12 @@ class TestFormatter(unittest.TestCase): self.assertEqual('lorem ipsum " foo bar zip " or " dd aa " mi', 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\"")) + + 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\"")) + def test_variable_template_tags(self): self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___", apply_variable_template_tags("foo bar ${myvar}")) From 0e11260d65ce320403a375c091752b13e2604820 Mon Sep 17 00:00:00 2001 From: weijunjie Date: Wed, 24 Jul 2019 20:18:52 +0800 Subject: [PATCH 2/2] add ut --- test_nginxfmt.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_nginxfmt.py b/test_nginxfmt.py index 0de6c8f..69c1f83 100644 --- a/test_nginxfmt.py +++ b/test_nginxfmt.py @@ -91,9 +91,11 @@ class TestFormatter(unittest.TestCase): 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\"")) 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\"")) def test_variable_template_tags(self): self.assertEqual("foo bar ___TEMPLATE_VARIABLE_OPENING_TAG___myvar___TEMPLATE_VARIABLE_CLOSING_TAG___",