File Coverage

blib/lib/Template/Plugin/DumbQuotes.pm
Criterion Covered Total %
statement 37 37 100.0
branch 4 4 100.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 2 2 100.0
total 54 55 98.1


line stmt bran cond sub pod time code
1             package Template::Plugin::DumbQuotes;
2              
3 2     2   22428 use vars qw($VERSION);
  2         3  
  2         103  
4             $VERSION = "0.01";
5 2     2   9 use strict;
  2         2  
  2         57  
6 2     2   9 use warnings;
  2         11  
  2         51  
7              
8 2     2   1703 use Template::Plugin::Filter;
  2         10402  
  2         61  
9 2     2   16 use base qw(Template::Plugin::Filter);
  2         5  
  2         139  
10 2     2   1062 use Encode;
  2         11757  
  2         173  
11 2     2   831 use utf8;
  2         13  
  2         13  
12              
13             =pod
14              
15             =head1 NAME
16              
17             Template::Plugin::DumbQuotes - Transform educated quotes to dumb quotes
18              
19             =head1 DESCRIPTION
20              
21             Installs a filter to change Smart Quotes (curly etc...) and similar characters
22             to plain/dumb equivalent (ASCII safe)
23              
24             =head1 SYNOPSIS
25              
26             [% USE DumbQuotes %]
27             [% FILTER dumb_quotes %]
28             “This is an example of smart-quotes”
29             [% END %]
30             [%# will be changed to :
31             "This is an example of smart-quotes"
32             %]
33            
34             [%# Specify another filter name for your convenience %]
35             [% USE DumbQuotes dq %]
36             [% | dq %][% | loc %]What’s up[% END %][% END %]
37              
38             =head1 FILTERS
39              
40             =over 4
41              
42             =item double quotes ” and “
43              
44             are changed to "
45              
46             =item guillemets «»
47              
48             are changed to "
49              
50             =item single quotes ‘’
51              
52             are changed to C<`> and C<'>
53              
54             =item dashes –—
55              
56             are changed to hyphen-minus C<->
57              
58             =item ellipsis …
59              
60             is changed to three dots : C<...>
61              
62             =back
63              
64              
65             =head1 INTERNALS
66              
67             =head2 init
68              
69             init respects TT interface to initialise the filter
70              
71             =cut
72              
73             sub init {
74 6     6 1 78165 my $self = shift;
75 6   50     45 my $name = $self->{ _ARGS }->[ 0 ] || 'dumb_quotes';
76 6         29 $self->install_filter($name);
77 6         217 return $self;
78             }
79              
80             =head2 filter
81              
82             uses a regexp internally to change the text in input.
83              
84             =cut
85              
86             sub filter {
87 6     6 1 336 my ($self, $text) = @_;
88 6         26 my $decoded = Encode::is_utf8($text);
89 6 100       16 unless ($decoded) {
90 2         11 $text = Encode::decode_utf8($text);
91             }
92 2     2   384 $text =~ y/«»”“‘’/""""`'/;
  2         5  
  2         21  
  6         140  
93 6         39 $text =~ s/[\x{2012}-\x{2015}]/-/g;
94 6         21 $text =~ s/…/.../g;
95 6 100       24 $text = Encode::encode_utf8($text) unless $decoded;
96 6         32 return $text;
97             }
98              
99             1;
100              
101             =head1 MOTIVATION
102              
103             The original reason why this plugin has been created is to decrease the number of
104             different but yet similar strings in translation files (Using L).
105             Indeed in templates, depending on the context, you want to use smort-quotes for
106             rich-capable user-agent, and in some other cases (exemple text-email templates) you
107             just want dumb-quotes.
108              
109             This plugins allows you to use the rich version in templates by filtering them out,
110             and still, you will only have an unique lexicon entry in your .po (or whatever).
111              
112             Example:
113              
114             [% |dumb_quotes %] [% |loc %]let’s roll[% END %] [% END %]
115              
116             =head1 UTF-8
117              
118             It should work flawlessly whether your template is unicode encoded or not.
119              
120             =head1 LICENSE
121              
122             I is free software; you may redistribute it and/or
123             modify it under the same terms as Perl itself.
124              
125             =head1 AUTHOR & COPYRIGHT
126              
127             Except where otherwise noted, I is Copyright 2005-2007
128             Six Apart, cpan (at) sixapart (dot) com. All rights reserved.
129              
130             =cut