Fix error which split line when it contained backslash. Fixes #5

pull/12/head
Michał Słomkowski 7 years ago
parent 4488f2ee63
commit f5a26225bd

@ -13,7 +13,7 @@ import re
__author__ = "Michał Słomkowski"
__license__ = "Apache 2.0"
__version__ = "1.0.1"
__version__ = "1.0.2"
INDENTATION = ' ' * 4
@ -54,7 +54,7 @@ def strip_variable_template_tags(line: str) -> str:
flags=re.UNICODE)
def clean_lines(orig_lines):
def clean_lines(orig_lines) -> list:
"""Strips the lines and splits them if they contain curly brackets."""
cleaned_lines = []
for line in orig_lines:
@ -68,7 +68,7 @@ def clean_lines(orig_lines):
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 != ""])
[strip_variable_template_tags(l).strip() for l in re.split(r"([{}])", line) if l != ""])
return cleaned_lines
@ -105,7 +105,8 @@ def perform_indentation(lines):
def format_config_contents(contents):
"""Accepts the string containing nginx configuration and returns formatted one. Adds newline at the end."""
lines = clean_lines(contents.splitlines())
lines = contents.splitlines()
lines = clean_lines(lines)
lines = join_opening_bracket(lines)
lines = perform_indentation(lines)

@ -24,7 +24,7 @@ class TestFormatter(unittest.TestCase):
self.assertEqual(["foo", "bar {", "johan {", "tee", "ka", "}"],
join_opening_bracket(("foo", "bar {", "johan", "{", "tee", "ka", "}")))
def test_clear_lines(self):
def test_clean_lines(self):
self.assertEqual(["ala", "ma", "{", "kota", "}", "to;", "", "ook"],
clean_lines(("ala", "ma {", "kota", "}", "to;", "", "ook")))
@ -37,6 +37,8 @@ class TestFormatter(unittest.TestCase):
self.assertEqual(["{", "ala", "# ma {{", "kota", "}", "to", "}", "# }"],
clean_lines(("{", "ala ", "# ma {{", " kota ", "}", " to} ", "# }")))
self.assertEqual(["location ~ /\.ht", "{"], clean_lines(["location ~ /\.ht {", ]))
def test_perform_indentation(self):
self.assertEqual([
"foo bar {",
@ -161,6 +163,21 @@ class TestFormatter(unittest.TestCase):
'}\n' +
'# in my line\n')
def test_backslash(self):
self._check_formatting('location ~ /\.ht {\n' +
'deny all;\n' +
'}',
'location ~ /\.ht {\n' +
' deny all;\n' +
'}\n')
self._check_formatting(' tag { wt ~ /\.ht \t "my ${var} and ~ /\.ht \tother ${ variable_name } "; }\n' +
'# in my line\n',
'tag {\n' +
' wt ~ /\.ht "my ${var} and ~ /\.ht \tother ${variable_name} ";\n' +
'}\n' +
'# in my line\n')
def test_loading_utf8_file(self):
tmp_file = tempfile.mkstemp('utf-8')[1]
shutil.copy('test-files/umlaut-utf8.conf', tmp_file)

Loading…
Cancel
Save