File: mhtxtenrich.pl

package info (click to toggle)
mhonarc 2.5.2-1.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,996 kB
  • ctags: 4,790
  • sloc: perl: 14,032; makefile: 172
file content (155 lines) | stat: -rw-r--r-- 4,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
##---------------------------------------------------------------------------##
##  File:
##	@(#) mhtxtenrich.pl 2.5 01/08/26 01:57:59
##  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:
##
##	    <MIMEFILTERS>
##	    text/enriched:m2h_text_enriched'filter:mhtxtenrich.pl
##	    text/richtext:m2h_text_enriched'filter:mhtxtenrich.pl
##	    </MIMEFILTERS>
##
##---------------------------------------------------------------------------##
##    MHonArc -- Internet mail-to-HTML converter
##    Copyright (C) 1997-2001	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;

##---------------------------------------------------------------------------
##	Filter routine.
##
sub filter {
    my($fields, $data, $isdecode, $args) = @_;
    my($innofill, $chunk, $ret, $charset);
    $ret  = "";
    $args = ""  unless defined($args);
    $charset = "";

    ## Grab charset parameter (if defined)
    $ctype = $fields->{'content-type'}[0] || "";
    if ($ctype =~ /charset=(\S+)/) {
	$charset = lc $1;
	$charset =~ s/['"]//g;
    }

    ## Convert specials
    $$data =~ s|&|\&amp;|gi;
    $$data =~ s|<<|\&lt;|gi;

    ## Translate text/enriched commands
    $innofill = 0;
    foreach $chunk (split(m|(</?nofill>)|i, $$data)) {
	if ($chunk =~ m|<nofill>|i) {
	    $ret .= "<PRE>";
	    $innofill = 1;
	    next;
	}
	if ($chunk =~ m|</nofill>|i) {
	    $ret .= "</PRE>";
	    $innofill = 0;
	    next;
	}
	convert_tags(\$chunk);
	if (!$innofill) {
	    $chunk =~ s|(\r?\n\s*)|&nl_seq_to_brs($1)|gie;
	}
	$ret .= $chunk;
    }

    ## Translate 8-bit characters to entity refs based on charset
    ## 		(we already did '<' and '&' characters)
    if ($charset =~ /iso-8859-([2-9]|10)/i) {
	require 'iso8859.pl';
	$ret = iso_8859::str2sgml($ret, $charset, 1);
    }

    $ret;
}

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

    $$str =~ s|<(/?)bold>|<$1B>|gi;
    $$str =~ s|<(/?)italic>|<$1I>|gi;
    $$str =~ s|<(/?)underline>|<$1U>|gi;
    $$str =~ s|<(/?)fixed>|<$1TT>|gi;
    $$str =~ s|<(/?)smaller>|<$1SMALL>|gi;
    $$str =~ s|<(/?)bigger>|<$1BIG>|gi;

    $$str =~ s|<fontfamily>\s*<param>([^<]+)</param>|<FONT face="$1">|gi;
    $$str =~ s|</fontfamily>|</FONT>|gi;
    $$str =~ s|<color>\s*<param>\s*(\S+)\s*</param>|<FONT color="$1">|gi;
    $$str =~ s|</color>|</FONT>|gi;
    $$str =~ s|<center>|<P align="center">|gi;
    $$str =~ s|</center>|</P>|gi;
    $$str =~ s|<flushleft>|<P align="left">|gi;
    $$str =~ s|</flushleft>|</P>|gi;
    $$str =~ s|<flushright>|<P align="right">|gi;
    $$str =~ s|</flushright>|</P>|gi;
    $$str =~ s|<flushboth>|<P align="both">|gi;	# Not supported in HTML
    $$str =~ s|</flushboth>|</P>|gi;
    $$str =~ s|<paraindent>\s*<param>([^<]*)</param>|<BLOCKQUOTE>|gi;
    $$str =~ s|</paraindent>|</BLOCKQUOTE>|gi;

    $$str =~ s|<excerpt>\s*(<param>([^<]*)</param>)?|<BLOCKQUOTE>|gi;
    $$str =~ s|</excerpt>|</BLOCKQUOTE>|gi;

    # Not supported commands
    $$str =~ s|<lang>\s*<param>([^<]*)</param>||gi;
    $$str =~ s|</lang>||gi;
}

##---------------------------------------------------------------------------
##	nl_seq_to_brs returns a "<BR>" string based on the number
##	on eols in a string.
##
sub nl_seq_to_brs {
    my($str) = shift;
    my($n);
    $n = $str =~ tr/\n/\n/;
    --$n;
    if ($n <= 0) {
	return " ";
    } else {
	return "<br>\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/ /\&nbsp;/g;
    $str;
}

##---------------------------------------------------------------------------
1;