dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles # fast
git clone https://src.adamsgaard.dk/dotfiles.git # slow
Log | Files | Refs | README | LICENSE Back to index

muttprint-groff.sh (1882B)


      1 #!/bin/sh
      2 set -e
      3 
      4 PDIR="$HOME/tmp" 
      5 
      6 mkdir -p "$PDIR"
      7 
      8 infile="$(mktemp "$PDIR"/mutt_XXXXXX)"
      9 tmpfile="$(mktemp "$PDIR"/mutt_XXXXXX)"
     10 outfile="$(mktemp "$PDIR"/mutt_XXXXXX)"
     11 mv "$outfile" "${outfile}.pdf"
     12 outfile="${outfile}.pdf"
     13 
     14 printf 'infile = %s\n' "$infile"
     15 printf 'tmpfile = %s\n' "$tmpfile"
     16 printf 'outfile = %s\n' "$outfile"
     17 
     18 cat "${1:--}" > "$infile"
     19 
     20 # join lines starting with space with previous line
     21 #sed -i '$!N;s/\n / /;P;D' "$infile"
     22 sed -i '$!N;s/\n / /;P;D' "$infile"
     23 sed -i '$!N;s/\n / /;P;D' "$infile"
     24 sed -i '$!N;s/\n / /;P;D' "$infile"
     25 sed -i '$!N;s/\n / /;P;D' "$infile"
     26 sed -i '$!N;s/\n / /;P;D' "$infile"
     27 
     28 echo "formatting $infile to groff format in $tmpfile"
     29 mail_date="$(grep '^Date: ' "$infile")"
     30 mail_from="$(grep '^From: ' "$infile")"
     31 mail_to="$(grep '^To: ' "$infile" | tr ',' '\n')"
     32 mail_cc="$(grep '^C[Cc]: ' "$infile" | tr ',' '\n')"
     33 mail_subject="$(grep '^Subject: ' "$infile")"
     34 
     35 { 
     36     [ -n "$mail_subject" ] && printf ".TL\n%s\n" "$mail_subject" | sed 's/^Subject: //'; [ -n "$mail_from" ] && printf ".AU\n%s\n" "$mail_from";
     37     [ -n "$mail_to" ] && printf ".AU\n%s\n" "$mail_to";
     38     [ -n "$mail_cc" ] && printf ".AU\n%s\n" "$mail_cc";
     39     [ -n "$mail_date" ] && printf ".AI\n%s\n" "$mail_date";
     40     printf ".LP\n";
     41     grep -v "^Date:\|^From:\|^To:\|^Subject:\|^C[Cc]:\|^X-Mailer:\|^User-Agent:" "$infile" |\
     42         sed 's/^>\s*$//' |\
     43         sed 's/ *\(---*\)/\1\n/' |\
     44         sed '$!N;s/\n \([a-z]\)/ \1/;P;D' |\
     45         sed 's/^> *\(.*\)$/.I "\1"/' |\
     46         sed 's/ > / /g' |\
     47         sed 's/\(Fra\|Dato\|Til\|Emne\): /\n\1: /g' |\
     48         sed 's/\(From\|Date\|To\|C[Cc]\|Subject\): /\n\1: /g' |\
     49         sed 's/\(Links:\)/\n\1\n/g' |\
     50         sed 's/\(References:\)/\n\1\n/g';
     51     } > "$tmpfile"
     52 
     53 echo "running groff $tmpfile $outfile"
     54 preconv "$tmpfile" | groff -ms -T pdf > "$outfile"
     55 
     56 rm "$infile" "$tmpfile"
     57 xdg-open "$outfile"