diff --git a/default-application.note b/default-application.note new file mode 100644 index 0000000..fd5334a --- /dev/null +++ b/default-application.note @@ -0,0 +1,5 @@ + 520 xdg-open 2024-07-01_myCSS.png + 521 xdg-mime default viewnior.desktop image/png + 522 xdg-open 2024-07-01_myCSS.png + 523 history + 524 ls /usr/share/applications diff --git a/pymuttprint.py b/pymuttprint.py new file mode 100755 index 0000000..a8281b9 --- /dev/null +++ b/pymuttprint.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python2 + +import re +import sys +import email +from os import chdir +from os.path import join, abspath, dirname +from subprocess import Popen, PIPE, STDOUT + +BASEDIR = dirname(abspath(__file__)) + +try: + chdir("/tmp") +except: + sys.exit("Couldn't chdir to /tmp") + +m = email.message_from_file( sys.stdin ) +body = '' +attachments = [] + +for part in m.walk(): + ct = part.get_content_type() + cd = part.get('Content-Disposition') + if ct == 'text/plain': + body += part.get_payload(decode=True) + else: + fn = part.get_filename() + if fn is not None: attachments.append( fn ) + +TEX_REPLACE = { # not an exhaustive list + "<" : r"\textless ", + ">" : r"\textgreater ", + "&" : r"\&", + "$" : r"\$", + "#" : r"\#", + "%" : r"\%", + "_" : r"\_", + "{" : r"\{", + "}" : r"\}", + "^" : r"^{}", + #r"\\" : r"\\ \relax " # see: http://tex.stackexchange.com/questions/64098/brackets-in-first-column-in-table-give-missing-number-treated-as-zero-error +} + + +def texify(s): + for c in TEX_REPLACE.keys(): + s = s.replace(c, TEX_REPLACE[c]) + return s + +to = m.get('to','') +date = m.get('date','') +ffrom = m.get('from','') +subject = m.get('subject','') +body = texify( re.sub(r'^(.+)\n|\r|\n\r$', r"\1\\\\", body, flags=re.MULTILINE) ) + +# +attachments = [texify(a) for a in attachments] + +template = open(join(BASEDIR, 'template.tex'), 'r').read() % ( + date, + to, + ffrom, + subject, + len( attachments ), + ', '.join( attachments ), + body +) + +p = Popen(['xelatex'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) +tex_log = p.communicate(input=template)[0] + +if p.returncode > 0: + print( tex_log) +exit(p.returncode) + +# for debugging: +#print m +#print body +#print template diff --git a/template.tex b/template.tex new file mode 100644 index 0000000..7882475 --- /dev/null +++ b/template.tex @@ -0,0 +1,23 @@ +\documentclass{report} +\usepackage{fullpage} +\usepackage[top=.5in, bottom=.75in, left=.75in, right=.75in]{geometry} +\usepackage{fontspec} +\usepackage{listings} +\setmainfont{Monaco} +\setsansfont{Monaco} +\setmonofont{Monaco} + +\begin{document} +\setlength{\parindent}{0pt} +\parindent=0pt + +Date: %s\\ +To: %s\\ +From: %s\\ +Subject: %s\\ +Attachment(s): %s %s\\ + +%s + + +\end{document}