fixing MARCmodbiblio API and reindenting code
[koha_gimpoz] / C4 / Charset.pm
1 package C4::Charset;
2
3 # $Id$
4
5 #package to work around problems in HTTP headers
6 # Note: This is just a utility module; it should not be instantiated.
7
8
9 # Copyright 2003 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Charset - Functions for handling charsets in HTML pages
37
38 =head1 SYNOPSIS
39
40   use C4::Charset;
41
42   print $query->header(-type => C4::Charset::gettype($output)), $output;
43
44 =head1 DESCRIPTION
45
46 The functions in this module peek into a piece of HTML and return strings
47 related to the (guessed) charset.
48
49 =head1 FUNCTIONS
50
51 =over 2
52
53 =cut
54
55 @ISA = qw(Exporter);
56 @EXPORT = qw(
57                 &guesscharset
58                 &guesstype
59              );
60
61 =pod
62
63   &guesscharset($output)
64
65 "Guesses" the charset from the some HTML that would be output.
66
67 C<$output> is the HTML page to be output. If it contains a META tag
68 with a Content-Type, the tag will be scanned for a language code.
69 This code is returned if it is found; undef is returned otherwise.
70
71 This function only does sloppy guessing; it will be confused by
72 unexpected things like SGML comments. What it basically does is to
73 grab something that looks like a META tag and scan it.
74
75 =cut
76
77 sub guesscharset ($) {
78    my($html) = @_;
79    my $charset = undef;
80    local($`, $&, $', $1, $2, $3);
81    # FIXME... These regular expressions will miss a lot of valid tags!
82    if ($html =~ /<meta\s+http-equiv=(["']?)Content-Type\1\s+content=(["'])text\/html\s*;\s*charset=([^\2\s\r\n]+)\2\s*(?:\/?)>/is) {
83       $charset = $3;
84    } elsif ($html =~ /<meta\s+content=(["'])text\/html\s*;\s*charset=([^\1\s\r\n]+)\1\s+http-equiv=(["']?)Content-Type\3\s*(?:\/?)>/is) {
85       $charset = $2;
86    }
87    return $charset;
88 } # guess
89
90 sub guesstype ($) {
91    my($html) = @_;
92    my $charset = guesscharset($html);
93    return defined $charset? "text/html; charset=$charset": "text/html";
94 }
95
96 #---------------------------------
97
98 END { }       # module clean-up code here (global destructor)
99
100 1;
101 __END__
102
103 =back
104
105 =head1 AUTHOR
106
107 Koha Developement team <info@koha.org>
108
109 =cut