On May 24, 2008, at 3:04 PM, Bill Moseley wrote:

> But, it's somewhat of a pain to maintain two very similar by different
> files.  I'd rather have a single file that generates both.
>
> Anyone have a better approach for generating both HTML and text
> versions of the same email?

We generate an html mail and then parse the text out for the plaintext  
version.  Don't recommend it though; or rather - it'll need more work  
than we put into it to work well.  The text mails are never as neatly  
formatted as they should be.  Another missing bit is that it still  
requires links to be written out as text in the html version, so we  
end up with both versions sucking a bit.

I've been planning to reverting to just having two separate templates,  
but if you want to try just having one then something like the  
following might help you get started.

Another approach could be having the "master template" in textile or  
Markdown or some such format and use that to convert to text and html.


  - ask

     my $html = $tt->process(...);

     my @msg;
     my $parser = HTML::Parser->new(api_version => 3,
                                    handlers => { default => [EMAIL PROTECTED], 
 
"tagname,event,attr,text"],
                                                },

                                   );
     $parser->parse($html);

     my $plain_text = "";
     my $subject = "";
     my $in_subject = 0;
     for my $m (@msg) {
         if ($in_subject) {
             $subject .= $m->[3] and next if $m->[1] eq 'text';
         }
         $plain_text .= $m->[3] and next if $m->[1] eq 'text';

         $plain_text .= "\n" if $m->[0] eq 'br';

         $in_subject = 1 if $m->[1] eq 'start' and $m->[0] eq 'title';
         $in_subject = 0 if $m->[1] eq 'end'   and $m->[0] eq 'title';
     }

     $plain_text =~ s/^\s+//s;  # whitespace before the message
     $plain_text =~ s/\s+$//s;  # whitespace after the message

     $plain_text =~ s/ +(\S)/ $1/gsm;
     $plain_text =~ s/\n(\s*\n)/\n\n/g;





-- 
http://develooper.com/ - http://askask.com/



_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to