File Coverage

blib/lib/Template/Plugin/Filter/IDN.pm
Criterion Covered Total %
statement 30 31 96.7
branch 7 8 87.5
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 47 51 92.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of Template-Plugin-Filter-IDN
3             #
4             # This software is copyright (c) 2018 by Michael Schout.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9              
10             package Template::Plugin::Filter::IDN;
11             $Template::Plugin::Filter::IDN::VERSION = '0.04';
12             # ABSTRACT: Template Toolkit plugin for encoding and decoding International Domain Names.
13              
14 1     1   47836 use strict;
  1         2  
  1         27  
15 1     1   5 use warnings;
  1         1  
  1         31  
16 1     1   5 use List::Util 'any';
  1         2  
  1         84  
17              
18 1     1   387 use parent 'Template::Plugin::Filter';
  1         276  
  1         7  
19              
20 1     1   2520 use Carp ();
  1         3  
  1         28  
21 1     1   388 use Net::IDN::Encode ();
  1         95841  
  1         560  
22              
23             our $DYNAMIC = 1;
24              
25             sub init {
26 13     13 0 56109 my $self = shift;
27              
28 13         43 $self->install_filter('idn');
29              
30 13         489 return $self;
31             }
32              
33             sub filter {
34 13     13 0 934 my ($self, $text, $args) = @_;
35              
36 13         25 my ($type) = @$args;
37              
38             # if no "type" was given, try to guess what we should do. If we have
39             # non-ascii chars, assume that we want to_ascii
40 13 100       30 unless (defined $type) {
41 2 100       10 $type = ($text =~ /[^ -~\s]/) ? 'to_ascii' : 'to_utf8';
42             }
43              
44 13 100   25   65 if (any { $_ eq $type } qw(encode to_ascii)) {
  25 50       59  
45 6         29 return Net::IDN::Encode::domain_to_ascii($text);
46             }
47 13     13   25 elsif (any { $_ eq $type } qw(decode to_utf8)) {
48 7         19 return Net::IDN::Encode::domain_to_unicode($text);
49             }
50             else {
51 0           Carp::croak "Unknown IDN filter action: $type";
52             }
53             }
54              
55             1;
56              
57             __END__