Last active
July 13, 2017 00:37
-
-
Save armancohan/7ed54bde2a12b726ae83467b4629d6c0 to your computer and use it in GitHub Desktop.
A simple script to change the format of the text so all lines would fit within a limited length (for python long comments to conform to pep8).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import sys | |
| def format_block(txt, maxlen=79, indent=2): | |
| lines = txt.replace('\n',' ') | |
| res = [] | |
| line = '' | |
| tokens = [e for e in lines.split(' ') if e] | |
| i=0 | |
| lineno=0 | |
| limit = maxlen - 2 * indent - 3 | |
| while i < len(tokens): | |
| line += tokens[i] + ' ' | |
| if len(line) < limit: | |
| i += 1 | |
| continue | |
| limit = maxlen - 2 * indent - 3 | |
| limit = limit - 3 if lineno==0 else limit | |
| res.append(line[:-len(tokens[i] + ' ')].strip()) | |
| lineno+=1 | |
| line = tokens[i] + ' ' | |
| i+=1 | |
| res.append(line) | |
| return '\n'.join(res) | |
| print(format_block(sys.argv[1].replace('"""', ''))) | |
| # usage ./comment_block.py """sample text""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment