From 5dd8f70e567f4540e75b0cac0c3a4fbc0769b137 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Mon, 18 Jul 2016 18:19:14 -0700 Subject: [PATCH] better newline logic --- certbot-nginx/certbot_nginx/parser.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/certbot-nginx/certbot_nginx/parser.py b/certbot-nginx/certbot_nginx/parser.py index 57ea2db56..e8fd50452 100644 --- a/certbot-nginx/certbot_nginx/parser.py +++ b/certbot-nginx/certbot_nginx/parser.py @@ -529,14 +529,19 @@ COMMENT = [' ', '#', ' managed by Certbot'] def _comment_directive(block, location): """Add a comment to the end of the line at location.""" + if len(block) > location + 1: # there is a block after us + next_entry = block[location + 1] + else: + # we're at the end of the block, pretend there's a newline after us; it will actually be added later in + # add_directives + next_entry = "\n" + if isinstance(next_entry, list): + if COMMENT[-1] in next_entry[-1]: + return + next_entry = next_entry.spaced[0] block.insert(location + 1, COMMENT[:]) - - if len(block) > location + 2: # there is a block after us - next_entry = block[location + 2] - if isinstance(next_entry, list): - next_entry = next_entry.spaced[0] - if "\n" not in next_entry: - block.insert(location + 2, '\n') + if "\n" not in next_entry: + block.insert(location + 2, '\n') def _add_directive(block, directive, replace):