Skip to content

Commit

Permalink
Create new function to handle line spacing on save.
Browse files Browse the repository at this point in the history
  • Loading branch information
theengineear committed Dec 26, 2014
1 parent aefc816 commit 026991b
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,31 @@ def get_init_code(leaf, language):
return ""


def fix_line_spacing(code):
"""
Removing beginning/trailing blank lines and allow max of two blank lines.
:param code:
:return:
"""
code_lines = code.splitlines()

# remove beginning blank lines
while not code_lines[0]:
code_lines.pop(0)

# remove ending blank lines
while not code_lines[-1]:
code_lines.pop()

# allow max of two consecutive blank lines
code = '\n'.join(code_lines)
while '\n\n\n' in code:
code = code.replace('\n\n\n', '\n\n')

return code


def save_code(code, leaf, language, mode, newline=True):
if mode == 'documentation':
leaf_folder = os.path.join(dirs['run'],
Expand All @@ -881,12 +906,7 @@ def save_code(code, leaf, language, mode, newline=True):
code_path = os.path.join(code_folder, filename.replace("-", "_"))
else:
raise Exception("mode: 'execution' | 'documentation' | 'exception'")
code_lines = code.splitlines()
while not code_lines[0]:
code_lines.pop(0)
while not code_lines[-1]:
code_lines.pop()
code = '\n'.join(code_lines)
code = fix_line_spacing(code)
if not os.path.exists(code_folder):
os.makedirs(code_folder)
with open(code_path, 'w') as f:
Expand Down

0 comments on commit 026991b

Please sign in to comment.