Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | ##---------------------------------------------------------------------------## |
2 | ## File: | |
3 | ## $Id: mhtxtenrich.pl,v 2.5 2001/08/25 19:57:59 ehood Exp $ | |
4 | ## Author: | |
5 | ## Earl Hood mhonarc@mhonarc.org | |
6 | ## Description: | |
7 | ## Library defines a routine for MHonArc to filter text/enriched | |
8 | ## data. | |
9 | ## | |
10 | ## Filter routine can be registered with the following: | |
11 | ## | |
12 | ## <MIMEFILTERS> | |
13 | ## text/enriched:m2h_text_enriched'filter:mhtxtenrich.pl | |
14 | ## text/richtext:m2h_text_enriched'filter:mhtxtenrich.pl | |
15 | ## </MIMEFILTERS> | |
16 | ## | |
17 | ##---------------------------------------------------------------------------## | |
18 | ## MHonArc -- Internet mail-to-HTML converter | |
19 | ## Copyright (C) 1997-2001 Earl Hood, mhonarc@mhonarc.org | |
20 | ## | |
21 | ## This program is free software; you can redistribute it and/or modify | |
22 | ## it under the terms of the GNU General Public License as published by | |
23 | ## the Free Software Foundation; either version 2 of the License, or | |
24 | ## (at your option) any later version. | |
25 | ## | |
26 | ## This program is distributed in the hope that it will be useful, | |
27 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
28 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
29 | ## GNU General Public License for more details. | |
30 | ## | |
31 | ## You should have received a copy of the GNU General Public License | |
32 | ## along with this program; if not, write to the Free Software | |
33 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
34 | ## 02111-1307, USA | |
35 | ##---------------------------------------------------------------------------## | |
36 | ||
37 | package m2h_text_enriched; | |
38 | ||
39 | ##--------------------------------------------------------------------------- | |
40 | ## Filter routine. | |
41 | ## | |
42 | sub filter { | |
43 | my($fields, $data, $isdecode, $args) = @_; | |
44 | my($innofill, $chunk, $ret, $charset); | |
45 | $ret = ""; | |
46 | $args = "" unless defined($args); | |
47 | $charset = ""; | |
48 | ||
49 | ## Grab charset parameter (if defined) | |
50 | $ctype = $fields->{'content-type'}[0] || ""; | |
51 | if ($ctype =~ /charset=(\S+)/) { | |
52 | $charset = lc $1; | |
53 | $charset =~ s/['"]//g; | |
54 | } | |
55 | ||
56 | ## Convert specials | |
57 | $$data =~ s|&|\&|gi; | |
58 | $$data =~ s|<<|\<|gi; | |
59 | ||
60 | ## Translate text/enriched commands | |
61 | $innofill = 0; | |
62 | foreach $chunk (split(m|(</?nofill>)|i, $$data)) { | |
63 | if ($chunk =~ m|<nofill>|i) { | |
64 | $ret .= "<PRE>"; | |
65 | $innofill = 1; | |
66 | next; | |
67 | } | |
68 | if ($chunk =~ m|</nofill>|i) { | |
69 | $ret .= "</PRE>"; | |
70 | $innofill = 0; | |
71 | next; | |
72 | } | |
73 | convert_tags(\$chunk); | |
74 | if (!$innofill) { | |
75 | $chunk =~ s|(\r?\n\s*)|&nl_seq_to_brs($1)|gie; | |
76 | } | |
77 | $ret .= $chunk; | |
78 | } | |
79 | ||
80 | ## Translate 8-bit characters to entity refs based on charset | |
81 | ## (we already did '<' and '&' characters) | |
82 | if ($charset =~ /iso-8859-([2-9]|10)/i) { | |
83 | require 'iso8859.pl'; | |
84 | $ret = iso_8859::str2sgml($ret, $charset, 1); | |
85 | } | |
86 | ||
87 | $ret; | |
88 | } | |
89 | ||
90 | ##--------------------------------------------------------------------------- | |
91 | ## convert_tags translates text/enriched commands to HTML tags. | |
92 | ## | |
93 | sub convert_tags { | |
94 | my($str) = shift; | |
95 | ||
96 | $$str =~ s|<(/?)bold>|<$1B>|gi; | |
97 | $$str =~ s|<(/?)italic>|<$1I>|gi; | |
98 | $$str =~ s|<(/?)underline>|<$1U>|gi; | |
99 | $$str =~ s|<(/?)fixed>|<$1TT>|gi; | |
100 | $$str =~ s|<(/?)smaller>|<$1SMALL>|gi; | |
101 | $$str =~ s|<(/?)bigger>|<$1BIG>|gi; | |
102 | ||
103 | $$str =~ s|<fontfamily>\s*<param>([^<]+)</param>|<FONT face="$1">|gi; | |
104 | $$str =~ s|</fontfamily>|</FONT>|gi; | |
105 | $$str =~ s|<color>\s*<param>\s*(\S+)\s*</param>|<FONT color="$1">|gi; | |
106 | $$str =~ s|</color>|</FONT>|gi; | |
107 | $$str =~ s|<center>|<P align="center">|gi; | |
108 | $$str =~ s|</center>|</P>|gi; | |
109 | $$str =~ s|<flushleft>|<P align="left">|gi; | |
110 | $$str =~ s|</flushleft>|</P>|gi; | |
111 | $$str =~ s|<flushright>|<P align="right">|gi; | |
112 | $$str =~ s|</flushright>|</P>|gi; | |
113 | $$str =~ s|<flushboth>|<P align="both">|gi; # Not supported in HTML | |
114 | $$str =~ s|</flushboth>|</P>|gi; | |
115 | $$str =~ s|<paraindent>\s*<param>([^<]*)</param>|<BLOCKQUOTE>|gi; | |
116 | $$str =~ s|</paraindent>|</BLOCKQUOTE>|gi; | |
117 | ||
118 | $$str =~ s|<excerpt>\s*(<param>([^<]*)</param>)?|<BLOCKQUOTE>|gi; | |
119 | $$str =~ s|</excerpt>|</BLOCKQUOTE>|gi; | |
120 | ||
121 | # Not supported commands | |
122 | $$str =~ s|<lang>\s*<param>([^<]*)</param>||gi; | |
123 | $$str =~ s|</lang>||gi; | |
124 | } | |
125 | ||
126 | ##--------------------------------------------------------------------------- | |
127 | ## nl_seq_to_brs returns a "<BR>" string based on the number | |
128 | ## on eols in a string. | |
129 | ## | |
130 | sub nl_seq_to_brs { | |
131 | my($str) = shift; | |
132 | my($n); | |
133 | $n = $str =~ tr/\n/\n/; | |
134 | --$n; | |
135 | if ($n <= 0) { | |
136 | return " "; | |
137 | } else { | |
138 | return "<br>\n" x $n; | |
139 | } | |
140 | } | |
141 | ||
142 | ##--------------------------------------------------------------------------- | |
143 | ## preserve_space returns a string with all spaces and tabs | |
144 | ## converted to nbsps. | |
145 | ## | |
146 | sub preserve_space { | |
147 | my($str) = shift; | |
148 | 1 while | |
149 | $str =~ s/^([^\t]*)(\t+)/$1 . ' ' x (length($2) * 8 - length($1) % 8)/e; | |
150 | $str =~ s/ /\ /g; | |
151 | $str; | |
152 | } | |
153 | ||
154 | ##--------------------------------------------------------------------------- | |
155 | 1; |