/usr/share/perl5/Dancer2/Template/Implementation/ForkedTiny.pm is in libdancer2-perl 0.204002+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | package Dancer2::Template::Implementation::ForkedTiny;
$Dancer2::Template::Implementation::ForkedTiny::VERSION = '0.204002';
# ABSTRACT: Dancer2 own implementation of Template::Tiny
use 5.00503;
use strict;
no warnings;
# Evaluatable expression
my $EXPR = qr/ [a-z_][\w.]* /xs;
sub new {
my $self = bless {
start_tag => '[%',
end_tag => '%]',
@_[ 1 .. $#_ ]
},
$_[0];
# Opening tag including whitespace chomping rules
my $LEFT = $self->{LEFT} = qr/
(?:
(?: (?:^|\n) [ \t]* )? \Q$self->{start_tag}\E\-
|
\Q$self->{start_tag}\E \+?
) \s*
/xs;
# Closing %] tag including whitespace chomping rules
my $RIGHT = $self->{RIGHT} = qr/
\s* (?:
\+? \Q$self->{end_tag}\E
|
\-\Q$self->{end_tag}\E (?: [ \t]* \n )?
)
/xs;
# Preparsing run for nesting tags
$self->{PREPARSE} = qr/
$LEFT ( IF | UNLESS | FOREACH ) \s+
(
(?: \S+ \s+ IN \s+ )?
\S+ )
$RIGHT
(?!
.*?
$LEFT (?: IF | UNLESS | FOREACH ) \b
)
( .*? )
(?:
$LEFT ELSE $RIGHT
(?!
.*?
$LEFT (?: IF | UNLESS | FOREACH ) \b
)
( .+? )
)?
$LEFT END $RIGHT
/xs;
$self->{CONDITION} = qr/
\Q$self->{start_tag}\E\s
( ([IUF])\d+ ) \s+
(?:
([a-z]\w*) \s+ IN \s+
)?
( $EXPR )
\s\Q$self->{end_tag}\E
( .*? )
(?:
\Q$self->{start_tag}\E\s \1 \s\Q$self->{end_tag}\E
( .+? )
)?
\Q$self->{start_tag}\E\s \1 \s\Q$self->{end_tag}\E
/xs;
$self;
}
# Copy and modify
sub preprocess {
my $self = shift;
my $text = shift;
$self->_preprocess( \$text );
return $text;
}
sub process {
my $self = shift;
my $copy = ${ shift() };
my $stash = shift || {};
local $@ = '';
local $^W = 0;
# Preprocess to establish unique matching tag sets
$self->_preprocess( \$copy );
# Process down the nested tree of conditions
my $result = $self->_process( $stash, $copy );
if (@_) {
${ $_[0] } = $result;
}
elsif ( defined wantarray ) {
require Carp;
Carp::carp(
'Returning of template results is deprecated in Template::Tiny 0.11'
);
return $result;
}
else {
print $result;
}
}
######################################################################
# Support Methods
# The only reason this is a standalone is so we can
# do more in-depth testing.
sub _preprocess {
my $self = shift;
my $copy = shift;
# Preprocess to establish unique matching tag sets
my $id = 0;
1 while $$copy =~ s/
$self->{PREPARSE}
/
my $tag = substr($1, 0, 1) . ++$id;
"\[\% $tag $2 \%\]$3\[\% $tag \%\]"
. (defined($4) ? "$4\[\% $tag \%\]" : '');
/sex;
}
sub _process {
my ( $self, $stash, $text ) = @_;
$text =~ s/
$self->{CONDITION}
/
($2 eq 'F')
? $self->_foreach($stash, $3, $4, $5)
: eval {
$2 eq 'U'
xor
!! # Force boolification
$self->_expression($stash, $4)
}
? $self->_process($stash, $5)
: $self->_process($stash, $6)
/gsex;
# Resolve expressions
$text =~ s/
$self->{LEFT} ( $EXPR ) $self->{RIGHT}
/
eval {
$self->_expression($stash, $1)
. '' # Force stringification
}
/gsex;
# Trim the document
$text =~ s/^\s*(.+?)\s*\z/$1/s if $self->{TRIM};
return $text;
}
# Special handling for foreach
sub _foreach {
my ( $self, $stash, $term, $expr, $text ) = @_;
# Resolve the expression
my $list = $self->_expression( $stash, $expr );
if ( ref $list ne 'ARRAY' ) {
return '';
}
# Iterate
return join '',
map { $self->_process( { %$stash, $term => $_ }, $text ) } @$list;
}
# Evaluates a stash expression
sub _expression {
my $cursor = $_[1];
my @path = split /\./, $_[2];
foreach (@path) {
# Support for private keys
return if substr( $_, 0, 1 ) eq '_';
# Split by data type
my $type = ref $cursor;
if ( $type eq 'ARRAY' ) {
return '' unless /^(?:0|[0-9]\d*)\z/;
$cursor = $cursor->[$_];
}
elsif ( $type eq 'HASH' ) {
$cursor = $cursor->{$_};
}
elsif ($type) {
$cursor = $cursor->$_();
}
else {
return '';
}
}
# If the last expression is a CodeRef, execute it
ref($cursor) eq 'CODE'
and $cursor = $cursor->();
return $cursor;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dancer2::Template::Implementation::ForkedTiny - Dancer2 own implementation of Template::Tiny
=head1 VERSION
version 0.204002
=head1 SYNOPSIS
my $template = Dancer2::Template::Implementation::ForkedTiny->new(
TRIM => 1,
);
# Print the template results to STDOUT
$template->process( <<'END_TEMPLATE', { foo => 'World' } );
Hello [% foo %]!
END_TEMPLATE
=head1 DESCRIPTION
B<Dancer2::Template::Implementation::ForkedTiny> is a reimplementation of a subset of the functionality from
L<Template> Toolkit in as few lines of code as possible.
It is intended for use in light-usage, low-memory, or low-cpu templating
situations, where you may need to upgrade to the full feature set in the
future, or if you want the retain the familiarity of TT-style templates.
For the subset of functionality it implements, it has fully-compatible template
and stash API. All templates used with B<Dancer2::Template::Implementation::ForkedTiny> should be able to be
transparently upgraded to full Template Toolkit.
Unlike Template Toolkit, B<Dancer2::Template::Implementation::ForkedTiny> will process templates without a
compile phase (but despite this is still quicker, owing to heavy use of
the Perl regular expression engine.
=head2 SUPPORTED USAGE
By default, the C<[% %]> tag style is used. You can change the start tag and
end tag by specifying them at object creation :
my $template = Dancer2::Template::Implementation::ForkedTiny->new(
start_tag => '<%',
end_tag => '%>,
);
In the rest of the documentation, C<[% %]> will be used, but it can be of
course your specified start / end tags.
Both the C<[%+ +%]> style explicit whitespace and the C<[%- -%]> style
explicit chomp B<are> support, although the C<[%+ +%]> version is unneeded
in practice as B<Dancer2::Template::Implementation::ForkedTiny> does not support default-enabled C<PRE_CHOMP>
or C<POST_CHOMP>.
Variable expressions in the form C<[% foo.bar.baz %]> B<are> supported.
Appropriate simple behaviours for C<ARRAY> references, C<HASH> references and
objects are supported. "VMethods" such as [% array.length %] are B<not>
supported at this time.
If the resulting expression is a CodeRef, it'll be evaluated.
C<IF>, C<ELSE> and C<UNLESS> conditional blocks B<are> supported, but only with
simple C<[% foo.bar.baz %]> conditions.
Support for looping (or rather iteration) is available in simple
C<[% FOREACH item IN list %]> form B<is> supported. Other loop structures are
B<not> supported. Because support for arbitrary or infinite looping is not
available, B<Dancer2::Template::Implementation::ForkedTiny> templates are not turing complete. This is
intentional.
All of the four supported control structures C<IF>/C<ELSE>/C<UNLESS>/C<FOREACH>
can be nested to arbitrary depth.
The treatment of C<_private> hash and method keys is compatible with
L<Template> Toolkit, returning null or false rather than the actual content
of the hash key or method.
Anything beyond the above is currently out of scope.
=head1 NAME
Dancer2::Template::Implementation::ForkedTiny - Template Toolkit reimplemented in as little code as possible, forked from Template::Tiny
=head1 METHODS
=head2 new
my $template = Dancer2::Template::Implementation::ForkedTiny->new(
TRIM => 1,
);
The C<new> constructor is provided for compatibility with Template Toolkit.
The only parameter it currently supports is C<TRIM> (which removes leading
and trailing whitespace from processed templates).
Additional parameters can be provided without error, but will be ignored.
=head2 process
# DEPRECATED: Return template results (emits a warning)
my $text = $template->process( \$input, $vars );
# Print template results to STDOUT
$template->process( \$input, $vars );
# Generate template results into a variable
my $output = '';
$template->process( \$input, $vars, \$output );
The C<process> method is called to process a template.
The first parameter is a reference to a text string containing the template
text. A reference to a hash may be passed as the second parameter containing
definitions of template variables.
If a third parameter is provided, it must be a scalar reference to be
populated with the output of the template.
For a limited amount of time, the old deprecated interface will continue to
be supported. If C<process> is called without a third parameter, and in
scalar or list contest, the template results will be returned to the caller.
If C<process> is called without a third parameter, and in void context, the
template results will be C<print()>ed to the currently selected file handle
(probably C<STDOUT>) for compatibility with L<Template>.
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Tiny>
For other issues, or commercial enhancement or support, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
Forked and improved by Damien Krotkine E<lt>dams@cpan.orgE<gt>
=head1 SEE ALSO
L<Config::Tiny>, L<CSS::Tiny>, L<YAML::Tiny>
=head1 COPYRIGHT
Copyright 2009 - 2011 Adam Kennedy.
Copyright 2012 Damien Krotkine.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|