|
|
@ -14,6 +14,7 @@ INDENTATION = ' ' * 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def strip_line(single_line):
|
|
|
|
def strip_line(single_line):
|
|
|
|
|
|
|
|
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
|
|
|
|
within_quotes = False
|
|
|
|
within_quotes = False
|
|
|
|
parts = []
|
|
|
|
parts = []
|
|
|
|
for part in re.split('"', single_line.strip()):
|
|
|
|
for part in re.split('"', single_line.strip()):
|
|
|
@ -26,6 +27,7 @@ def strip_line(single_line):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_lines(orig_lines):
|
|
|
|
def clean_lines(orig_lines):
|
|
|
|
|
|
|
|
"""Strips the lines and splits them if they contain curly brackets."""
|
|
|
|
cleaned_lines = []
|
|
|
|
cleaned_lines = []
|
|
|
|
for line in orig_lines:
|
|
|
|
for line in orig_lines:
|
|
|
|
line = strip_line(line)
|
|
|
|
line = strip_line(line)
|
|
|
@ -38,7 +40,8 @@ def clean_lines(orig_lines):
|
|
|
|
return cleaned_lines
|
|
|
|
return cleaned_lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def join_opening_parenthesis(lines):
|
|
|
|
def join_opening_bracket(lines):
|
|
|
|
|
|
|
|
"""When opening curly bracket is in it's own line (K&R convention), it's joined with precluding line (Java)."""
|
|
|
|
modified_lines = []
|
|
|
|
modified_lines = []
|
|
|
|
for i in range(len(lines)):
|
|
|
|
for i in range(len(lines)):
|
|
|
|
if i > 0 and lines[i] == "{":
|
|
|
|
if i > 0 and lines[i] == "{":
|
|
|
@ -49,6 +52,7 @@ def join_opening_parenthesis(lines):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def perform_indentation(lines):
|
|
|
|
def perform_indentation(lines):
|
|
|
|
|
|
|
|
"""Indents the lines according to their nesting level determined by curly brackets."""
|
|
|
|
indented_lines = []
|
|
|
|
indented_lines = []
|
|
|
|
current_indent = 0
|
|
|
|
current_indent = 0
|
|
|
|
for line in lines:
|
|
|
|
for line in lines:
|
|
|
@ -64,8 +68,9 @@ def perform_indentation(lines):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_config_file(contents):
|
|
|
|
def format_config_file(contents):
|
|
|
|
|
|
|
|
"""Accepts the string containing nginx configuration and returns formatted one. Adds newline at the end."""
|
|
|
|
lines = clean_lines(contents.splitlines())
|
|
|
|
lines = clean_lines(contents.splitlines())
|
|
|
|
lines = join_opening_parenthesis(lines)
|
|
|
|
lines = join_opening_bracket(lines)
|
|
|
|
lines = perform_indentation(lines)
|
|
|
|
lines = perform_indentation(lines)
|
|
|
|
|
|
|
|
|
|
|
|
return '\n'.join(lines) + '\n'
|
|
|
|
return '\n'.join(lines) + '\n'
|
|
|
|