| 1 | package Template::Extract::Compile; | ||||||||||
| 2 | 1 | $Template::Extract::Compile::VERSION = '0.41'; | |||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | our ( $DEBUG, $EXACT ); | |||||||||
| 9 | 1 | my ( $paren_id, $block_id ); | |||||||||
| 10 | |||||||||||
| 11 | sub new { | ||||||||||
| 12 | 13 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 13 | 13 | my $self = {}; | |||||||||
| 14 | 13 | return bless( $self, $class ); | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | sub compile { | ||||||||||
| 18 | 12 | ▶ | my ( $self, $template, $parser ) = @_; | ||||||||
|
Subroutine
compile: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 19 | |||||||||||
| 20 | 12 | $self->_init(); | |||||||||
| 21 | |||||||||||
| 22 | 12 | ▶ | if ( defined $template ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 12 | $parser->{FACTORY} = ref($self); | |||||||||
| 24 | 12 | ▶ | $template = $$template if UNIVERSAL::isa( $template, 'SCALAR' ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 12 | $template =~ s/\n+$//; | |||||||||
| 26 | 12 | $template =~ s/\[%\s*(?:\.\.\.|_|__)\s*%\]/[% \/.*?\/ %]/g; | |||||||||
| 27 | 12 | $template =~ s/\[%\s*(\/.*?\/)\s*%\]/'[% "' . quotemeta($1) . '" %]'/eg; | |||||||||
| 28 | 12 | $template =~ s{ | |||||||||
| 29 | \[%\s*([a-zA-z0-9]+)\s*\=\~\s*(/.*?/)\s*%\] | ||||||||||
| 30 | }{ | ||||||||||
| 31 | 1 | '[% SET ' . $1 . ' = "' . quotemeta($2) . '" %]' | |||||||||
| 32 | }mxegi; | ||||||||||
| 33 | |||||||||||
| 34 | 12 | return $parser->parse($template)->{BLOCK}; | |||||||||
| 35 | } | ||||||||||
| 36 | 0 | return undef; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | # initialize temporary variables | ||||||||||
| 40 | sub _init { | ||||||||||
| 41 | 12 | ▶ | $paren_id = 0; | ||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||
| 42 | 12 | $block_id = 0; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | # utility function to add regex eval brackets | ||||||||||
| 46 | 68 | ▶ | sub _re { "(?{\n @_\n})" } | ||||||||
|
Subroutine
_re: called CC=1 SCAR=0.0
| |||||||||||
| 47 | |||||||||||
| 48 | # --- Factory API implementation begins here --- | ||||||||||
| 49 | |||||||||||
| 50 | sub template { | ||||||||||
| 51 | 12 | ▶ | my $regex = $_[1]; | ||||||||
|
Subroutine
template: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 52 | |||||||||||
| 53 | 12 | $regex =~ s/\*\*//g; | |||||||||
| 54 | 12 | $regex =~ s/\+\+/+/g; | |||||||||
| 55 | 12 | ▶ | $regex = "^$regex\$" if $EXACT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | |||||||||||
| 57 | # Deal with backtracking here -- substitute repeated occurences of | ||||||||||
| 58 | # the variable into backtracking sequences like (\1) | ||||||||||
| 59 | 12 | my %seen; | |||||||||
| 60 | 12 | $regex =~ s{( # entire sequence [1] | |||||||||
| 61 | \(\.\*\?\) # matching regex | ||||||||||
| 62 | \(\?\{ # post-matching regex... | ||||||||||
| 63 | \s* # whitespaces | ||||||||||
| 64 | _ext\( # capturing handler... | ||||||||||
| 65 | \( # inner cluster of... | ||||||||||
| 66 | \[ (.+?) \],\s* # var name [2] | ||||||||||
| 67 | \$.*?,\s* # dollar with ^N/counter | ||||||||||
| 68 | (\d+) # counter [3] | ||||||||||
| 69 | \) # ...end inner cluster | ||||||||||
| 70 | (.*?) # outer loop stack [4] | ||||||||||
| 71 | \) # ...end capturing handler | ||||||||||
| 72 | \s* # whitespaces | ||||||||||
| 73 | \}\) # ...end post-maching regex | ||||||||||
| 74 | )}{ | ||||||||||
| 75 | 41 | ▶ | if ($seen{$2, $4}) { # if var reoccured in the same loop | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 8 | "(\\$seen{$2, $4})"; # replace it with backtracker | |||||||||
| 77 | } | ||||||||||
| 78 | else { | ||||||||||
| 79 | 33 | $seen{$2, $4} = $3; | |||||||||
| 80 | 33 | ▶ | if ($+[0] == length $regex) { # otherwise, if it is the end | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | '(.*)' . substr( $1, 5 ); # make it greedy | |||||||||
| 82 | } | ||||||||||
| 83 | else { | ||||||||||
| 84 | 31 | $1; # otherwise, preserve the sequence | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | }gex; | ||||||||||
| 88 | |||||||||||
| 89 | 12 | return $regex; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | sub foreach { | ||||||||||
| 93 | 12 | ▶ | my $regex = $_[4]; | ||||||||
|
Subroutine
foreach: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 94 | |||||||||||
| 95 | # find out immediate children | ||||||||||
| 96 | 12 | my %vars = | |||||||||
| 97 | reverse( $regex =~ /_ext\(\(\[(\[?)('\w+').*?\], [^,]+, \d+\)\*\*/g ); | ||||||||||
| 98 | 12 | ▶ | my $vars = join( ',', map { $vars{$_} ? "\\$_" : $_ } sort keys %vars ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | |||||||||||
| 100 | # append this block's id into the _get calling chain | ||||||||||
| 101 | 12 | ++$block_id; | |||||||||
| 102 | 12 | ++$paren_id; | |||||||||
| 103 | 12 | $regex =~ s/\*\*/, $block_id**/g; | |||||||||
| 104 | 12 | $regex =~ s/\+\+/*/g; | |||||||||
| 105 | |||||||||||
| 106 | return ( | ||||||||||
| 107 | |||||||||||
| 108 | # sets $cur_loop | ||||||||||
| 109 | 12 | _re("_enter_loop($_[2], $block_id)") . | |||||||||
| 110 | |||||||||||
| 111 | # match loop content | ||||||||||
| 112 | "(?:\\n*?$regex)++()" . | ||||||||||
| 113 | |||||||||||
| 114 | # weed out partial matches | ||||||||||
| 115 | _re("_ext(([[$_[2],[$vars]]], \\'leave_loop', $paren_id)**)") . | ||||||||||
| 116 | |||||||||||
| 117 | # optional, implicit newline | ||||||||||
| 118 | "\\n*?" | ||||||||||
| 119 | ); | ||||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | sub get { | ||||||||||
| 123 | 49 | ▶ | return "(?:$1)" if $_[1] =~ m{^/(.*)/$}; | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 124 | |||||||||||
| 125 | 41 | ++$paren_id; | |||||||||
| 126 | |||||||||||
| 127 | # ** is the placeholder for parent loop ids | ||||||||||
| 128 | 41 | return "(.*?)" . _re("_ext(([$_[1]], \$$paren_id, $paren_id)\*\*)"); | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | sub set { | ||||||||||
| 132 | 3 | ▶ | my $regex = undef; | ||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 133 | |||||||||||
| 134 | 3 | ++$paren_id; | |||||||||
| 135 | |||||||||||
| 136 | 3 | ▶ | if ( $_[1][1] =~ m|^/(.*)/$| ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 1 | $regex = $1; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 3 | my $val = $_[1][1]; | |||||||||
| 141 | 3 | $val =~ s/^'(.*)'\z/$1/; | |||||||||
| 142 | 3 | $val = quotemeta($val); | |||||||||
| 143 | |||||||||||
| 144 | my $parents = | ||||||||||
| 145 | 3 | join( ',', map { $_[1][0][ $_ * 2 ] } ( 0 .. $#{ $_[1][0] } / 2 ) ); | |||||||||
| 146 | |||||||||||
| 147 | 3 | ▶ | if ( defined($regex) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 1 | return $1 . _re("_ext(([$parents], \$$paren_id, $paren_id)\*\*)"); | |||||||||
| 149 | } | ||||||||||
| 150 | else { | ||||||||||
| 151 | 2 | return '()' . _re("_ext(([$parents], \\\\'$val', $paren_id)\*\*)"); | |||||||||
| 152 | } | ||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | sub textblock { | ||||||||||
| 156 | 57 | ▶ | return quotemeta( $_[1] ); | ||||||||
|
Subroutine
textblock: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | sub block { | ||||||||||
| 160 | 24 | ▶ | my $rv = ''; | ||||||||
|
Subroutine
block: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 161 | 24 | ▶ | foreach my $chunk ( map "$_", @{ $_[1] || [] } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 121 | $chunk =~ s/^#line .*\n//; | |||||||||
| 163 | 121 | $rv .= $chunk; | |||||||||
| 164 | } | ||||||||||
| 165 | 24 | return $rv; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | sub quoted { | ||||||||||
| 169 | 9 | ▶ | my $rv = ''; | ||||||||
|
Subroutine
quoted: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||
| 170 | |||||||||||
| 171 | 9 | foreach my $token ( @{ $_[1] } ) { | |||||||||
| 172 | 9 | ▶ | if ( $token =~ m/^'(.+)'$/ ) { # nested hash traversal | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | $rv .= '$'; | |||||||||
| 174 | 0 | $rv .= "{$_}" foreach split( /','/, $1 ); | |||||||||
| 175 | } | ||||||||||
| 176 | else { | ||||||||||
| 177 | 9 | $rv .= $token; | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 9 | return $rv; | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | sub ident { | ||||||||||
| 185 | 53 | ▶ | return join( ',', map { $_[1][ $_ * 2 ] } ( 0 .. $#{ $_[1] } / 2 ) ); | ||||||||
|
Subroutine
ident: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | sub text { | ||||||||||
| 189 | 9 | ▶ | return $_[1]; | ||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | # debug routine to catch unsupported directives | ||||||||||
| 193 | sub AUTOLOAD { | ||||||||||
| 194 | 13 | ▶ | $DEBUG or return; | ||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=26.2
Branch: 1 branch
| |||||||||||
| 195 | |||||||||||
| 196 | 0 | require Data::Dumper; | |||||||||
| 197 | 0 | $Data::Dumper::Indent = $Data::Dumper::Indent = 1; | |||||||||
| 198 | |||||||||||
| 199 | 0 | our $AUTOLOAD; | |||||||||
| 200 | 0 | print "\n$AUTOLOAD -"; | |||||||||
| 201 | |||||||||||
| 202 | 0 | for my $arg ( 1 .. $#_ ) { | |||||||||
| 203 | 0 | print "\n [$arg]: "; | |||||||||
| 204 | 0 | ▶ | print ref( $_[$arg] ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | ? Data::Dumper->Dump( [ $_[$arg] ], ['__'] ) | ||||||||||
| 206 | : $_[$arg]; | ||||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | 0 | return ''; | |||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 1 | 1; | |||||||||
| 213 | |||||||||||
| 214 | __END__ | ||||||||||