Skip to content

Commit 50a3ac6

Browse files
committed
Added new option -m --multi to remove multiple whitespaces
Fixes #40
1 parent f44a366 commit 50a3ac6

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
META = {
2525
'name': 'ttws',
2626
'url': 'https://github.com/simulatino/trimtrailingwhitespaces',
27-
'version': '0.8.4',
27+
'version': '0.8.5',
2828
'description': 'Script to remove trailing whitespaces from textfiles and more.',
2929
'classifiers': CLASSIFIERS,
3030
'license': 'UNLICENSE',

ttws/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import os
2525
import textwrap
2626
import io
27+
import re
2728

2829
from pyparsing import (White, Keyword, nestedExpr, lineEnd, Suppress,
2930
ZeroOrMore, Optional, ParseException, FollowedBy,
@@ -82,15 +83,17 @@ def detecttype(filepath):
8283
return "unknown"
8384

8485

85-
def trimWhitespace(filepath, eol):
86+
def trimWhitespace(filepath, multiOpt, eol):
8687
"""Trim trailing white spaces from a given filepath."""
8788
try:
88-
with io.open(filepath, "r", encoding='utf-8-sig') as source:
89-
lines = [line.rstrip() for line in source]
90-
while len(lines) > 1 and not lines[-1]:
91-
lines.pop(-1)
92-
with io.open(filepath, "w", newline="") as target:
93-
target.write(eol.join(lines) + eol)
89+
with open(filepath, "r", encoding='utf-8-sig') as f:
90+
lines = f.readlines()
91+
with open(filepath, "w", newline=eol) as f:
92+
for line in lines:
93+
if multiOpt and not line.lstrip().startswith('//'):
94+
line = re.sub(r'(?<=\S)[\s]+', ' ', line)
95+
f.write(line.rstrip() + eol)
96+
9497
except (UnicodeDecodeError, TypeError) as err:
9598
print("\nOops! Failing to process file: %s\n"
9699
"Are you sure it is of pure ASCII or UTF8 encoding?\n"

ttws/cli.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def main(args=None):
1717

1818
# Look for optional arguments:
1919
try:
20-
opts, dirnames = getopt.getopt(args, "hvsbc", ["help", "version", "strip", "blanks", "clean",
21-
"eol="])
20+
opts, dirnames = getopt.getopt(args, "hvsbcm", ["help", "version", "strip", "blanks", "clean", "multi", "eol="])
2221
# If unknown option is given trigger the display message:
2322
except getopt.GetoptError:
2423
unknownOption(args)
@@ -30,6 +29,7 @@ def main(args=None):
3029

3130
# If help option is given display help otherwise display warning:
3231
cleanOpt = False
32+
multiOpt = False
3333
stripOpt = False
3434
eol = os.linesep
3535
for opt, arg in opts:
@@ -41,6 +41,8 @@ def main(args=None):
4141
sys.exit(0)
4242
elif opt in ("-c", "--clean"):
4343
cleanOpt = True
44+
elif opt in ("-m", "--multi"):
45+
multiOpt = True
4446
elif opt in ("-b", "--blanks"):
4547
os.system('for fn in `find -name "*.mo"`; do cat -s $fn >$fn.1; mv $fn.1 $fn; done')
4648
sys.exit(0)
@@ -73,15 +75,15 @@ def main(args=None):
7375
filetype = detecttype(filepath)
7476
if filetype is "mo" and cleanOpt is True:
7577
print("trimming and cleaning %s" % filepath)
76-
trimWhitespace(filepath, eol)
78+
trimWhitespace(filepath, multiOpt, eol)
7779
cleanAnnotation(filepath, eol)
7880
elif filetype is "mo" and stripOpt is True:
7981
print("trimming and stripping %s" % filepath)
80-
trimWhitespace(filepath, eol)
82+
trimWhitespace(filepath, multiOpt, eol)
8183
stripDocString(filepath, eol)
8284
elif filetype is "mo" or filetype is "text":
8385
print("trimming %s" % filepath)
84-
trimWhitespace(filepath, eol)
86+
trimWhitespace(filepath, multiOpt, eol)
8587
else:
8688
print("skipping file of type %s: %s" % (filetype, filepath))
8789

@@ -134,6 +136,12 @@ def usage(script_name):
134136
Only use this if your code is under version control
135137
and in combination with a careful code-diff review.
136138
139+
-m, --multi
140+
Replaces multiple inline whitespaces by one whitespace.
141+
It ignores commented lines starting with '//'
142+
CAREFUL: It will damage whitespace sensitive documentation
143+
like verbatim code snippets!
144+
137145
-b, --blanks
138146
suppress repeated empty output lines from *.mo files
139147
(This option should not be run in combination with others.)

0 commit comments

Comments
 (0)