annotate textshaper/whitespace.py @ 38:56fa70e2e239

STUB: textshaper/url2txt.py
author Jeff Hammel <k0scist@gmail.com>
date Thu, 03 Jul 2014 13:23:19 -0700
parents 55e0579e2143
children
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
36
55e0579e2143 STUB: textshaper/main.py textshaper/whitespace.py
Jeff Hammel <k0scist@gmail.com>
parents: 26
diff changeset
8 from .decorator import lines
8
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
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
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
20
26
c23782a7b7ba more hookups, yo
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
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
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
31 def filename2name(text, whitespace=('_',), replacement=' '):
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
32 """
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
33 convert filename to name
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
34 """
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
35
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
36 name = os.path.splitext(os.path.basenmae(text))[0]
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
37 for string in whitespace:
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
38 name = name.replace(string, replace)
22c830449604 textshaper/whitespace.py
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
39 return name