#!/usr/bin/perl -w # # mail2html-invert - converts a mail to HTML and inverts the quoting # # Copyright © 2008 Penny Leach # Released under the GPLv2 # use strict; use warnings; use Text::Quoted; use HTML::Entities; # configurable bits my $printblockquotes = 1; # change this to 0 if you want

instead of

etc my $title = 'email'; # this could helpfully extract the names in the email or such my $raw; # raw body of message my $bodystarted; # flag for end of headers my @newstructure; # post-processed structure my $maxlevel = 0; # for inverting indenting print < $title

EOH while () { if (/^$/ && !$bodystarted) { # empty line, start processing body after this $bodystarted = 1; print "

\n"; # end the paragraph containing headers next; } unless ($bodystarted) { # if we're still processing headers, just print them chomp; print encode_entities($_) . "
\n"; next; } $raw .= $_; } # convert the raw body to some whack datastructure provide but Text::Quoted my $structure = extract($raw); foreach my $item (@$structure) { # process the weird format we have and turn it into something sensible and flatter handleitem($item); } # each 'item' is basically one lump of text with an indent level, which we invert foreach my $item (@newstructure) { my $invertedlevel = $maxlevel - $item->{level}; my $escaped = encode_entities($item->{text}); unless ($printblockquotes) { print '

' . $escaped . '

' . "\n"; next; } my $pre = ''; my $post = ''; for (1..$invertedlevel) { $pre .= '
'; $post .= '
'; } print $pre . '

' . $escaped . '

' . $post . "\n"; } print " \n\n"; # -------------------------------------------------- # extract gives us back a weird structure that contains an array of either hashes or arrays # flatten it and add it into @newstructure with an indent level sub handleitem { my $item = shift; my $level = shift || 0; $maxlevel = $level if $maxlevel < $level; if (ref $item eq 'HASH') { return if $item->{empty}; push @newstructure, { 'text' => $item->{text}, 'level' => $level}; } elsif (ref $item eq 'ARRAY') { foreach my $newitem (@$item) { handleitem($newitem, $level+1); } } }