Changeset 1747

Show
Ignore:
Timestamp:
01/14/06 23:45:55
Author:
miyagawa
Message:

0.02: Added Stash::ForceUTF8 magic.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Template-Provider-Encoding/trunk/Changes

    r1733 r1747  
    11Revision history for Perl extension Template::Provider::Encoding 
     2 
     30.02 
     4        - Added Template::Stash::ForceUTF8 and now this module works magically 
     5          (you can mix Unicode flagged and UTF-8 bytes in stash!) 
     6        - Now no_unicode mode is unnecessary since we have magical stash 
    27 
    380.01  Tue Jan 10 14:39:56 2006 
  • Template-Provider-Encoding/trunk/MANIFEST

    r1734 r1747  
    22lib/Template/Plugin/encoding.pm 
    33lib/Template/Provider/Encoding.pm 
     4lib/Template/Stash/ForceUTF8.pm 
    45Makefile.PL 
    56MANIFEST                        This list of files 
     7META.yml                        Module meta-data (added by MakeMaker) 
    68t/00_compile.t 
    79t/01_unicode.t 
     
    911t/utf-8-wo-encoding.tt 
    1012t/utf-8.tt 
    11 META.yml                                 Module meta-data (added by MakeMaker) 
  • Template-Provider-Encoding/trunk/lib/Template/Plugin/encoding.pm

    r1733 r1747  
    11package Template::Plugin::encoding; 
    22use base qw( Template::Plugin ); 
     3 
     4our $VERSION = '0.02'; 
    35 
    46sub new { 
  • Template-Provider-Encoding/trunk/lib/Template/Provider/Encoding.pm

    r1733 r1747  
    22 
    33use strict; 
    4 our $VERSION = '0.01'; 
     4our $VERSION = '0.02'; 
    55 
    66use base qw( Template::Provider ); 
    77use Encode; 
    8  
    9 sub new { 
    10     my $class = shift; 
    11     my $options = shift || {}; 
    12  
    13     my $self = $class->SUPER::new($options); 
    14     $self->{__no_unicode} = delete $options->{no_unicode}; 
    15     $self; 
    16 } 
    178 
    189sub _load { 
     
    2314        ? $1 : 'utf-8'; 
    2415 
    25     if ($self->{__no_unicode}) { 
    26         if ($encoding !~ /^utf-?8$/i) { 
    27             Encode::from_to($data->{text}, $encoding => 'utf-8'); 
    28         } 
    29     } else { 
    30         $data->{text} = Encode::decode($encoding, $data->{text}); 
    31     } 
    32  
     16    $data->{text} = Encode::decode($encoding, $data->{text}); 
    3317    return ($data, $error); 
    3418} 
     
    4428 
    4529  use Template::Provider::Encoding; 
     30  use Template::Stash::ForceUTF8; 
    4631  use Template; 
    4732 
    4833  my $tt = Template->new( 
    4934      LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], 
     35      STASH => Template::Stash::ForceUTF8->new, 
    5036  ); 
    5137 
    52   # By default, everything should be Unicode 
     38  # Everything should be Unicode 
     39  # (but you can pass UTF-8 bytes as well, thanks to Template::Stash::ForceUTF8) 
    5340  my $author = "\x{5bae}\x{5ddd}"; 
    5441 
     
    5946 
    6047  # in your templates 
    61   [% USE encoding 'shift_jis' -%] 
    62   My name is [% author %]. { ... whatever Shift_JIS bytes } 
    63  
    64   # ---------------------------------------------------------------- 
    65  
    66   # If you don't like Unicode flag at all: 
    67   my $tt = Template->new( 
    68       LOAD_TEMPLATES => [ Template::Provider::Encoding->new({ no_unicode => 1 }) ], 
    69   ); 
    70  
    71   # name in UTF-8 bytes 
    72   my $author = "\xe5\xae\xae\xe5\xb7\x9d"; 
    73  
    74   # this will emit UTF-8 bytes, not Unicode string 
    75   $tt->process($template, { author => $author }); 
     48  [% USE encoding 'utf-8' -%] 
     49  My name is [% author %]. { ... whatever UTF-8 bytes } 
    7650 
    7751=head1 DESCRIPTION 
     
    8559  Here comes utf-8 strings with [% variable %]. 
    8660 
    87 =head1 OPTION
     61=head1 DIFFERNCE WITH OTHER WAY
    8862 
    89 Template::Provider::Encoding C<new> method takes following options. 
     63=head2 UNICODE option and BOM 
    9064 
    91 =over 4 
     65Recnet TT allows C<UNICODE> option to Template::Provider and by adding 
     66it Provider scans BOM (byte-order mark) to detect UTF-8/UTF-16 encoded 
     67template files. This module does basically the same thing in a 
     68different way, but IMHO adding BOM to template files is a little 
     69painful especially for non-programmers. 
    9270 
    93 =item no_unicode 
     71=head2 Template::Provider::Encode 
    9472 
    95 By default, Template::Provider::Encoding assumes everything is Unicode 
    96 and utf-8 flagged. This is the right thing (TM) but reality might not 
    97 allow it, when your app talks with various data source and use some 
    98 nasty CPAN modules that doen't care UTF-8 flags. In that case you'd 
    99 want to add C<no_unicode> option to Template::Provider::Encoding 
    100 C<new>, in which case the module handles everything in UTF-8 with no 
    101 Unicode flags. 
    102  
    103 =back 
    104  
    105 =head1 DIFFERNCE WITH ENCODE PROVIDER 
    106  
    107 So what's the difference between L<Template::Provider::Encode> and 
    108 this module? 
     73L<Template::Provider::Encode> provides a very similar way to detect 
     74Template file encodings and output the template into various 
     75encodings. 
    10976 
    11077This module doesn't touch output encoding of the template and instead 
     
    11885explicit and, I think, is a good convention. 
    11986 
    120 When you encode template files in UTF-8 and handle all the variables 
    121 in UTF-8 bytes (not UTF-8 flagged) on perl level, this module's 
    122 C<no_unicode> mode does just the same thing and you don't have to use 
    123 this module. 
    124  
    12587=head1 AUTHOR 
    12688 
     
    13294=head1 SEE ALSO 
    13395 
    134 L<Template::Provider::Encode> 
     96L<Template::Stash::ForceUTF8>, L<Template::Provider::Encode> 
    13597 
    13698=cut 
  • Template-Provider-Encoding/trunk/t/01_unicode.t

    r1733 r1747  
    44use Encode; 
    55use Template::Provider::Encoding; 
     6use Template::Stash::ForceUTF8; 
    67use Template; 
    78 
    89my @files = qw( euc-jp.tt utf-8.tt utf-8-wo-encoding.tt ); 
     10 
     11my $author = "\x{5bae}\x{5ddd}"; # miyagawa 
     12my $place  = "\x{6771}\x{4eac}"; # Tokyo 
     13my $author_utf8 = encode("utf-8", $author); 
     14my $place_utf8  = encode("utf-8", $place); 
    915 
    1016for my $file (@files) { 
     
    1218        LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], 
    1319    ); 
    14     my $author = "\x{5bae}\x{5ddd}"; # unicode string 
    15     $tt->process("t/$file", { author => $author }, \my $out) or die $tt->error; 
     20    my $vars; 
     21    $vars->{author} = $author;             # Unicode string 
     22    $vars->{my}     = { place => $place }; # Unicode string 
     23    $tt->process("t/$file", $vars, \my $out) or die $tt->error; 
    1624 
    1725    ok Encode::is_utf8($out), "$file output is utf-8 flagged"; 
    18     like $out, qr/\x{5bae}\x{5ddd}/, "$file it includes author name correctly"; 
     26    like $out, qr/$author/, "$file includes author name correctly"; 
     27    like $out, qr/$place/, "$file includes place correctly"; 
    1928    unless ($file =~ /-wo-/) { 
    2029        my $encoding = ($file =~ /(.*)\.tt/)[0]; 
     
    2332} 
    2433 
     34# test mixing Unicode flagged and UTF-8 bytes in the stash (Unicode flagged) 
    2535for my $file (@files) { 
    2636    my $tt = Template->new( 
    27         LOAD_TEMPLATES => [ Template::Provider::Encoding->new({ no_unicode => 1 }) ], 
     37        LOAD_TEMPLATES => [ Template::Provider::Encoding->new ], 
     38        STASH => Template::Stash::ForceUTF8->new, 
    2839    ); 
    29     my $author = "\xe5\xae\xae\xe5\xb7\x9d"; # utf-8 bytes 
    30     $tt->process("t/$file", { author => $author }, \my $out) or die $tt->error; 
     40    my $vars; 
     41    $vars->{author} = $author;                  # unicode string 
     42    $vars->{my}     = { place => $place_utf8 }; # utf-8 
     43    $tt->process("t/$file", $vars, \my $out) or die $tt->error; 
    3144 
    32     ok !Encode::is_utf8($out), "$file output is not utf-8 flagged"; 
    33     my $copy = $out; 
    34     my $decode = Encode::decode("utf-8", $copy, Encode::FB_CROAK); 
    35     ok $decode, "decode it correctly"; 
    36     like $out, qr/$author/, "$file it includes author name correctly"; 
     45    ok Encode::is_utf8($out), "$file output is utf-8 flagged"; 
     46    like $out, qr/$author/, "$file includes author name correctly"; 
     47    like $out, qr/$place/, "$file includes place correctly"; 
     48    unless ($file =~ /-wo-/) { 
     49        my $encoding = ($file =~ /(.*)\.tt/)[0]; 
     50        like $out, qr/encoding=$encoding/, "$file has encoding $encoding"; 
     51    } 
    3752} 
  • Template-Provider-Encoding/trunk/t/euc-jp.tt

    r1733 r1747  
    11[% USE encoding 'euc-jp' -%] 
    22€ï€¿€·€ÎÌŸÁ°€Ï [% author %] €Ç€¹¡£ 
     3€ï€¿€·€Ï [% my.place %] €Ë€¹€ó€Ç€€€Þ€¹¡£ 
    34encoding=[% encoding %] 
  • Template-Provider-Encoding/trunk/t/utf-8-wo-encoding.tt

    r1733 r1747  
    11わたしの名前は [% author %] です。 
     2わたしは [% my.place %] にすんでいます。 
    23 
    34 
  • Template-Provider-Encoding/trunk/t/utf-8.tt

    r1733 r1747  
    11[% USE encoding 'utf-8' -%] 
    22わたしの名前は [% author %] です。 
     3わたしは [% my.place %] にすんでいます。 
    34encoding=[% encoding %]