|
|
@ -20,6 +20,8 @@ INDENTATION = ' ' * 4
|
|
|
|
TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___'
|
|
|
|
TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___'
|
|
|
|
TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___'
|
|
|
|
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):
|
|
|
|
def strip_line(single_line):
|
|
|
|
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
|
|
|
|
"""Strips the line and replaces neighbouring whitespaces with single space (except when within quotation marks)."""
|
|
|
@ -85,6 +87,37 @@ def strip_variable_template_tags(line: str) -> str:
|
|
|
|
line,
|
|
|
|
line,
|
|
|
|
flags=re.UNICODE)
|
|
|
|
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:
|
|
|
|
def clean_lines(orig_lines) -> list:
|
|
|
|
"""Strips the lines and splits them if they contain curly brackets."""
|
|
|
|
"""Strips the lines and splits them if they contain curly brackets."""
|
|
|
@ -147,12 +180,14 @@ def perform_indentation(lines):
|
|
|
|
|
|
|
|
|
|
|
|
def format_config_contents(contents):
|
|
|
|
def format_config_contents(contents):
|
|
|
|
"""Accepts the string containing nginx configuration and returns formatted one. Adds newline at the end."""
|
|
|
|
"""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 = contents.splitlines()
|
|
|
|
lines = clean_lines(lines)
|
|
|
|
lines = clean_lines(lines)
|
|
|
|
lines = join_opening_bracket(lines)
|
|
|
|
lines = join_opening_bracket(lines)
|
|
|
|
lines = perform_indentation(lines)
|
|
|
|
lines = perform_indentation(lines)
|
|
|
|
|
|
|
|
|
|
|
|
text = '\n'.join(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$', '')):
|
|
|
|
for pattern, substitute in ((r'\n{3,}', '\n\n\n'), (r'^\n', ''), (r'\n$', '')):
|
|
|
|
text = re.sub(pattern, substitute, text, re.MULTILINE)
|
|
|
|
text = re.sub(pattern, substitute, text, re.MULTILINE)
|
|
|
|