Template-Provider-Encoding-0.10/0000755000175000017500000000000010654027217020145 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/t/0000755000175000017500000000000010654027216020407 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/t/utf-8.tt0000644000175000017500000000022610406771676021736 0ustar miyagawamiyagawa00000000000000[% USE encoding 'utf-8' -%] わたしの名前は [% author %] です。 わたしは [% my.place %] にすんでいます。 encoding=[% encoding %] Template-Provider-Encoding-0.10/t/02_obj.t0000644000175000017500000000103210516616100021634 0ustar miyagawamiyagawa00000000000000use strict; use Test::More; eval { require DateTime; 1 }; if ($@) { plan skip_all => 'require DateTime'; } use Template; use Template::Stash::ForceUTF8; if ($Template::Config::STASH ne 'Template::Stash::XS') { plan skip_all => 'require Template::Stash::XS'; } plan tests => 1; my $tt = Template->new({ STASH => Template::Stash::ForceUTF8->new, }); my $dt = DateTime->new(year => 2005, month => 9, day => 12); $tt->process(\< $dt }, \my $out) or die $tt->error; [% dt.ymd %] EOF like $out, qr/2005-09-12/; Template-Provider-Encoding-0.10/t/01_unicode.t0000644000175000017500000000352510406771676022542 0ustar miyagawamiyagawa00000000000000use strict; use Test::More 'no_plan'; use Encode; use Template::Provider::Encoding; use Template::Stash::ForceUTF8; use Template; my @files = qw( euc-jp.tt utf-8.tt utf-8-wo-encoding.tt utf-8-bom.tt ); my $author = "\x{5bae}\x{5ddd}"; # miyagawa my $place = "\x{6771}\x{4eac}"; # Tokyo my $author_utf8 = encode("utf-8", $author); my $place_utf8 = encode("utf-8", $place); for my $file (@files) { my $tt = Template->new( LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], ); my $vars; $vars->{author} = $author; # Unicode string $vars->{my} = { place => $place }; # Unicode string $tt->process("t/$file", $vars, \my $out) or die $tt->error; ok Encode::is_utf8($out), "$file output is utf-8 flagged"; like $out, qr/$author/, "$file includes author name correctly"; like $out, qr/$place/, "$file includes place correctly"; unless ($file =~ /(-wo-|-bom)/) { my $encoding = ($file =~ /(.*)\.tt/)[0]; like $out, qr/encoding=$encoding/, "$file has encoding $encoding"; } } # test mixing Unicode flagged and UTF-8 bytes in the stash (Unicode flagged) for my $file (@files) { my $tt = Template->new( LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], STASH => Template::Stash::ForceUTF8->new, ); my $vars; $vars->{author} = $author; # unicode string $vars->{my} = { place => $place_utf8 }; # utf-8 $tt->process("t/$file", $vars, \my $out) or die $tt->error; ok Encode::is_utf8($out), "$file output is utf-8 flagged"; like $out, qr/$author/, "$file includes author name correctly"; like $out, qr/$place/, "$file includes place correctly"; unless ($file =~ /(-wo-|-bom)/) { my $encoding = ($file =~ /(.*)\.tt/)[0]; like $out, qr/encoding=$encoding/, "$file has encoding $encoding"; } } Template-Provider-Encoding-0.10/t/euc-jp.tt0000644000175000017500000000020110406771676022147 0ustar miyagawamiyagawa00000000000000[% USE encoding 'euc-jp' -%] 錄̾ [% author %] Ǥ 錄 [% my.place %] ˤǤޤ encoding=[% encoding %] Template-Provider-Encoding-0.10/t/utf-8-wo-encoding.tt0000644000175000017500000000014410406771676024144 0ustar miyagawamiyagawa00000000000000わたしの名前は [% author %] です。 わたしは [% my.place %] にすんでいます。 Template-Provider-Encoding-0.10/t/00_compile.t0000644000175000017500000000013010406771676022530 0ustar miyagawamiyagawa00000000000000use strict; use Test::More tests => 1; BEGIN { use_ok 'Template::Provider::Encoding' } Template-Provider-Encoding-0.10/t/utf-8-bom.tt0000644000175000017500000000017510406771676022514 0ustar miyagawamiyagawa00000000000000わたしの名前は [% author %] です。 わたしは [% my.place %] にすんでいます。 encoding=[% encoding %] Template-Provider-Encoding-0.10/lib/0000755000175000017500000000000010654027216020712 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/lib/Template/0000755000175000017500000000000010654027216022465 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/lib/Template/Provider/0000755000175000017500000000000010654027216024257 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/lib/Template/Provider/Encoding.pm0000644000175000017500000000647310654027175026361 0ustar miyagawamiyagawa00000000000000package Template::Provider::Encoding; use strict; our $VERSION = '0.10'; use base qw( Template::Provider ); use Encode; sub _init { my ($self, $params) = @_; $self = $self->SUPER::_init($params); $self->{DEFAULT_ENCODING} = $params->{DEFAULT_ENCODING} || 'utf8'; $self->{ENCODE_CHECK} = $params->{ENCODE_CHECK} || Encode::FB_DEFAULT; return $self; } sub _load { my $self = shift; my($data, $error) = $self->SUPER::_load(@_); return ($data, $error) unless defined $data; unless (Encode::is_utf8($data->{text})) { my $decoder = $self->detect_encoding($data); $data->{text} = $decoder->decode($data->{text}, $self->{ENCODE_CHECK}); } return ($data, $error); } sub detect_encoding { my ($self, $data) = @_; my $encoding = $data->{text} =~ /^\[% USE encoding '([\w\-]+)'/ ? $1 : $self->{DEFAULT_ENCODING}; return Encode::find_encoding($encoding); } 1; __END__ =head1 NAME Template::Provider::Encoding - Explicitly declare encodings of your templates =head1 SYNOPSIS use Template::Provider::Encoding; use Template::Stash::ForceUTF8; use Template; my $tt = Template->new( LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], STASH => Template::Stash::ForceUTF8->new, ); # Everything should be Unicode # (but you can pass UTF-8 bytes as well, thanks to Template::Stash::ForceUTF8) my $author = "\x{5bae}\x{5ddd}"; # this will emit Unicode flagged string to STDOUT. You might # probably want to binmode(STDOUT, ":encoding($enccoding)") # before process() call $tt->process($template, { author => $author }); # in your templates [% USE encoding 'utf-8' -%] My name is [% author %]. { ... whatever UTF-8 bytes } =head1 DESCRIPTION Template::Provider::Encoding is a Template Provider subclass to decode template using its declaration. You have to declare encoding of the template in the head (1st line) of template using (fake) encoding TT plugin. Otherwise the template is handled as utf-8. [% USE encoding 'utf-8' %] Here comes utf-8 strings with [% variable %]. =head1 DIFFERNCE WITH OTHER WAYS =head2 UNICODE option and BOM Recent TT allows C option to Template::Provider and by adding it Provider scans BOM (byte-order mark) to detect UTF-8/UTF-16 encoded template files. This module does basically the same thing in a different way, but IMHO adding BOM to template files is a little painful especially for non-programmers. =head2 Template::Provider::Encode L provides a very similar way to detect Template file encodings and output the template into various encodings. This module doesn't touch output encoding of the template and instead it emits valid Unicode flagged string. I think the output encoding conversion should be done by other piece of code, especially in the framework. This module doesn't require you to specify encoding in the code, nor doesn't I encodings. Instead it forces you to put C<< [% USE encoding 'foo-bar' %] >> in the top of template files, which is explicit and, I think, is a good convention. =head1 AUTHOR Tatsuhiko Miyagawa Emiyagawa@bulknews.netE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L, L =cut Template-Provider-Encoding-0.10/lib/Template/Plugin/0000755000175000017500000000000010654027216023723 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/lib/Template/Plugin/encoding.pm0000644000175000017500000000171710406771676026070 0ustar miyagawamiyagawa00000000000000package Template::Plugin::encoding; use base qw( Template::Plugin ); our $VERSION = '0.02'; sub new { my $class = shift; my $contetx = shift; $_[0]; } 1; __END__ =head1 NAME Tempate::Plugin::encoding - Template plugin to specify encoding =head1 SYNOPSIS [% USE encoding 'euc-jp' -%] =head1 DESCRIPTION Template::Plugin::encoding is a Template plugin to declare the encoding of template files. This plugin doesn't actually do anything but Template::Provider::Encoding scans the usage of this module to find the encoding of templates. As a bonus, you can use C variable in the template to specify file encoding, which might be useful for XML or HTML meta tag. =head1 AUTHOR Tatsuhiko Miyagawa Emiyagawa@bulknews.netE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L =cut Template-Provider-Encoding-0.10/lib/Template/Stash/0000755000175000017500000000000010654027216023547 5ustar miyagawamiyagawa00000000000000Template-Provider-Encoding-0.10/lib/Template/Stash/ForceUTF8.pm0000644000175000017500000000203510406771676025625 0ustar miyagawamiyagawa00000000000000package Template::Stash::ForceUTF8; use strict; our $VERSION = '0.03'; use Template::Config; use base ( $Template::Config::STASH ); use Encode; sub get { my $self = shift; my $result = $self->SUPER::get(@_); return $result if ref $result; Encode::_utf8_on($result) unless Encode::is_utf8($result); return $result; } 1; __END__ =head1 NAME Template::Stash::ForceUTF8 - Force UTF-8 (Unicode) flag on stash variables =head1 SYNOPSIS use Template::Stash::ForceUTF8; use Template; my $tt = Template->new( LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], STASH => Template::Stash::ForceUTF8->new, ); my $vars; $vars->{foo} = "\x{5bae}\x{5ddd}"; # Unicode flagged $vars->{bar} = "\xe5\xae\xae\xe5\xb7\x9d"; # UTF-8 bytes $tt->process($template, $vars); # this DWIMs =head1 DESCRIPTION Template::Stash::ForceUTF8 is a Template::Stash that forces Unicode flag on stash variables. Best used with L. =head1 SEE ALSO L =cut Template-Provider-Encoding-0.10/Changes0000644000175000017500000000234510654027021021435 0ustar miyagawamiyagawa00000000000000Revision history for Perl extension Template::Provider::Encoding 0.10 Tue Jul 31 23:53:58 PDT 2007 - Fixed warning issue. RT 27645 0.09 Wed Apr 18 14:49:48 PDT 2007 - Added Encode.pm to prereq 0.08 Tue Apr 17 13:52:41 PDT 2007 - Applied patch http://www.cpanforum.com/posts/2177 so the default encoding other than 'utf-8' can be set (Thanks to Nobuaki ITO) 0.07 Sun Oct 22 21:41:03 JST 2006 - Added dependency for Template-Toolkit (Thanks to Andreas Koenig) 0.06 Sun Oct 22 21:11:38 JST 2006 - Skip obj.t test if Template::Stash::XS is not available 0.05 2006-02-05T01:10:24Z - Packaging bug caused test errors 0.04 2006-01-26T18:50:22Z - Do nothing when template is BOMed (Thanks to Bill Moseley) 0.03 2006-01-16T22:38:37Z - Use $Template::Stash::Config as a base class to support XS based object variable correctly 0.02 Sat Jan 14 15:01:49 UTC 2006 - Added Template::Stash::ForceUTF8 and now this module works magically (you can mix Unicode flagged and UTF-8 bytes in stash!) - Now no_unicode mode is unnecessary since we have magical stash 0.01 Tue Jan 10 14:39:56 2006 - original version Template-Provider-Encoding-0.10/MANIFEST0000644000175000017500000000051110654027217021273 0ustar miyagawamiyagawa00000000000000Changes lib/Template/Plugin/encoding.pm lib/Template/Provider/Encoding.pm lib/Template/Stash/ForceUTF8.pm Makefile.PL MANIFEST This list of files t/00_compile.t t/01_unicode.t t/02_obj.t t/euc-jp.tt t/utf-8-bom.tt t/utf-8-wo-encoding.tt t/utf-8.tt META.yml Module meta-data (added by MakeMaker) Template-Provider-Encoding-0.10/Makefile.PL0000644000175000017500000000042310611511072022104 0ustar miyagawamiyagawa00000000000000use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'Template::Provider::Encoding', 'VERSION_FROM' => 'lib/Template/Provider/Encoding.pm', # finds $VERSION 'PREREQ_PM' => { Test::More => 0.32, Template => 2.10, Encode => 1.00, }, ); Template-Provider-Encoding-0.10/META.yml0000644000175000017500000000070410654027217021417 0ustar miyagawamiyagawa00000000000000# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Template-Provider-Encoding version: 0.10 version_from: lib/Template/Provider/Encoding.pm installdirs: site requires: Encode: 1 Template: 2.1 Test::More: 0.32 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.30