# Blosxom3 Plugin: Encode # Author: Pomin Wu # Version: 2005-08-11 # More notes at the bottom of this file or type: perldoc Encode package Blosxom::Plugin::Encode; use strict; # ----------- configuration --------------------- # In what encoding are the entry files written? my $entry_encoding = 'utf-8'; # What encoding would you like to use for output? my $output_encoding = 'utf-8'; # Set encoding for the following MIME types. my %mime_type_to_set = ( 'text/html' => 1, 'application/xml' => 1, 'application/xml-dtd' => 1, 'application/xml-external-parsed-entity' => 1, 'application/atom+xml' => 1, 'application/rss+xml' => 1, 'text/xml' => 1, 'text/xml-external-parsed-entity' => 1, ); # Debug level. my $debug = 0; # -------------------------------- our $VERSION = "2005-08-11"; # put before Blosxom::run_entries sub set_filehandle { print STDERR "[Encode.pm] calling set_filehandle" if $debug >= 1; my $blosxom = shift; $blosxom->{state}->{filehandle} = FileHandle::EntryEncoding->new($blosxom->{state}->{filehandle}); print STDERR "[Encode.pm] set entry encoding to $entry_encoding" if $debug >= 1; binmode(STDOUT, ":encoding($output_encoding)"); print STDERR "[Encode.pm] set output encoding to $output_encoding" if $debug >= 1; 1; } # put before Blosxom::output_header sub encode_header { print STDERR "[Encode.pm] calling encode_header" if $debug >=1 ; my $blosxom = shift; my ($mimetype, @param) = split /\s*[;,]\s*/, $blosxom->{response}->{content_type}->{rendered}; return unless $mime_type_to_set{$mimetype}; do { my $has_set = 0; @param = grep {(/^charset=/ and !$has_set and ($has_set=1)) or 1} (@param, "charset=$output_encoding"); print STDERR "[Encode.pm] change HTTP Content-Type charset to $output_encoding" if $debug >=1; }; $blosxom->{response}->{content_type}->{rendered} = join '; ', $mimetype, (join ', ', @param); print STDERR "[Encode.pm] resulting HTTP Content-Type: '$blosxom->{response}->{content_type}->{rendered}'" if $debug >=2; 1; } package FileHandle::EntryEncoding; use FileHandle; @FileHandle::EntryEncoding::ISA = qw(FileHandle); sub new { my ($class, $filehandle) = @_; return bless $filehandle, $class; } sub open { my ($self, $filename) = @_; my $status = FileHandle::open($self, $filename); binmode($self, ":encoding($entry_encoding)"); return $status; } 1; __END__ =head1 NAME Blosxom 3.0 Plugin: Encode =head1 SYNOPSIS Make Blosxom encoding-aware. Normally Blosxom does not care about encodings. It assumes UTF-8 encoding for everything (which is the Perl 5.8.x default). Unfortunately web browsers by default read in ISO-8895-1, so usually the simplest way to make everything correct is to always write in UTF-8, and change the "content_type.html" template to something like text/html; charset=utf-8 If you think it's too tricky, or you can't write in UTF-8 on your platform, or simply don't like to put configurations in layout, Blosxom::Plugin::Encode provides another way. =head1 INSTALLATION Drop the "Encode.pm" file under your plugin directory. =head1 CONFIGURATION In the "handlers.flow" file under settings directory, put the following line before "Blosxom::run_entries": # put before Blosxom::run_entries Blosxom::Encode::set_filehandle And put the following line before "Blosxom::output_header": # put before Blosxom::output_header Blosxom::Encode::encode_header Check the configuration variables in "Encode.pm" file. You'll probably want to set "$entry_encoding" and "$output_encoding". Yes, they may be different. Blosxom::Plugin::Encode leaves the Content-Type header untouched if charset has already been set, even if the existing value is different from the one set in "$output_encoding". =head1 VERSION 2005-08-11 Version number is the date on which this version of the plug-in was created. =head1 AUTHOR Pomin Wu Epomin5@gmail.comE =head1 SEE ALSO Blosxom Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ Blosxom Plugin Docs: http://www.raelity.org/apps/blosxom/plugin.shtml =head1 BUGS Address bug reports and comments to the Blosxom mailing list [http://www.yahoogroups.com/group/blosxom]. Blosxom::Plugin::Encode tries to solve every problem related to encoding in Blosxom 3 core, so if you experience some glitch that are not fixed, =head1 LICENSE Copyright 2005, Pomin Wu. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.