blib/lib/Text/Xatena/Util.pm

filter: Statementstmt 100% 31 / 31 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 90.7% 39 / 43 CC 2 SCAR 2.2
CC
2
Cov
97%
template
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« lib/Text/Xatena/Inline/Aggressive.pm ↑ Summary lib/Text/Xatena/Node/SeeMore.pm »
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 '<' => '&lt;',
13 '>' => '&gt;',
14 '"' => '&#34;',
15 "'" => '&#39;',
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
BranchTrueFalseTotal
my $renderer = eval " sub {\n $expand;\n $code->();\n }\n" or die $@ 79 0 79
45 sub {
46 $expand;
47 $code->();
48 }
49 ...
50 }
51  
52 21 1;
53 __END__
« lib/Text/Xatena/Inline/Aggressive.pm ↑ Summary lib/Text/Xatena/Node/SeeMore.pm »