source: tags/gsdl-2_30d-distribution/gsdl/perllib/ghtml.pm@ 2308

Last change on this file since 2308 was 1222, checked in by sjboddie, 24 years ago

changed some ghtml.pm regular expressions to handle multiline strings

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