Created
May 24, 2017 10:43
-
-
Save n-st/6851167de1331ce6c457f27740f01a79 to your computer and use it in GitHub Desktop.
Undo wrapping at 78-character line length
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/python3 | |
import sys | |
import fileinput | |
lines = list(fileinput.input()) | |
lines = [l.strip('\r\n') for l in lines] | |
for i, l in enumerate(lines): | |
# We need to account for 8-char-wide tabulators when calculating our line | |
# length | |
sanitized_line = l.replace('\t', ' '*8) | |
# Is there a next line? | |
if i+1 < len(lines): | |
sanitized_next_line = lines[i+1].replace('\t', ' '*8) | |
else: | |
sanitized_next_line = '' | |
next_line_first_word = sanitized_next_line.split(' ', 1)[0] | |
if next_line_first_word != '': | |
extended_line = sanitized_line + ' ' + next_line_first_word | |
else: | |
extended_line = sanitized_line | |
if len(sanitized_line) <= 78 and len(extended_line) > 74: | |
# This line was wrapped due to 78-char limit => unwrap it! | |
sys.stdout.write(l + ' ') | |
else: | |
sys.stdout.write(l + '\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment