<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">##---------------------------------------------------------------------------##
##  File:
##	$Id: mhtxtenrich.pl,v 2.9 2003/01/18 03:10:35 ehood Exp $
##  Author:
##      Earl Hood       mhonarc@mhonarc.org
##  Description:
##	Library defines a routine for MHonArc to filter text/enriched
##	data.
##
##	Filter routine can be registered with the following:
##
##	    &lt;MIMEFILTERS&gt;
##	    text/enriched;m2h_text_enriched::filter;mhtxtenrich.pl
##	    text/richtext;m2h_text_enriched::filter;mhtxtenrich.pl
##	    &lt;/MIMEFILTERS&gt;
##
##---------------------------------------------------------------------------##
##    MHonArc -- Internet mail-to-HTML converter
##    Copyright (C) 1997-2002	Earl Hood, mhonarc@mhonarc.org
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
##    02111-1307, USA
##---------------------------------------------------------------------------##

package m2h_text_enriched;

my %enriched_tags = (
    'bigger' =&gt; 1,
    'bold' =&gt; 1,
    'center' =&gt; 1,
    'color' =&gt; 1,
    'comment' =&gt; 1,
    'excerpt' =&gt; 1,
    'fixed' =&gt; 1,
    'flushboth' =&gt; 1,
    'flushleft' =&gt; 1,
    'flushright' =&gt; 1,
    'fontfamily' =&gt; 1,
    'indent' =&gt; 1,
    'indentright' =&gt; 1,
    'italic' =&gt; 1,
    'lang' =&gt; 1,
    'lt' =&gt; 1,
    'nl' =&gt; 1,
    'nofill' =&gt; 1,
    'paraindent' =&gt; 1,
    'param' =&gt; 1,
    'signature' =&gt; 1,
    'smaller' =&gt; 1,
    'subscript' =&gt; 1,
    'superscript' =&gt; 1,
    'underline' =&gt; 1,
);

my %special_to_char = (
    'lt'  =&gt; '&lt;',
    'gt'  =&gt; '&gt;',
);

##---------------------------------------------------------------------------
##	Filter routine.
##	XXX: Need to update this filter.  However, does anyone still use
##	     text/enriched anymore.
##
sub filter {
    my($fields, $data, $isdecode, $args) = @_;
    my($innofill, $chunk);
    my $charset = $fields-&gt;{'x-mha-charset'};
    my($charcnv, $real_charset_name) =
	    readmail::MAILload_charset_converter($charset);
    my $ret = "";
    $args   = ""  unless defined($args);

    ## Get content-type
    my($ctype) = $fields-&gt;{'content-type'}[0] =~ m%^\s*([\w\-\./]+)%;
    my $richtext = $ctype =~ /\btext\/richtext\b/i;

    if (defined($charcnv) &amp;&amp; defined(&amp;$charcnv)) {
	$$data = &amp;$charcnv($$data, $real_charset_name);
    } else {
	mhonarc::htmlize($data);
	warn qq/\n/,
	     qq/Warning: Unrecognized character set: $charset\n/,
	     qq/         Message-Id: &lt;$mhonarc::MHAmsgid&gt;\n/,
	     qq/         Message Number: $mhonarc::MHAmsgnum\n/
		unless ($charcnv eq '-decode-');
    }
    ## Fixup any EOL mess
    $$data =~ s/\r?\n/\n/g;
    $$data =~ s/\r/\n/g;

    # translate back &lt;&gt;'s for tag processing
    $$data =~ s/&amp;([lg]t);/$special_to_char{$1}/g;

    ## Convert specials
    if (!$richtext) {
	$$data =~ s/&lt;&lt;/\&amp;lt;/g;
    }

    ## Make sure only non-enriched tags are escaped
    $$data =~ s{&lt;(/?)([^&gt;]*)&gt;}
    {
	my $eot = $1;
	my $tag = lc $2;
	$tag =~ s/\s+//g;
	($enriched_tags{$tag}) ? '&lt;'.$eot.$tag.'&gt;' : '&amp;lt;'.$eot.$tag.'&amp;gt;';
    }gexs;

    $innofill = 0;
    foreach $chunk (split(m|(&lt;/?nofill&gt;)|i, $$data)) {
	if ($chunk =~ m|&lt;nofill&gt;|i) {
	    $ret .= '&lt;pre&gt;';
	    $innofill = 1;
	    next;
	}
	if ($chunk =~ m|&lt;/nofill&gt;|i) {
	    $ret .= '&lt;/pre&gt;';
	    $innofill = 0;
	    next;
	}
	convert_tags(\$chunk, $richtext);
	if (!$richtext &amp;&amp; !$innofill) {
	    $chunk =~ s/(\n\s*)/&amp;nl_seq_to_brs($1)/ge;
	}
	$ret .= $chunk;
    }
    $ret;
}

##---------------------------------------------------------------------------
##	convert_tags translates text/enriched commands to HTML tags.
##
sub convert_tags {
    my $str  = shift;
    my $richtext = shift;

    $$str =~ s{&lt;comment\s*&gt;.*?&lt;/comment\s*&gt;}{}gis;

    $$str =~ s{&lt;(/?)bold\s*&gt;}{&lt;$1b&gt;}gi;
    $$str =~ s{&lt;(/?)italic\s*&gt;}{&lt;$1i&gt;}gi;
    $$str =~ s{&lt;(/?)underline\s*&gt;}{&lt;$1u&gt;}gi;
    $$str =~ s{&lt;(/?)fixed\s*&gt;}{&lt;$1tt&gt;}gi;
    $$str =~ s{&lt;(/?)smaller\s*&gt;}{&lt;$1small&gt;}gi;
    $$str =~ s{&lt;(/?)bigger\s*&gt;}{&lt;$1big&gt;}gi;
    $$str =~ s{&lt;(/?)signature\s*&gt;}{&lt;$1pre&gt;}gi;

    $$str =~ s{&lt;fontfamily\s*&gt;\s*&lt;param\s*&gt;([^&lt;]+)&lt;/param\s*&gt;}
	      {&lt;font face="$1"&gt;}gix;
    $$str =~ s|&lt;/fontfamily\s*&gt;|&lt;/font&gt;|gi;
    $$str =~ s{&lt;color\s*&gt;\s*&lt;param\s*&gt;\s*(\S+)\s*&lt;/param\s*&gt;}
	      {&lt;font color="$1"&gt;}gix;
    $$str =~ s|&lt;/color\s*&gt;|&lt;/font&gt;|gi;
    $$str =~ s|&lt;center\s*&gt;|&lt;p align="center"&gt;|gi;
    $$str =~ s|&lt;/center\s*&gt;|&lt;/p&gt;|gi;
    $$str =~ s|&lt;flushleft\s*&gt;|&lt;p align="left"&gt;|gi;
    $$str =~ s|&lt;/flushleft\s*&gt;|&lt;/p&gt;|gi;
    $$str =~ s|&lt;flushright\s*&gt;|&lt;p align="right"&gt;|gi;
    $$str =~ s|&lt;/flushright\s*&gt;|&lt;/p&gt;|gi;
    $$str =~ s|&lt;flushboth\s*&gt;|&lt;p align="justify"&gt;|gi;
    $$str =~ s|&lt;/flushboth\s*&gt;|&lt;/p&gt;|gi;
    $$str =~ s|&lt;paraindent\s*&gt;\s*&lt;param\s*&gt;([^&lt;]*)&lt;/param\s*&gt;|&lt;blockquote&gt;|gi;
    $$str =~ s|&lt;/paraindent\s*&gt;|&lt;/blockquote&gt;|gi;

    $$str =~ s|&lt;excerpt\s*&gt;\s*(&lt;param\s*&gt;([^&lt;]*)&lt;/param\s*&gt;)?|&lt;blockquote&gt;|gi;
    $$str =~ s|&lt;/excerpt\s*&gt;|&lt;/blockquote&gt;|gi;

    $$str =~ s|&lt;lang\s*&gt;\s*&lt;param\s*&gt;([^&lt;]*)&lt;/param\s*&gt;|&lt;div lang="$1"&gt;|gi;
    $$str =~ s|&lt;/lang\s*&gt;|&lt;/div&gt;|gi;

    # richtext commands
    $$str =~ s{&lt;(/?)subscript\s*&gt;}{&lt;$1sub&gt;}gi;
    $$str =~ s{&lt;(/?)superscript\s*&gt;}{&lt;$1sup&gt;}gi;
    $$str =~ s{&lt;lt\s*&gt;}{&amp;lt;}gi;
    $$str =~ s{&lt;np\s*&gt;}{\f}gi;
    $$str =~ s{&lt;paragraph\s*&gt;}{&lt;p&gt;}gi;
    $$str =~ s{&lt;/paragraph\s*&gt;\n?}{&lt;/p&gt;}gis;
    $$str =~ s{&lt;indent\s*&gt;}{&lt;p style="margin-left: 1em;"&gt;}gi;
    $$str =~ s{&lt;/indent\s*&gt;}{&lt;/p&gt;}gi;
    $$str =~ s{&lt;indentright\s*&gt;}{&lt;p style="margin-right: 1em;"&gt;}gi;
    $$str =~ s{&lt;/indentright\s*&gt;}{&lt;/p&gt;}gi;

    if ($richtext) {
	$$str =~ s{&lt;nl\s*&gt;\n?}{&lt;br&gt;}gis;
    } else {
	$$str =~ s{&lt;nl\s*&gt;}{}gis;
    }

    # Cleanup bad tags
    $$str =~ s{&lt;/?(?:para(?:m|indent)|excerpt|lang|color|fontfamily)\s*&gt;}{}g;
}

##---------------------------------------------------------------------------
##	nl_seq_to_brs returns a "&lt;BR&gt;" string based on the number
##	of eols in a string.
##
sub nl_seq_to_brs {
    my($str) = shift;
    my($n);
    $n = $str =~ tr/\n/\n/;
    --$n;
    if ($n &lt;= 0) {
	return " ";
    } else {
	return "&lt;br&gt;\n" x $n;
    }
}

##---------------------------------------------------------------------------
##	preserve_space returns a string with all spaces and tabs
##	converted to nbsps.
##
sub preserve_space {
    my($str) = shift;
    1 while
      $str =~ s/^([^\t]*)(\t+)/$1 . ' ' x (length($2) * 8 - length($1) % 8)/e;
    $str =~ s/ /\&amp;nbsp;/g;
    $str;
}

##---------------------------------------------------------------------------
1;
</pre></body></html>