Mercurial > hg > TextShaper
comparison textshaper/indent.py @ 9:71fb16088d54
add file for indentation; wth did my work go??? :(
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 17 Jan 2014 18:17:59 -0800 |
parents | |
children | 466386702968 |
comparison
equal
deleted
inserted
replaced
8:22c830449604 | 9:71fb16088d54 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 """ | |
4 indentation of text blocks | |
5 """ | |
6 | |
7 import optparse | |
8 import os | |
9 import sys | |
10 | |
11 def indent(text, indentation=4, space=' ', strict=False): | |
12 """ | |
13 indent a block of text | |
14 | |
15 text -- lines of text to indent | |
16 indentation -- number of spaces to indent | |
17 space -- what to indent with | |
18 strict -- whether to enforce required whitespace for negative indentation | |
19 """ | |
20 | |
21 if not indentation: | |
22 # nothing to do | |
23 return text | |
24 | |
25 if indentation > 0: | |
26 retval = [space * indentation + line for line in text] | |
27 else: | |
28 # negative indentation | |
29 indentation = -indentation | |
30 retval = [] | |
31 for line in text: | |
32 prefix = line[:indentation] | |
33 for index, char in enumerate(prefix): | |
34 if not char == space: | |
35 if strict: | |
36 raise AssertionError("Found non-'%s' charcter at column %d for indentation -%d" % (space, index, indentation)) | |
37 break | |
38 else: | |
39 index = indentation | |
40 retval.append(line[index:]) | |
41 | |
42 def main(args=sys.argv[1:]): | |
43 | |
44 usage = '%prog [options]' | |
45 parser = optparse.OptionParser(usage=usage, description=__doc__) | |
46 options, args = parser.parse_args(args) | |
47 | |
48 | |
49 if __name__ == '__main__': | |
50 main() |