| 1 | package Template::Plugin::DumbQuotes; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use vars qw($VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | $VERSION = "0.01"; | |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | use Template::Plugin::Filter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use base qw(Template::Plugin::Filter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use Encode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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 | 6 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 74 | 6 | my $self = shift; | |||||||||||
| 75 | 6 | ▶ | my $name = $self->{ _ARGS }->[ 0 ] || 'dumb_quotes'; | ||||||||||
|
Condition50%1/2
$self->{'_ARGS'}[0] || 'dumb_quotes'
MC/DC0%0/1
$self->{'_ARGS'}[0] || 'dumb_quotes'
$self->{'_ARGS'}[0]
Truth table50%1/2
A
A= $self->{'_ARGS'}[0]
| |||||||||||||
| 76 | 6 | $self->install_filter($name); | |||||||||||
| 77 | 6 | return $self; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 filter | ||||||||||||
| 81 | |||||||||||||
| 82 | uses a regexp internally to change the text in input. | ||||||||||||
| 83 | |||||||||||||
| 84 | =cut | ||||||||||||
| 85 | |||||||||||||
| 86 | 6 | ▶ | sub filter { | ||||||||||
|
Subroutine
filter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 87 | 6 | my ($self, $text) = @_; | |||||||||||
| 88 | 6 | my $decoded = Encode::is_utf8($text); | |||||||||||
| 89 | 6 | ▶ | unless ($decoded) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 2 | $text = Encode::decode_utf8($text); | |||||||||||
| 91 | } | ||||||||||||
| 92 | 6 | $text =~ y/«»”“‘’/""""`'/; | |||||||||||
| 93 | 6 | $text =~ s/[\x{2012}-\x{2015}]/-/g; | |||||||||||
| 94 | 6 | $text =~ s/…/.../g; | |||||||||||
| 95 | 6 | ▶ | $text = Encode::encode_utf8($text) unless $decoded; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 6 | return $text; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 3 | 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<Locale::Maketext>). | ||||||||||||
| 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<Template::Plugin::DumbQuotes> 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<Template::Plugin::DumbQuotes> is Copyright 2005-2007 | ||||||||||||
| 128 | Six Apart, cpan (at) sixapart (dot) com. All rights reserved. | ||||||||||||
| 129 | |||||||||||||
| 130 | =cut | ||||||||||||