Convert from/to UTF8 TT2 filter plugin (unreleased).

liol on 2004-12-21T18:09:23

This is not released on CPAN module. After discussion in Template Toolkit mailing list it was decided not to publish it. Neighthertheless I'm saving it here. In two words, this is a wrapper around Unicode::MapUTF8. It provides two filters from_utf8 and to_utf8.

#==================================================== -*-Perl-*-
#
# MyOrg::Template::Plugin::Unicode::MapUTF8
#
# DESCRIPTION
#   Wrapper around Unicode::MapUTF8 module which converts 
#   text in different charsets into UTF8
#
# AUTHOR
#   Igor Lobanov   <*@*>
#
# COPYRIGHT
#   Copyright (C) 2004 Igor Lobanov.  All Rights Reserved.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#===============================================================

package MyOrg::Template::Plugin::Unicode::MapUTF8;

use strict;
use vars qw( $VERSION );
use Template::Plugin;
use Template::Exception;
use base qw( Template::Plugin );
use Unicode::MapUTF8 qw( to_utf8 from_utf8 utf8_supported_charset );

$VERSION = sprintf("%d.%02d", q$Revision: 0.01 $ =~ /(\d+)\.(\d+)/);

# Use this charset in filters by default
my $DEFAULT_CHARSET = 'ISO-8859-1';
# Type name of TT exception
my $EXCEPTION_TYPE = 'MapUTF8';

# Hash with filter subs for each requested charset
my $filter_set = {};

sub new {
	my ( $class, $context ) = @_;
	$context->define_filter( 'to_utf8', [ \&to_utf8_filter, 1 ] );
	$context->define_filter( 'from_utf8', [ \&from_utf8_filter, 1 ] );
	bless {}, $class;
}

sub to_utf8_filter {
	my ( $context, $charset ) = @_;
	$charset ||= $DEFAULT_CHARSET;
	utf8_supported_charset( $charset )
		or die Template::Exception->new(
			$EXCEPTION_TYPE,
			"character set '$charset' is not supported" );
	# Create filter sub for charset only once and reuse it later
	$filter_set->{$charset}->{'utf8'} ||= sub {
		my $text = shift;
		to_utf8( -string => $text, -charset => $charset );
	};
	return $filter_set->{$charset}->{'utf8'};
}

sub from_utf8_filter {
	my ( $context, $charset ) = @_;
	$charset ||= $DEFAULT_CHARSET;
	utf8_supported_charset( $charset )
		or die Template::Exception->new(
			$EXCEPTION_TYPE,
			"character set '$charset' is not supported" );
	$filter_set->{'utf8'}->{$charset} ||= sub {
		my $text = shift;
		from_utf8( -string => $text, -charset => $charset );
	};
	return $filter_set->{'utf8'}->{$charset};
}

1;

__END__

=head1 NAME

MyOrg::Template::Plugin::Unicode::MapUTF8 - Template Toolkit
plugin wrapper around Unicode::MapUTF8.

=head1 SYNOPSIS

code:

 use Template;
 ...
 my $template = new Template(
 	...
 	PLUGIN_BASE	=> 'MyOrg::Template::Plugin'
 	...
 );

template:

 [% USE Unicode::MapUTF8 %]

 [%# From cyrillic KOI8-R to UTF-8 #%]
 [% FILTER from_utf8( 'koi8-r' ) %]
 ...
 here UTF8 text
 ...
 [% END %]

 [%# From ISO-8859-1 (by default) to UTF-8 #%]
 [% FILTER to_utf8 %]
 ...
 here ISO-8859-1 text
 ...
 [% END %]

=head1 DESCRIPTION

This Template Toolkit plugin implements two filters for
conversions to and from arbitrary character sets and UTF8.
Plugin uses Benjamin Franz module L.

=head2 to_utf8

 [% FILTER to_utf8( charset ) %]
 ...
 here text in
 ...
 [% END %]

Converts text inside block to UTF8 from the specified source
charset. Default B value is I.

=head2 from_utf8

 [% FILTER from_utf8( charset ) %]
 ...
 here UTF8 text
 ...
 [% END %]

Converts text inside block from UTF8 to the specified source
charset. Default B value is I.

=head1 SEE ALSO

L, L

=head1 AUTHOR

Igor Lobanov, E*@*E

=head1 COPYRIGHT

Copyright (C) 2004 Igor Lobanov. All Rights Reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut