| 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 | 2 | $Template::Plugin::Filter::IDN::VERSION = '0.03'; | |||||||||||||
| 12 | # ABSTRACT: Template Toolkit plugin for encoding and decoding International Domain Names. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 2 | ▶ | use syntax 'junction'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 2 | ▶ | use parent 'Template::Plugin::Filter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 2 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 2 | ▶ | use Net::IDN::Encode (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | our $DYNAMIC = 1; | |||||||||||||
| 24 | |||||||||||||||
| 25 | sub init { | ||||||||||||||
| 26 | 13 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 13 | $self->install_filter('idn'); | |||||||||||||
| 29 | |||||||||||||||
| 30 | 13 | return $self; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | sub filter { | ||||||||||||||
| 34 | 13 | ▶ | my ($self, $text, $args) = @_; | ||||||||||||
|
Subroutine
filter: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 13 | 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 | ▶ | unless (defined $type) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 2 | ▶ | $type = ($text =~ /[^ -~\s]/) ? 'to_ascii' : 'to_utf8'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 13 | ▶ | if ($type eq any(qw(encode to_ascii))) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 45 | 6 | return Net::IDN::Encode::domain_to_ascii($text); | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | elsif ($type eq any(qw(decode to_utf8))) { | ||||||||||||||
| 48 | 7 | return Net::IDN::Encode::domain_to_unicode($text); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | else { | ||||||||||||||
| 51 | 0 | Carp::croak "Unknown IDN filter action: $type"; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 2 | 1; | |||||||||||||
| 56 | |||||||||||||||
| 57 | __END__ | ||||||||||||||