From ae8c8b44e73d6a6173686f7e0fce64c46a977646 Mon Sep 17 00:00:00 2001 From: Brainwrecked Tech Date: Tue, 26 Nov 2019 08:40:29 -0500 Subject: [PATCH] Added uniform spacing of directives and their parameters --- nginxfmt.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) mode change 100755 => 100644 nginxfmt.py diff --git a/nginxfmt.py b/nginxfmt.py old mode 100755 new mode 100644 index 01ae020..98863ec --- a/nginxfmt.py +++ b/nginxfmt.py @@ -15,7 +15,7 @@ __author__ = "Michał Słomkowski" __license__ = "Apache 2.0" __version__ = "1.0.2" -INDENTATION = ' ' * 4 +INDENTATION = ' ' TEMPLATE_VARIABLE_OPENING_TAG = '___TEMPLATE_VARIABLE_OPENING_TAG___' TEMPLATE_VARIABLE_CLOSING_TAG = '___TEMPLATE_VARIABLE_CLOSING_TAG___' @@ -171,12 +171,21 @@ def perform_indentation(lines): """Indents the lines according to their nesting level determined by curly brackets.""" indented_lines = [] current_indent = 0 + current_line = "" + for line in lines: if not line.startswith("#") and line.endswith('}') and current_indent > 0: current_indent -= 1 if line != "": - indented_lines.append(current_indent * INDENTATION + line) + directive = line.split(' ', maxsplit=1) + if directive[0] == '}' or directive[0] == 'if' or directive[0] == 'location': + indented_lines.append(current_indent * INDENTATION + line) + elif directive[1] == '{': + indented_lines.append(current_indent * INDENTATION + line) + else: + directive_split_space = int( ( 40 - (current_indent * 8) - len(directive[0]) ) / 8 ) + 1 + indented_lines.append(current_indent * INDENTATION + directive[0] + directive_split_space * INDENTATION + directive[1]) else: indented_lines.append("")