#!/usr/bin/perl # menu.pl a doc menu reformatter use MIME::Parser; use Date::Parse; use POSIX qw(strftime); my $dir="menu"; # working directory my $out_dir="htdocs"; # output directory # use MIME::Parser to output the attachment to MENU.doc my $parser=new MIME::Parser; $parser->output_to_core(1); my $entity=$parser->parse(\*STDIN); for(@{$entity->{ME_Parts}}){ if($_->{ME_Bodyhandle}->{MB_Binmode}){ open(FH,">$dir/MENU.doc"); print FH join "",@{$_->{ME_Bodyhandle}->{MBC_Data}}; close FH; last; } } exit if !-e "$dir/MENU.doc"; # exit if no menu found. # shell out to wvText to convert doc to txt. system("/usr/local/bin/wvText $dir/MENU.doc $dir/menu.txt") || die; # what day's lunch is it? my $date; for(@{$entity->{mail_inet_head}->{mail_hdr_list}}){ if(/^Date: (.*?\d{4}).*$/){ $date=strftime"%A %B %d %Y",localtime(str2time($1)); last; } }; # start of html $menu=<Canteen Menu Menu for $date
EOM # read the translated file open(MENU,"$dir/menu.txt"); while(){ # convert to unix format and tidy s#\cM##gs; s#^\s+##gs; chomp; next if !$_; # underline headings (sandwiches/soup/main dishes etc). s#.*(?:sandwiches|soup(?: of the day|:)|(?:main (?:dish(?:es)? (?!prices))|event)|salads|toasties|desserts|naughties).*#
$&#is; # add it to the menu $menu.= "$_
$/"; } close MENU; # close html $menu.=""; # put prices after the dish rather than on next line #(in uk so price in £'s) $menu=~s#
\n£# - £#sg; my $menu_file="$out_dir/menu.html"; # print output open(FH,">$menu_file"), print FH $menu; close FH; chmod 0644,$menu_file; # clean up temporary files made by or used by wvText unlink "$dir/MENU.doc"; unlink "$dir/menu.txt"; system("/bin/rm /tmp/wv* 2>/dev/null");