| 1 | package Catalyst::View::TT::ForceUTF8; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Catalyst::View::TT'; | ||
| 6 | |||
| 7 | our $VERSION = '0.13'; | ||
| 8 | |||
| 9 | use Template::Provider::Encoding 0.09; | ||
| 10 | use Template::Stash::ForceUTF8; | ||
| 11 | use Path::Class; | ||
| 12 | |||
| 13 | my @PROVIDER_CONFIG_KEYS = qw/ | ||
| 14 | INCLUDE_PATH | ||
| 15 | DEFAULT_ENCODING | ||
| 16 | COMPILE_DIR | ||
| 17 | COMPILE_EXT | ||
| 18 | EVAL_PERL | ||
| 19 | PARSER | ||
| 20 | PRE_CHOMP | ||
| 21 | POST_CHOMP | ||
| 22 | /; | ||
| 23 | |||
| 24 | # XXX: this is but a copy from View::TT. | ||
| 25 | # But this subroutine isn't class or instance method, | ||
| 26 | # so this module couldn't inherit from View::TT | ||
| 27 | sub _coerce_paths { | ||
| 28 | my ($paths, $dlim) = @_; | ||
| 29 | return () if (!$paths); | ||
| 30 | return @{$paths} if (ref $paths eq 'ARRAY'); | ||
| 31 | unless (defined $dlim) { | ||
| 32 | $dlim = ($^O eq 'MSWin32') ? ':(?!\\/)' : ':'; | ||
| 33 | } | ||
| 34 | return split(/$dlim/, $paths); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub new { | ||
| 38 | my ( $class, $c, $arguments ) = @_; | ||
| 39 | my $config = {%{$class->config}, %{$arguments}}; | ||
| 40 | |||
| 41 | # XXX: copied from View::TT | ||
| 42 | if (!(ref $config->{INCLUDE_PATH} eq 'ARRAY')) { | ||
| 43 | my $delim = $config->{DELIMITER}; | ||
| 44 | my @include_path | ||
| 45 | = _coerce_paths($config->{INCLUDE_PATH}, $delim); | ||
| 46 | if ( !@include_path ) { | ||
| 47 | my $root = $c->config->{root}; | ||
| 48 | my $base = Path::Class::dir($root, 'base'); | ||
| 49 | @include_path = ("$root", "$base"); | ||
| 50 | } | ||
| 51 | $config->{INCLUDE_PATH} = \@include_path; | ||
| 52 | } | ||
| 53 | |||
| 54 | my %args = map { ($_, $config->{$_}) } | ||
| 55 | grep { exists $config->{$_} } | ||
| 56 | @PROVIDER_CONFIG_KEYS; | ||
| 57 | |||
| 58 | $class->config->{PROVIDERS} = [ { | ||
| 59 | name => 'Encoding', | ||
| 60 | args => \%args, | ||
| 61 | copy_config => [qw(INCLUDE_PATH)], | ||
| 62 | }, ]; | ||
| 63 | $class->config->{STASH} = Template::Stash::ForceUTF8->new; | ||
| 64 | $class->config->{STRICT_CONTENT_TYPE} ||= 0; | ||
| 65 | $class->SUPER::new($c, $arguments); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub process { | ||
| 69 | my ($self, $c) = @_; | ||
| 70 | unless ($c->res->content_type) { | ||
| 71 | if ($self->config->{STRICT_CONTENT_TYPE}) { | ||
| 72 | my $agent = $c->req->user_agent || ''; | ||
| 73 | $c->res->content_type( | ||
| 74 | $agent =~ /MSIE/ | ||
| 75 | ? 'text/html; charset=utf-8' | ||
| 76 | : 'application/xhtml+xml; charset=utf-8' | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | $self->SUPER::process($c); | ||
| 81 | } | ||
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | Catalyst::View::TT::ForceUTF8 - (DEPRECATED) Template View Class with utf8 encoding | ||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | package MyApp::View::TT; | ||
| 90 | use base 'Catalyst::View::TT::ForceUTF8'; | ||
| 91 | |||
| 92 | =head1 DESCRIPITON | ||
| 93 | |||
| 94 | Template View Class with utf8 encoding. | ||
| 95 | |||
| 96 | This allows you to prevent publishing garbled result. | ||
| 97 | |||
| 98 | Now this module is deprecated. | ||
| 99 | http://dev.catalystframework.org/wiki/gettingstarted/tutorialsandhowtos/using_unicode | ||
| 100 | |||
| 101 | =head1 CONFIG | ||
| 102 | |||
| 103 | __PACKAGE__->config( | ||
| 104 | INCLUDE_PATH => [..], | ||
| 105 | TIMER => 0, | ||
| 106 | ... # and other View::TT's configuration. | ||
| 107 | STRICT_CONTENT_TYPE => 1, | ||
| 108 | DEFAULT_ENCODING => 'utf-8', | ||
| 109 | ); | ||
| 110 | |||
| 111 | |||
| 112 | =over 4 | ||
| 113 | |||
| 114 | =item DEFAULT_ENCODING | ||
| 115 | |||
| 116 | 'utf-8' is set by default. See more detail L<Template::Provider::Encoding>. | ||
| 117 | |||
| 118 | =item CONTENT TYPE | ||
| 119 | |||
| 120 | When you set *STRICT_CONTENT_TYPE* configuration, | ||
| 121 | It automatically set content-type 'application/xhtml+xml; charset=utf-8' | ||
| 122 | for browsers except MSIE. | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =head1 SEE ALSO | ||
| 127 | |||
| 128 | L<Catalyst::View::TT>, L<Template::Provider::Encoding> | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Lyo Kato, C<lyo.kato@gmail.com> | ||
| 133 | |||
| 134 | =head1 LISENCE | ||
| 135 | |||
| 136 | The library if free software; you can redistribute it and/or modify | ||
| 137 | it under the same terms as Perl itself. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | 1; |