annotate textshaper/whitespace.py @ 32:929a5e97af3e

prestrip text unless specified
author Jeff Hammel <k0scist@gmail.com>
date Sun, 23 Feb 2014 14:56:17 -0800
parents c23782a7b7ba
children 55e0579e2143
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
7 import os
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
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
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
19
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
20
8
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
21 def filename2name(text, whitespace=('_',), replacement=' '):
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
22 """
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
23 convert filename to name
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
24 """
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
25
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
26 name = os.path.splitext(os.path.basenmae(text))[0]
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
27 for string in whitespace:
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
28 name = name.replace(string, replace)
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
29 return name