source: trunk/gsdl/perllib/html.pm@ 14

Last change on this file since 14 was 4, checked in by sjboddie, 26 years ago

Initial revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1package html;
2
3# htmlsafe(TEXT)
4#
5# Converts SGML meta characters in TEXT to entity references.
6#
7sub htmlsafe
8{
9 $_[0] =~ s/&/&/og;
10 $_[0] =~ s/</&lt;/og;
11 $_[0] =~ s/>/&gt;/og;
12}
13
14# urlsafe(TEXT)
15#
16# Converts characters not allowed in a URL to their hex representation.
17#
18sub urlsafe
19{
20 $_[0] =~ s/[\x09\x20\x22\x3c\x3e\x5b\x5c\x5d\x5e\x60\x7b\x7c\x7d\x7e\?\=\&\+_]/sprintf("%%%2x", ord($&))/ge;
21}
22
23# dmsafe
24#
25# Converts characters which could make display.pl or html crack to their entity references.
26# Don't use this on dm macros as they will no longer be recognisable by dm (which
27# by the way, is the whole idea).
28# -- Rodger 12/1/98
29sub dmsafe {
30 my ($s) = $_[0];
31 $s =~ s/&/&amp;/og; # for html
32 $s =~ s/</&lt;/og; # for html
33 $s =~ s/>/&gt;/og; # for html
34 $s =~ s/\_/&#095;/og; # for dm (we have a convention of starting macros with _
35 $s =~ s/\"/&quot;/og; # for html (don't want to be interpreted as a quote)
36 $s =~ s/\{/&#123;/og; # for dm blocks
37 $s =~ s/\}/&#125;/og; # for dm blocks
38 return $s;
39}
40
41
42# named entry to the standard html font
43%charnetosf = ("Agrave"=> "192", "Aacute"=> "193", "Acirc" => "194", "Atilde"=> "195",
44 "Auml" => "196", "Aring" => "197", "AElig" => "198", "Ccedil"=> "199",
45 "Egrave"=> "200", "Eacute"=> "201", "Ecirc" => "202", "Euml" => "203",
46 "Igrave"=> "204", "Iacute"=> "205", "Icirc" => "206", "Iuml" => "207",
47 "ETH" => "208", "Ntilde"=> "209", "Ograve"=> "210", "Oacute"=> "211",
48 "Ocirc" => "212", "Otilde"=> "213", "Ouml" => "214",
49 "Oslash"=> "216", "Ugrave"=> "217", "Uacute"=> "218", "Ucirc" => "219",
50 "Uuml" => "220", "Yacute"=> "221", "THORN" => "222", "szlig" => "223",
51 "agrave"=> "224", "aacute"=> "225", "acirc" => "226", "atilde"=> "227",
52 "auml" => "228", "aring" => "229", "aelig" => "230", "ccedil"=> "231",
53 "egrave"=> "232", "eacute"=> "233", "ecirc" => "234", "euml" => "235",
54 "igrave"=> "236", "iacute"=> "237", "icirc" => "238", "iuml" => "239",
55 "eth" => "240", "ntidle"=> "241", "ograve"=> "242", "oacute"=> "243",
56 "ocirc" => "244", "otilde"=> "245", "ouml" => "246",
57 "oslash"=> "248", "ugrave"=> "249", "uacute"=> "250", "ucirc" => "251",
58 "uuml" => "252", "yacute"=> "253", "thorn" => "254", "yuml" => "255");
59
60%symnetosf = ("quot" => "34", "amp" => "38", "lt" => "60", "gt" => "62",
61 "nbsp" => "160", "iexcl" => "161", "cent" => "162", "pound" => "163",
62 "curren"=> "164", "yen" => "165", "brvbar"=> "166", "sect" => "167",
63 "uml" => "168", "copy" => "169", "ordf" => "170", "laquo" => "171",
64 "not" => "172", "shy" => "173", "reg" => "174", "macr" => "175",
65 "deg" => "176", "plusmn"=> "177", "sup2" => "178", "sup3" => "179",
66 "acute" => "180", "micro" => "181", "para" => "182", "middot"=> "183",
67 "cedil" => "184", "sup1" => "185", "ordm" => "186", "raquo" => "187",
68 "frac14"=> "188", "frac12"=> "189", "frac34"=> "190", "iquest"=> "191",
69 "times" => "215", "divide"=> "247");
70
71
72
73# standard font to plain text
74%sftotxt = ("32" => " ", "33" => "!", "34" => "\"", "35" => "\#", "36" => "\$",
75 "37" => "\%", "38" => "&", "39" => "'", "40" => "(", "41" => ")",
76 "42" => "*", "43" => "+", "44" => ",", "45" => "-", "46" => ".",
77 "47" => "/", "48" => "0", "49" => "1", "50" => "2", "51" => "3",
78 "52" => "4", "53" => "5", "54" => "6", "55" => "7", "56" => "8",
79 "57" => "9", "58" => ":", "59" => ";", "60" => "<", "61" => "=",
80 "62" => ">", "63" => "?", "64" => "\@", "65" => "A", "66" => "B",
81 "57" => "9", "58" => ":", "59" => ";", "61" => "=",
82 "63" => "?", "64" => "\@", "65" => "A", "66" => "B",
83 "67" => "C", "68" => "D", "69" => "E", "70" => "F", "71" => "G",
84 "72" => "H", "73" => "I", "74" => "J", "75" => "K", "76" => "L",
85 "77" => "M", "78" => "N", "79" => "O", "80" => "P", "81" => "Q",
86 "82" => "R", "83" => "S", "84" => "T", "85" => "U", "86" => "V",
87 "87" => "W", "88" => "X", "89" => "Y", "90" => "Z", "91" => "[",
88 "92" => "\\", "93" => "]", "94" => "^", "95" => "_", "96" => "`",
89 "97" => "a", "98" => "b", "99" => "c", "100" => "d", "101" => "e",
90 "102" => "f", "103" => "g", "104" => "h", "105" => "i", "106" => "j",
91 "107" => "k", "108" => "l", "109" => "m", "110" => "n", "111" => "o",
92 "112" => "p", "113" => "q", "114" => "r", "115" => "s", "116" => "t",
93 "117" => "u", "118" => "v", "119" => "w", "120" => "x", "121" => "y",
94 "122" => "z", "123" => "{", "124" => "|", "125" => "}", "126" => "~",
95 "130" => ",", "131" => "f", "132" => "\"", "133" => "...", "139" => "<",
96 "140" => "OE", "145" => "'", "146" => "'", "147" => "\"", "148" => "\"",
97 "149" => "o", "150" => "--", "151" => "-", "152" => "~", "153" => "TM",
98 "155" => ">", "156" => "oe", "159" => "Y", "160" => " ", "178" => "2",
99 "179" => "3", "185" => "1", "188" => "1/4", "189" => "1/2", "190" => "3/4",
100 "192" => "A", "193" => "A", "194" => "A", "195" => "A", "196" => "A",
101 "197" => "A", "198" => "AE", "199" => "C", "200" => "E", "201" => "E",
102 "202" => "E", "203" => "E", "204" => "I", "205" => "I", "206" => "I",
103 "207" => "I", "208" => "D", "209" => "N", "210" => "O", "211" => "O",
104 "212" => "O", "213" => "O", "214" => "O", "215" => "*", "216" => "O",
105 "217" => "U", "218" => "U", "219" => "U", "220" => "U", "221" => "Y",
106 "223" => "ss", "224" => "a", "225" => "a", "226" => "a", "227" => "a",
107 "228" => "a", "229" => "a", "230" => "ae", "231" => "c", "232" => "e",
108 "233" => "e", "234" => "e", "235" => "e", "236" => "i", "237" => "i",
109 "238" => "i", "239" => "i", "241" => "n", "242" => "o", "243" => "o",
110 "244" => "o", "245" => "o", "246" => "o", "247" => "/", "248" => "o",
111 "249" => "u", "250" => "u", "251" => "u", "252" => "u", "253" => "y",
112 "255" => "y", "8218" => ",");
113
114
115# returns the character in the standard html font. It assumes that the
116# & and ; have been stripped off the string.
117sub getcharequiv {
118 my ($entity, $convertsymbols) = @_;
119
120 # replace &#8218 with comma (",")
121 $entity =~ s/8218/044/;
122
123 # a numeric entity
124 if ($entity =~ /^\#0*(\d+)/) {
125 return pack("c", $1);
126 }
127
128 # a named character entity
129 if (defined $charnetosf{$entity}) {
130 return pack("c", $charnetosf{$entity});
131 }
132
133 # a named symbol entity
134 if ($convertsymbols && defined $symnetosf{$entity}) {
135 return pack("c", $symnetosf{$entity});
136 }
137
138 return "&$entity;"; # unknown character
139}
140
141# convert character entities from named equivalents to html font
142sub convertcharentities {
143 # args: the text that you want to convert
144
145 $_[0] =~ s/&([^;]+);/&getcharequiv($1,0)/ge;
146}
147
148# convert any entities from named equivalents to html font
149sub convertallentities {
150 # args: the text that you want to convert
151
152 $_[0] =~ s/&([^;]+);/&getcharequiv($1,1)/ge;
153}
154
155sub html2txt {
156 # args: the text that you want converted to ascii,
157 # and whether to strip out sgml tags
158
159 # strip out sgml tags if needed
160 $_[0] =~ s/<[^>]>//g if $_[1];
161
162 # convert the char entities to the standard html font
163 &convertcharentities($_[0]);
164
165 # convert the html character set to a plain ascii character set
166 my $pos = 0;
167 while ($pos < length($_[0])) {
168 my $charnum = ord(substr($_[0], $pos, 1));
169 if ($charnum >= 32) { # only convert characters above #32
170 my $replacechars = " ";
171 $replacechars = $sftotxt{$charnum} if defined $sftotxt{$charnum};
172 substr($_[0], $pos, 1) = $replacechars;
173 $pos += length ($replacechars);
174
175 } else {
176 $pos ++;
177 }
178 }
179}
180
181
182sub guess_mime_type {
183 my ($filename) = @_;
184
185 my ($fileext) = $filename =~ /\.(\w+)$/;
186 return "unknown" unless defined $fileext;
187
188 my %mime_type = ("ai"=>"application/postscript", "aif"=>"audio/x-aiff",
189 "aifc"=>"audio/x-aiff", "aiff"=>"audio/x-aiff",
190 "au"=>"audio/basic", "avi"=>"video/x-msvideo",
191 "bcpio"=>"application/x-bcpio", "bin"=>"application/octet-stream",
192 "cdf"=>"application/x-netcdf", "class"=>"application/octet-stream",
193 "cpio"=>"application/x-cpio", "cpt"=>"application/mac-compactpro",
194 "csh"=>"application/x-csh", "dcr"=>"application/x-director",
195 "dir"=>"application/x-director", "dms"=>"application/octet-stream",
196 "doc"=>"application/msword", "dvi"=>"application/x-dvi",
197 "dxr"=>"application/x-director", "eps"=>"application/postscript",
198 "etx"=>"text/x-setext",
199 "exe"=>"application/octet-stream", "gif"=>"image/gif",
200 "gtar"=>"application/x-gtar", "hdf"=>"application/x-hdf",
201 "hqx"=>"application/mac-binhex40", "htm"=>"text/html",
202 "html"=>"text/html", "ice"=>"x-conference/x-cooltalk",
203 "ief"=>"image/ief", "jpe"=>"image/jpeg",
204 "jpeg"=>"image/jpeg", "jpg"=>"image/jpeg",
205 "kar"=>"audio/midi", "latex"=>"application/x-latex",
206 "lha"=>"application/octet-stream", "lzh"=>"application/octet-stream",
207 "man"=>"application/x-troff-man", "mcf"=>"image/vasa",
208 "me"=>"application/x-troff-me", "mid"=>"audio/midi",
209 "midi"=>"audio/midi", "mif"=>"application/x-mif",
210 "mov"=>"video/quicktime", "movie"=>"video/x-sgi-movie",
211 "mp2"=>"audio/mpeg", "mpe"=>"video/mpeg",
212 "mpeg"=>"video/mpeg", "mpg"=>"video/mpeg",
213 "mpga"=>"audio/mpeg", "ms"=>"application/x-troff-ms",
214 "nc"=>"application/x-netcdf", "oda"=>"application/oda",
215 "pbm"=>"image/x-portable-bitmap", "pdb"=>"chemical/x-pdb",
216 "pdf"=>"application/pdf", "pgm"=>"image/x-portable-graymap",
217 "png"=>"image/png", "pnm"=>"image/x-portable-anymap",
218 "ppm"=>"image/x-portable-pixmap", "ppt"=>"application/powerpoint",
219 "ps"=>"application/postscript", "qt"=>"video/quicktime",
220 "ra"=>"audio/x-realaudio", "ram"=>"audio/x-pn-realaudio",
221 "ras"=>"image/x-cmu-raster", "rgb"=>"image/x-rgb",
222 "roff"=>"application/x-troff", "rpm"=>"audio/x-pn-realaudio-plugin",
223 "rtf"=>"application/rtf", "rtx"=>"text/richtext",
224 "sgm"=>"text/x-sgml", "sgml"=>"text/x-sgml",
225 "sh"=>"application/x-sh", "shar"=>"application/x-shar",
226 "sit"=>"application/x-stuffit", "skd"=>"application/x-koan",
227 "skm"=>"application/x-koan", "skp"=>"application/x-koan",
228 "skt"=>"application/x-koan", "snd"=>"audio/basic",
229 "src"=>"application/x-wais-source", "sv4cpio"=>"application/x-sv4cpio",
230 "sv4crc"=>"application/x-sv4crc", "t"=>"application/x-troff",
231 "tar"=>"application/x-tar", "tcl"=>"application/x-tcl",
232 "tex"=>"application/x-tex", "texi"=>"application/x-texinfo",
233 "texinfo"=>"application/x-texinfo", "tif"=>"image/tiff",
234 "tiff"=>"image/tiff", "tr"=>"application/x-troff",
235 "tsv"=>"text/tab-separated-values", "txt"=>"text/plain",
236 "ustar"=>"application/x-ustar", "vcd"=>"application/x-cdlink",
237 "vrml"=>"x-world/x-vrml", "wav"=>"audio/x-wav",
238 "wrl"=>"x-world/x-vrml", "xbm"=>"image/x-xbitmap",
239 "xpm"=>"image/x-xpixmap", "xwd"=>"image/x-xwindowdump",
240 "xyz"=>"chemical/x-pdb", "zip"=>"application/zip");
241
242 return $mime_type{$fileext} if (defined $mime_type{$fileext});
243
244 return "unknown";
245}
246
247
2481;
Note: See TracBrowser for help on using the repository browser.