Mercurial > hg > TextShaper
annotate textshaper/whitespace.py @ 34:88a69d587326
round1 of commands
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 02 Mar 2014 15:33:07 -0800 |
parents | c23782a7b7ba |
children | 55e0579e2143 |
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 |
8 | |
7
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 __all__ = ['normalize'] |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 def normalize(text, separator=None, joiner=None): |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 strips text and |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 replace multiple whitespace occurance with single occurance |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 """ |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 if joiner is None: |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 joiner = ' ' if separator is None else separator |
2693d9f8ed8c
add file for dealing with whitespace
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 return joiner.join(text.strip().split(separator)) |
8 | 19 |
26 | 20 |
8 | 21 def filename2name(text, whitespace=('_',), replacement=' '): |
22 """ | |
23 convert filename to name | |
24 """ | |
25 | |
26 name = os.path.splitext(os.path.basenmae(text))[0] | |
27 for string in whitespace: | |
28 name = name.replace(string, replace) | |
29 return name |