Skip to content

Commit 97e1b40

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master'
2 parents 28e6b14 + 9ed1787 commit 97e1b40

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

nginxfmt.py

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,24 @@ def _strip_line(single_line):
121121
return single_line
122122

123123
within_quotes = False
124-
parts = []
125-
for part in re.split('"', single_line):
126-
if within_quotes:
127-
parts.append(part)
124+
quote_char = None
125+
result = []
126+
for char in single_line:
127+
if char in ['"', "'"]:
128+
if within_quotes:
129+
if char == quote_char:
130+
within_quotes = False
131+
quote_char = None
132+
else:
133+
within_quotes = True
134+
quote_char = char
135+
result.append(char)
136+
elif not within_quotes and re.match(r'\s', char):
137+
if result[-1] != ' ':
138+
result.append(' ')
128139
else:
129-
parts.append(re.sub(r'[\s]+', ' ', part))
130-
within_quotes = not within_quotes
131-
return '"'.join(parts)
140+
result.append(char)
141+
return ''.join(result)
132142

133143
@staticmethod
134144
def _count_multi_semicolon(single_line):
@@ -138,14 +148,21 @@ def _count_multi_semicolon(single_line):
138148
return 0, 0
139149

140150
within_quotes = False
151+
quote_char = None
141152
q = 0
142153
c = 0
143-
for part in re.split('"', single_line):
144-
if within_quotes:
145-
q = 1
146-
else:
147-
c += part.count(';')
148-
within_quotes = not within_quotes
154+
for char in single_line:
155+
if char in ['"', "'"]:
156+
if within_quotes:
157+
if char == quote_char:
158+
within_quotes = False
159+
quote_char = None
160+
else:
161+
within_quotes = True
162+
quote_char = char
163+
q = 1
164+
elif not within_quotes and char == ';':
165+
c += 1
149166
return q, c
150167

151168
@staticmethod
@@ -156,14 +173,23 @@ def _multi_semicolon(single_line):
156173
return single_line
157174

158175
within_quotes = False
159-
parts = []
160-
for part in re.split('"', single_line):
161-
if within_quotes:
162-
parts.append(part)
176+
quote_char = None
177+
result = []
178+
for char in single_line:
179+
if char in ['"', "'"]:
180+
if within_quotes:
181+
if char == quote_char:
182+
within_quotes = False
183+
quote_char = None
184+
else:
185+
within_quotes = True
186+
quote_char = char
187+
result.append(char)
188+
elif not within_quotes and char == ';':
189+
result.append(";\n")
163190
else:
164-
parts.append(part.replace(";", ";\n"))
165-
within_quotes = not within_quotes
166-
return '"'.join(parts)
191+
result.append(char)
192+
return ''.join(result)
167193

168194
def _apply_variable_template_tags(self, line: str) -> str:
169195
"""Replaces variable indicators ${ and } with tags, so subsequent formatting is easier."""

test_nginxfmt.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ def test_multi_semicolon(self):
239239
' deny all;\n' +
240240
'}\n')
241241

242+
def test_quotes1(self):
243+
self.check_formatting('''add_header Alt-Svc 'h3-25=":443"; ma=86400'; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
244+
'''add_header Alt-Svc 'h3-25=":443"; ma=86400';\n''' +
245+
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
246+
247+
def test_quotes2(self):
248+
self.check_formatting('''add_header Alt-Svc "h3-23=':443'; ma=86400"; add_header Alt-Svc 'h3-29=":443"; ma=86400';''',
249+
'''add_header Alt-Svc "h3-23=':443'; ma=86400";\n''' +
250+
'''add_header Alt-Svc 'h3-29=":443"; ma=86400';\n''')
251+
242252
def test_loading_utf8_file(self):
243253
tmp_file = pathlib.Path(tempfile.mkstemp('utf-8')[1])
244254
try:

0 commit comments

Comments
 (0)