Created
July 18, 2011 20:36
-
-
Save laat/1090583 to your computer and use it in GitHub Desktop.
edits mails for easier replying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#-*- coding: utf-8 | |
""" | |
This script formats emails for easier replying | |
Strips out greetings and signatures | |
usage: | |
$ cat mail.txt | python script.py | |
""" | |
greetings = ["Hello", "Hei" "Hei!"] | |
greetouts = ["Regards", "Hilsen", "MVH"] | |
def is_greeating(line, greetings): | |
""" if lines matches greetings, return True """ | |
for greet in greetings: | |
if re.match("^> %s$" % greet, line): | |
return True | |
return False | |
def main() | |
original_mail = [] | |
if not sys.stdin.isatty(): | |
original_mail = sys.stdin.readlines() | |
purged_mail = [] | |
signature_found = False | |
for line in original_mail: | |
# non_quoted lines, leave them | |
if not line.startswith(">"): | |
purged_mail.append(line) | |
continue | |
# remove greegings | |
if not saw_greeting: | |
if is_greeating(line, greetings): | |
saw_greeting = True | |
continue | |
# remove greetouts | |
if not saw_greetout: | |
if is_greeating(line, greetouts): | |
saw_greeting = True | |
continue | |
#find indentation level | |
pref, context = re.match("^([>\s]+)(.*)$", line).groups() | |
indents = pref.count(">") | |
# remove signatures | |
if re.match("^[> ]+ ?-- ?$", line): | |
signature_found = indents | |
continue | |
if signature_found == indents: | |
continue | |
else: | |
signature_found = False | |
#> should not be spaced | |
pref = ">"*indents | |
purged_mail.append(pref +" "+context+"\n") | |
prev_indent = indents | |
print "".join(purged_mail) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment