| 1 | package Tags::Utils; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 27 | ▶ | use base qw(Exporter); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 27 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 27 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 27 | ▶ | use Error::Pure qw(err); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 27 | ▶ | use HTML::Entities; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 27 | ▶ | use Readonly; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | # Constants. | ||||||||||||||||||
| 12 | 27 | Readonly::Array our @EXPORT_OK => qw(encode_newline encode_attr_entities | |||||||||||||||||
| 13 | encode_char_entities); | ||||||||||||||||||
| 14 | 27 | Readonly::Scalar my $ATTR_CHARS => q{<&"}; | |||||||||||||||||
| 15 | 27 | Readonly::Scalar my $CHAR_CHARS => q{<&\240}; | |||||||||||||||||
| 16 | 27 | Readonly::Scalar my $EMPTY_STR => q{}; | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 27 | our $VERSION = 0.16; | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | # Encode newline in data to '\n' in output. | ||||||||||||||||||
| 21 | 1 | ▶ | sub encode_newline { | ||||||||||||||||
|
Subroutine
encode_newline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 22 | 1 | my $string = shift; | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 1 | $string =~ s/\n/\\n/gms; | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 1 | return $string; | |||||||||||||||||
| 27 | } | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | # Encode '<&"' attribute entities. | ||||||||||||||||||
| 30 | 92 | ▶ | sub encode_attr_entities { | ||||||||||||||||
|
Subroutine
encode_attr_entities: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||
| 31 | 92 | my $data_r = shift; | |||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 92 | ▶ | if (ref $data_r eq 'SCALAR') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 34 | 62 | ${$data_r} = encode_entities(decode_entities(${$data_r}), | |||||||||||||||||
| 35 | $ATTR_CHARS); | ||||||||||||||||||
| 36 | } elsif (ref $data_r eq 'ARRAY') { | ||||||||||||||||||
| 37 | 29 | foreach my $one_data (@{$data_r}) { | |||||||||||||||||
| 38 | 61 | encode_attr_entities(\$one_data); | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | } elsif (ref $data_r eq $EMPTY_STR) { | ||||||||||||||||||
| 41 | 1 | return encode_entities(decode_entities($data_r), $ATTR_CHARS); | |||||||||||||||||
| 42 | } else { | ||||||||||||||||||
| 43 | 0 | err 'Reference \''.(ref $data_r).'\' doesn\'t supported.'; | |||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | 91 | return; | |||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | # Encode '<&NBSP' char entities. | ||||||||||||||||||
| 49 | 77 | ▶ | sub encode_char_entities { | ||||||||||||||||
|
Subroutine
encode_char_entities: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||
| 50 | 77 | my $data_r = shift; | |||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 77 | ▶ | if (ref $data_r eq 'SCALAR') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 53 | 42 | ${$data_r} = encode_entities(decode_entities(${$data_r}), | |||||||||||||||||
| 54 | $CHAR_CHARS); | ||||||||||||||||||
| 55 | } elsif (ref $data_r eq 'ARRAY') { | ||||||||||||||||||
| 56 | 34 | foreach my $one_data (@{$data_r}) { | |||||||||||||||||
| 57 | 41 | encode_char_entities(\$one_data); | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | } elsif (ref $data_r eq $EMPTY_STR) { | ||||||||||||||||||
| 60 | 1 | return encode_entities(decode_entities($data_r), $CHAR_CHARS); | |||||||||||||||||
| 61 | } else { | ||||||||||||||||||
| 62 | 0 | err 'Reference \''.(ref $data_r).'\' doesn\'t supported.'; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | 76 | return; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 27 | 1; | |||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||