Mercurial > hg > TextShaper
annotate textshaper/whitespace.py @ 47:6c7ca72777af
add a test all convenience
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 16 May 2015 19:02:03 -0700 |
parents | 55e0579e2143 |
children |
rev | line source |
---|---|
7
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 text shaping functionality having to do with whitespace |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
8 | 7 import os |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
8 from .decorator import lines |
8 | 9 |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
10 __all__ = ['normalize', 'underscore', 'filename2name'] |
7
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 def normalize(text, separator=None, joiner=None): |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 strips text and |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 replace multiple whitespace occurance with single occurance |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 if joiner is None: |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 joiner = ' ' if separator is None else separator |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 return joiner.join(text.strip().split(separator)) |
8 | 20 |
26 | 21 |
36
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
22 @lines |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
23 def underscore(text, replacement='_', split=None, strip=str.rstrip): |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
24 retval = [] |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
25 for line in text: |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
26 if strip: |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
27 strip(line) |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
28 retval.append(replacement.join(line.split(split))) |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
29 return retval |
55e0579e2143
STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents:
26
diff
changeset
|
30 |
8 | 31 def filename2name(text, whitespace=('_',), replacement=' '): |
32 """ | |
33 convert filename to name | |
34 """ | |
35 | |
36 name = os.path.splitext(os.path.basenmae(text))[0] | |
37 for string in whitespace: | |
38 name = name.replace(string, replace) | |
39 return name |