| 1 | package Text::MiniTmpl; | ||||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use 5.012000; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use utf8; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||
| 8 | 11 | our $VERSION = 'v2.0.1'; | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Export::Attrs; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use JSON::XS qw( encode_json ); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use URI::Escape qw(); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use HTML::Entities qw(); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use constant UNSAFE_HTML=> '&"\'<>' . join q{},map{chr}0..8,11,12,14..31,127; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use constant RAWPRE => q{$}.__PACKAGE__.'::__ .= '; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use constant RAWPOST => q{}; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use constant UTF8PRE => q{$}.__PACKAGE__.'::__utf8 = '; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use constant UTF8POST => 'utf8::encode($'.__PACKAGE__.'::__utf8);' . | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 20 | 11 | q{$}.__PACKAGE__.'::__ .= $'.__PACKAGE__.'::__utf8;'; | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||
| 22 | 11 | our $__; | |||||||||||||||||||||||||||||
| 23 | 11 | our $__utf8; | |||||||||||||||||||||||||||||
| 24 | 11 | our $TMPL_DIR = q{./}; | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 11 | my %CACHE; | |||||||||||||||||||||||||||||
| 27 | 11 | my ($PRE, $POST) = (UTF8PRE, UTF8POST); | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub raw :Export { | ||||||||||||||||||||||||||||
|
Subroutine
raw: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 31 | 1 | my ($is_raw) = @_; | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | ($PRE, $POST) = $is_raw ? (RAWPRE, RAWPOST) : (UTF8PRE, UTF8POST); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 33 | 1 | return; | |||||||||||||||||||||||||||||
| 34 | 11 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | 55 | ▶ | sub render :Export { | ||||||||||||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 37 | 55 | my ($tmpl, %p) = @_; | |||||||||||||||||||||||||||||
| 38 | 55 | ▶ | my $path = $tmpl =~ m{\A[.]?/}xms ? $tmpl : "$TMPL_DIR$tmpl"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 55 | ▶ | 1 while $path =~ s{(\A|/) (?![.][.]?/) [^/]+/[.][.]/}{$1}xms; ## no critic(ProhibitPostfixControls) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 40 | 55 | my $pkg = caller; | |||||||||||||||||||||||||||||
| 41 | 55 | ▶ | $CACHE{$path}{$pkg} ||= tmpl2code($tmpl); | ||||||||||||||||||||||||||||
|
Condition100%3/3
$CACHE{$path}{$pkg} ||= tmpl2code($tmpl)
MC/DC100%2/2
$CACHE{$path}{$pkg} ||= tmpl2code($tmpl)
$CACHE{$path}{$pkg}
tmpl2code($tmpl)
Truth table100%3/3
A ||= B
A= $CACHE{$path}{$pkg}B= tmpl2code($tmpl)
| |||||||||||||||||||||||||||||||
| 42 | 51 | return ${ $CACHE{$path}{$pkg}->(%p) }; | |||||||||||||||||||||||||||||
| 43 | 11 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | 41 | ▶ | sub tmpl2code :Export { | ||||||||||||||||||||||||||||
|
Subroutine
tmpl2code: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||
| 46 | 41 | my ($tmpl) = @_; | |||||||||||||||||||||||||||||
| 47 | 41 | ▶ | my $path = $tmpl =~ m{\A[.]?/}xms ? $tmpl : "$TMPL_DIR$tmpl"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 48 | 41 | ▶ | 1 while $path =~ s{(\A|/) (?![.][.]?/) [^/]+/[.][.]/}{$1}xms; ## no critic(ProhibitPostfixControls) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 49 | 41 | my $dir = $path; | |||||||||||||||||||||||||||||
| 50 | 41 | $dir =~ s{/[^/]*\z}{/}xms; | |||||||||||||||||||||||||||||
| 51 | 41 | my $line = 1; | |||||||||||||||||||||||||||||
| 52 | 41 | my $pkg = caller; | |||||||||||||||||||||||||||||
| 53 | 41 | ▶ | if ($pkg eq __PACKAGE__) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 54 | 34 | $pkg = caller 1; | |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||
| 56 | 41 | my $e | |||||||||||||||||||||||||||||
| 57 | = 'package '.$pkg.'; use warnings; use strict;' | ||||||||||||||||||||||||||||||
| 58 | . 'sub {' | ||||||||||||||||||||||||||||||
| 59 | . 'local $'.__PACKAGE__.'::__ = q{};' | ||||||||||||||||||||||||||||||
| 60 | . 'local $'.__PACKAGE__."::TMPL_DIR = \"\Q$dir\E\";" | ||||||||||||||||||||||||||||||
| 61 | . 'local %_ = @_;' | ||||||||||||||||||||||||||||||
| 62 | . "\n#line $line \"$path\"\n" | ||||||||||||||||||||||||||||||
| 63 | ; | ||||||||||||||||||||||||||||||
| 64 | 41 | ▶ | open my $fh, '<', $path or croak "open: $!"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 65 | 41 | my $s = do { local $/ = undef; <$fh> }; | |||||||||||||||||||||||||||||
| 66 | 41 | ▶ | close $fh or croak "close: $!"; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 67 | 41 | while ( 1 ) { | |||||||||||||||||||||||||||||
| 68 | 261 | ▶ | $e .= | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 69 | $s=~/\G<!--& ( (?>[^-]*) .*? ) -->/xmsgc | ||||||||||||||||||||||||||||||
| 70 | ? "$1;" | ||||||||||||||||||||||||||||||
| 71 | : $s=~/\G&~ ( (?>[^~]*) .*? ) ~&/xmsgc | ||||||||||||||||||||||||||||||
| 72 | ? "$1;" | ||||||||||||||||||||||||||||||
| 73 | : $s=~/\G@~ ( (?>[^~]*) .*? ) ~@/xmsgc | ||||||||||||||||||||||||||||||
| 74 | ? $PRE."HTML::Entities::encode_entities(''.(do { $1; }), ".__PACKAGE__.'::UNSAFE_HTML);'.$POST | ||||||||||||||||||||||||||||||
| 75 | : $s=~/\G\#~ ( (?>[^~]*) .*? ) ~\#/xmsgc | ||||||||||||||||||||||||||||||
| 76 | ? $PRE."do { $1; };".$POST | ||||||||||||||||||||||||||||||
| 77 | : $s=~/\G\^~ ( (?>[^~]*) .*? ) ~\^/xmsgc | ||||||||||||||||||||||||||||||
| 78 | ? $PRE."URI::Escape::uri_escape_utf8(''.(do { $1; }));".$POST | ||||||||||||||||||||||||||||||
| 79 | : $s=~/\G ( (?>[^<&@\#^]*) .*? ) (?=<!--&|&~|@~|\#~|\^~|\z)/xmsgc | ||||||||||||||||||||||||||||||
| 80 | ? q{$}.__PACKAGE__."::__ .= \"\Q$1\E\";" | ||||||||||||||||||||||||||||||
| 81 | : last; | ||||||||||||||||||||||||||||||
| 82 | 220 | $line += $1 =~ tr/\n//; ## no critic (ProhibitCaptureWithoutTest) | |||||||||||||||||||||||||||||
| 83 | 220 | $e .= "\n#line $line \"$path\"\n"; | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||
| 85 | 41 | $e .= '; return \$'.__PACKAGE__.'::__ }'; | |||||||||||||||||||||||||||||
| 86 | # do instead of eval to have better diagnostics and support source filters | ||||||||||||||||||||||||||||||
| 87 | 41 | ▶ | open my $fhe, '<', \$e or croak "open: $!"; ## no critic(RequireBriefOpen) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 88 | 41 | ▶ | local @INC = ( sub {shift @INC; $fhe}, @INC ); | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 89 | 41 | my $code = do '[eval]'; | |||||||||||||||||||||||||||||
| 90 | 41 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 91 | 37 | return $code; | |||||||||||||||||||||||||||||
| 92 | 11 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub encode_js :Export { | ||||||||||||||||||||||||||||
|
Subroutine
encode_js: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 95 | 0 | my ($s) = @_; | |||||||||||||||||||||||||||||
| 96 | 0 | $s = quotemeta $s; | |||||||||||||||||||||||||||||
| 97 | 0 | $s =~ s/\n/n/xmsg; | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | while ($s =~ s/\G([^\\]*(?:\\[^.+-][^\\]*)*)\\([.+-])/$1$2/xmsg) {}; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 99 | 0 | return $s; | |||||||||||||||||||||||||||||
| 100 | 11 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub encode_js_data :Export { | ||||||||||||||||||||||||||||
|
Subroutine
encode_js_data: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||
| 103 | 0 | my ($s) = @_; | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if ($POST eq UTF8POST) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 105 | 0 | $s = JSON::XS->new->encode($s); | |||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||||
| 107 | 0 | $s = encode_json($s); | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | 0 | $s =~ s{</script}{<\\/script}xmsg; | |||||||||||||||||||||||||||||
| 110 | 0 | return $s; | |||||||||||||||||||||||||||||
| 111 | 11 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | 11 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||