Mercurial > hg > TextShaper
annotate textshaper/whitespace.py @ 21:e6f680d25d63
migrate url2txt
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 23 Feb 2014 00:45:06 -0800 |
parents | 22c830449604 |
children | c23782a7b7ba |
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 |
20 def filename2name(text, whitespace=('_',), replacement=' '): | |
21 """ | |
22 convert filename to name | |
23 """ | |
24 | |
25 name = os.path.splitext(os.path.basenmae(text))[0] | |
26 for string in whitespace: | |
27 name = name.replace(string, replace) | |
28 return name |