annotate python/quote.py @ 916:82763d37ab09

add web2 convenience script
author Jeff Hammel <k0scist@gmail.com>
date Thu, 30 May 2024 18:56:59 -0700
parents a075f8a93183
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a075f8a93183 python/quote.py
Jeff Hammel <jhammel@mozilla.com>
parents: 271
diff changeset
7 # TODO:
a075f8a93183 python/quote.py
Jeff Hammel <jhammel@mozilla.com>
parents: 271
diff changeset
8 # - combine with quotemail...wth?
a075f8a93183 python/quote.py
Jeff Hammel <jhammel@mozilla.com>
parents: 271
diff changeset
9 # -> textshaper
a075f8a93183 python/quote.py
Jeff Hammel <jhammel@mozilla.com>
parents: 271
diff changeset
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()