| 1 | package Text::Xatena::Util; | ||||||||||
| 2 | |||||||||||
| 3 | 21 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 21 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 21 | our @EXPORT = qw(escape_html template unindent); | |||||||||
| 7 | 21 | ▶ | use Exporter::Lite; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 21 | ▶ | use Text::MicroTemplate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 21 | my %escape = ( | |||||||||
| 11 | '&' => '&', | ||||||||||
| 12 | '<' => '<', | ||||||||||
| 13 | '>' => '>', | ||||||||||
| 14 | '"' => '"', | ||||||||||
| 15 | "'" => ''', | ||||||||||
| 16 | ); | ||||||||||
| 17 | 22 | ▶ | sub escape_html ($) { ## no critic | ||||||||
|
Subroutine
escape_html: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 18 | 22 | my ($str) = @_; | |||||||||
| 19 | 22 | my $escape = join "|", keys %escape; | |||||||||
| 20 | 22 | $str =~ s{($escape)}{ $escape{$1} }ego; | |||||||||
| 21 | 22 | $str; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 81 | ▶ | sub unindent ($) { ## no critic | ||||||||
|
Subroutine
unindent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 81 | my $string = shift; | |||||||||
| 26 | 81 | my ($indent) = ($string =~ /^\n?(\s*)/); | |||||||||
| 27 | 81 | $string =~ s/^$indent//gm; | |||||||||
| 28 | 81 | $string =~ s/\s+$//; | |||||||||
| 29 | 81 | $string; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 79 | ▶ | sub template ($$) { ## no critic | ||||||||
|
Subroutine
template: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 33 | 79 | my ($template, $keys) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 79 | my $mt = Text::MicroTemplate->new( | |||||||||
| 36 | tag_start => '{{', | ||||||||||
| 37 | tag_end => '}}', | ||||||||||
| 38 | template => unindent $template, | ||||||||||
| 39 | escape_func => undef, | ||||||||||
| 40 | ); | ||||||||||
| 41 | |||||||||||
| 42 | 79 | my $code = $mt->code; | |||||||||
| 43 | 79 | my $expand = join('; ', map { "my \$$_ = \$_[0]->{$_}" } @$keys); | |||||||||
| 44 | 79 | ▶ | my $renderer = eval << " ..." or die $@; ## no critic | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | sub { | ||||||||||
| 46 | $expand; | ||||||||||
| 47 | $code->(); | ||||||||||
| 48 | } | ||||||||||
| 49 | ... | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 21 | 1; | |||||||||
| 53 | __END__ | ||||||||||