Mercurial > hg > config
annotate python/quote.py @ 794:e059c4e85d9f
add docker cleanup helper
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 19 Oct 2016 09:04:34 -0700 |
parents | a075f8a93183 |
children |
rev | line source |
---|---|
271
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 quote text |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """ |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
517 | 7 # TODO: |
8 # - combine with quotemail...wth? | |
9 # -> textshaper | |
10 | |
271
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 import sys |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 import textwrap |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 from subprocess import check_output as call |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 def quote(text, prefix='> ', width=69): |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 """ |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 returns quoted text |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 - prefix: string to prepend quote |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 - width: final width (emacs wraps at 70) |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 """ |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 width -= len(prefix) # subtract the prefix |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 text = text.strip() # remove surrounding whitespace |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 lines = [] |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 for line in text.splitlines(): |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 line = line.strip() |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 lines.extend(textwrap.wrap(line, width)) |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 return '\n'.join(['%s%s' % (prefix, line) |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 for line in lines]) |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 def main(args=sys.argv[1:]): |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 text = sys.stdin.read() |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 sys.stdout.write(quote(text)) |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 if __name__ == '__main__': |
2454d5a1728b
add a email-esque quoting script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 main() |