/usr/share/perl5/Text/MicroTemplate.pm is in libtext-microtemplate-perl 0.24-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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | # modified for NanoA by kazuho, some modified by tokuhirom
# based on Mojo::Template. Copyright (C) 2008, Sebastian Riedel.
package Text::MicroTemplate;
require Exporter;
use strict;
use warnings;
use constant DEBUG => $ENV{MICRO_TEMPLATE_DEBUG} || 0;
use 5.00800;
use Scalar::Util;
our $VERSION = '0.24';
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(encoded_string build_mt render_mt);
our %EXPORT_TAGS = (
all => [ @EXPORT_OK ],
);
our $_mt_setter = '';
sub new {
my $class = shift;
my $self = bless {
code => undef,
comment_mark => '#',
expression_mark => '=',
line_start => '?',
template => undef,
tree => [],
tag_start => '<?',
tag_end => '?>',
escape_func => \&_inline_escape_html,
prepend => '',
package_name => undef, # defaults to caller
@_ == 1 ? ref($_[0]) ? %{$_[0]} : (template => $_[0]) : @_,
}, $class;
if (defined $self->{template}) {
$self->parse($self->{template});
}
unless (defined $self->{package_name}) {
$self->{package_name} = 'main';
my $i = 0;
while (my $c = caller(++$i)) {
if ($c !~ /^Text::MicroTemplate\b/) {
$self->{package_name} = $c;
last;
}
}
}
$self;
}
sub escape_func {
my $self = shift;
if (@_) {
$self->{escape_func} = shift;
}
$self->{escape_func};
}
sub package_name {
my $self = shift;
if (@_) {
$self->{package_name} = shift;
}
$self->{package_name};
}
sub template { shift->{template} }
sub code {
my $self = shift;
unless (defined $self->{code}) {
$self->_build();
}
$self->{code};
}
sub _build {
my $self = shift;
my $escape_func = $self->{escape_func} || '';
my $embed_escape_func = ref($escape_func) eq 'CODE'
? $escape_func
: sub{ $escape_func . "(@_)" };
# Compile
my @lines;
my $last_was_code;
my $last_text;
for my $line (@{$self->{tree}}) {
# New line
push @lines, '';
for (my $j = 0; $j < @{$line}; $j += 2) {
my $type = $line->[$j];
my $value = $line->[$j + 1];
if ($type ne 'text' && defined $last_text) {
# do not mess the start of current line, since it might be
# the start of "=pod", etc.
$lines[
$j == 0 && @lines >= 2 ? -2 : -1
] .= "\$_MT .=\"$last_text\";";
undef $last_text;
}
# Need to fix line ending?
my $newline = chomp $value;
# add semicolon to last line of code
if ($last_was_code && $type ne 'code') {
$lines[-1] .= ';';
undef $last_was_code;
}
# Text
if ($type eq 'text') {
# Quote and fix line ending
$value = quotemeta($value);
$value .= '\n' if $newline;
$last_text = defined $last_text ? "$last_text$value" : $value;
}
# Code
if ($type eq 'code') {
$lines[-1] .= $value;
$last_was_code = 1;
}
# Expression
if ($type eq 'expr') {
my $escaped = $embed_escape_func->('$_MT_T');
if ($newline && $value =~ /\n/) {
$value .= "\n"; # temporary workaround for t/13-heredoc.t
}
$lines[-1] .= "\$_MT_T = $value;\$_MT .= ref \$_MT_T eq 'Text::MicroTemplate::EncodedString' ? \$\$_MT_T : $escaped; \$_MT_T = '';";
}
}
}
# add semicolon to last line of code
if ($last_was_code) {
$lines[-1] .= "\n;";
}
# add last text line(s)
if (defined $last_text) {
$lines[-1] .= "\$_MT .=\"$last_text\";";
}
# Wrap
$lines[0] = q/sub { my $_MT = ''; local $/ . $self->{package_name} . q/::_MTREF = \$_MT; my $_MT_T = '';/ . (@lines ? $lines[0] : '');
$lines[-1] .= q/return $_MT; }/;
$self->{code} = join "\n", @lines;
return $self;
}
# I am so smart! I am so smart! S-M-R-T! I mean S-M-A-R-T...
sub parse {
my ($self, $tmpl) = @_;
$self->{template} = $tmpl;
# Clean start
delete $self->{tree};
delete $self->{code};
# Tags
my $line_start = quotemeta $self->{line_start};
my $tag_start = quotemeta $self->{tag_start};
my $tag_end = quotemeta $self->{tag_end};
my $cmnt_mark = quotemeta $self->{comment_mark};
my $expr_mark = quotemeta $self->{expression_mark};
# Tokenize
my $state = 'text';
my @lines = split /(\n)/, $tmpl;
my $tokens = [];
while (@lines) {
my $line = shift @lines;
my $newline = undef;
if (@lines) {
shift @lines;
$newline = 1;
}
if ($state eq 'text') {
# Perl line without return value
if ($line =~ /^$line_start\s+(.*)$/) {
push @{$self->{tree}}, ['code', $1];
next;
}
# Perl line with return value
if ($line =~ /^$line_start$expr_mark\s+(.+)$/) {
push @{$self->{tree}}, [
'expr', $1,
$newline ? ('text', "\n") : (),
];
next;
}
# Comment line, dummy token needed for line count
if ($line =~ /^$line_start$cmnt_mark/) {
push @{$self->{tree}}, [];
next;
}
}
# Escaped line ending?
if ($line =~ /(\\+)$/) {
my $length = length $1;
# Newline escaped
if ($length == 1) {
$line =~ s/\\$//;
}
# Backslash escaped
if ($length >= 2) {
$line =~ s/\\\\$/\\/;
$line .= "\n";
}
} else {
$line .= "\n" if $newline;
}
# Mixed line
for my $token (split /
(
$tag_start$expr_mark # Expression
|
$tag_start$cmnt_mark # Comment
|
$tag_start # Code
|
$tag_end # End
)
/x, $line) {
# handle tags and bail out
if ($token eq '') {
next;
} elsif ($token =~ /^$tag_end$/) {
$state = 'text';
next;
} elsif ($token =~ /^$tag_start$/) {
$state = 'code';
next;
} elsif ($token =~ /^$tag_start$cmnt_mark$/) {
$state = 'cmnt';
next;
} elsif ($token =~ /^$tag_start$expr_mark$/) {
$state = 'expr';
next;
}
# value
if ($state eq 'text') {
push @$tokens, $state, $token;
} elsif ($state eq 'cmnt') {
next; # ignore comments
} elsif ($state eq 'cont') {
$tokens->[-1] .= $token;
} else {
# state is code or expr
push @$tokens, $state, $token;
$state = 'cont';
}
}
if ($state eq 'text') {
push @{$self->{tree}}, $tokens;
$tokens = [];
}
}
push @{$self->{tree}}, $tokens
if @$tokens;
return $self;
}
sub _context {
my ($self, $text, $line) = @_;
my @lines = split /\n/, $text;
join '', map {
0 < $_ && $_ <= @lines ? sprintf("%4d: %s\n", $_, $lines[$_ - 1]) : ''
} ($line - 2) .. ($line + 2);
}
# Debug goodness
sub _error {
my ($self, $error, $line_offset, $from) = @_;
# Line
if ($error =~ /^(.*)\s+at\s+\(eval\s+\d+\)\s+line\s+(\d+)/) {
my $reason = $1;
my $line = $2 - $line_offset;
my $delim = '-' x 76;
my $report = "$reason at line $line in template passed from $from.\n";
my $template = $self->_context($self->{template}, $line);
$report .= "$delim\n$template$delim\n";
# Advanced debugging
if (DEBUG) {
my $code = $self->_context($self->code, $line);
$report .= "$code$delim\n";
$report .= $error;
}
return $report;
}
# No line found
return "Template error: $error";
}
# create raw string (that does not need to be escaped)
sub encoded_string {
Text::MicroTemplate::EncodedString->new($_[0]);
}
sub _inline_escape_html{
my($variable) = @_;
my $source = qq{
do{
$variable =~ s/([&><"'])/\$Text::MicroTemplate::_escape_table{\$1}/ge;
$variable;
}
}; #" for poor editors
$source =~ s/\n//g; # to keep line numbers
return $source;
}
our %_escape_table = ( '&' => '&', '>' => '>', '<' => '<', q{"} => '"', q{'} => ''' );
sub escape_html {
my $str = shift;
return ''
unless defined $str;
return $str->as_string
if ref $str eq 'Text::MicroTemplate::EncodedString';
$str =~ s/([&><"'])/$_escape_table{$1}/ge; #' for poor editors
return $str;
}
sub build_mt {
my $mt = Text::MicroTemplate->new(@_);
$mt->build();
}
sub build {
my $_mt = shift;
Scalar::Util::weaken($_mt) if $_mt_setter;
my $_code = $_mt->code;
my $_from = sub {
my $i = 0;
while (my @c = caller(++$i)) {
return "$c[1] at line $c[2]"
if $c[0] ne __PACKAGE__;
}
'';
}->();
my $line_offset = (() = ($_mt->{prepend} =~ /\n/sg)) + 5;
my $expr = << "...";
package $_mt->{package_name};
sub {
${_mt_setter}local \$SIG{__WARN__} = sub { print STDERR \$_mt->_error(shift, $line_offset, \$_from) };
$_mt->{prepend}
Text::MicroTemplate::encoded_string((
$_code
)->(\@_));
}
...
if(DEBUG >= 2){
DEBUG >= 3 ? die $expr : warn $expr;
}
my $die_msg;
{
local $@;
if (my $_builder = eval($expr)) {
return $_builder;
}
$die_msg = $_mt->_error($@, $line_offset, $_from);
}
die $die_msg;
}
sub render_mt {
my $builder = build_mt(shift);
$builder->(@_);
}
# ? $_mt->filter(sub { s/\s+//smg; s/[\r\n]//g; })->(sub { ... ? });
sub filter {
my ($self, $callback) = @_;
my $mtref = do {
no strict 'refs';
${"$self->{package_name}::_MTREF"};
};
my $before = $$mtref;
$$mtref = '';
return sub {
my $inner_func = shift;
$inner_func->(@_);
## sub { s/foo/bar/g } is a valid filter
## sub { DateTime::Format::Foo->parse_string(shift) } is valid too
local $_ = $$mtref;
my $retval = $callback->($$mtref);
no warnings 'uninitialized';
if (($retval =~ /^\d+$/ and $_ ne $$mtref) or (defined $retval and !$retval)) {
$$mtref = $before . $_;
} else {
$$mtref = $before . $retval;
}
}
}
package Text::MicroTemplate::EncodedString;
use strict;
use warnings;
use overload q{""} => sub { shift->as_string }, fallback => 1;
sub new {
my ($klass, $str) = @_;
bless \$str, $klass;
}
sub as_string {
my $self = shift;
$$self;
}
1;
__END__
=head1 NAME
Text::MicroTemplate - Micro template engine with Perl5 language
=head1 SYNOPSIS
use Text::MicroTemplate qw(:all);
# compile template, and render
$renderer = build_mt('hello, <?= $_[0] ?>');
$html = $renderer->('John')->as_string;
# or in one line
$html = render_mt('hello, <?= $_[0] ?>', 'John')->as_string;
# complex form
$mt = Text::MicroTemplate->new(
template => 'hello, <?= $query->param('user') ?>',
);
$code = $mt->code;
$renderer = eval << "..." or die $@;
sub {
my \$query = shift;
$code->();
}
...
$html = $renderer->(CGI->new)->as_string;
=head1 DESCRIPTION
Text::MicroTemplate is a standalone, fast, intelligent, extensible template engine with following features.
=head2 standalone
Text::MicroTemplate does not rely on other CPAN modules.
=head2 fast
Based on L<Mojo::Template>, expressions in the template is perl code.
=head2 intelligent
Text::MicroTemplate automatically escapes variables when and only when necessary.
=head2 extensible
Text::MicroTemplate does not provide features like template cache or including other files by itself. However, it is easy to add you own (that suites the most to your application), by wrapping the result of the module (which is a perl expression).
The module only provides basic building blocks for a template engine. Refer to L<Text::MicroTemplate::File> for higher-level interface.
=head1 TEMPLATE SYNTAX
The template language is Perl5 itself!
# output the result of expression with automatic escape
<?= $expr ?> (tag style)
?= $expr (per-line)
# execute perl code (tag style)
<? foo() ?>
? foo()
# comment (tag style)
<?# comment ?>
?# comment
# loops
<ul>
? for my $item (@list) {
<li><?= $item ?></li>
? }
</ul>
=head1 EXPORTABLE FUNCTIONS
=head2 build_mt($template)
Returns a subref that renders given template. Parameters are equivalent to Text::MicroTemplate->new.
# build template renderer at startup time and use it multiple times
my $renderer = build_mt('hello, <?= $_[0] ?>!');
sub run {
...
my $hello = $renderer->($query->param('user'));
...
}
=head2 render_mt($template, @args)
Utility function that combines build_mt and call to the generated template builder.
# render
$hello = render_mt('hello, <?= $_[0] ?>!', 'John');
# print as HTML
print $hello->as_string;
# use the result in another template (no double-escapes)
$enc = render_mt('<h1><?= $_[0] ?></h1>', $hello);
Internally, the function is equivalent to:
build_mt($template)->(@_);
=head2 encoded_string($str)
wraps given string to an object that will not be escaped by the template engine
=head1 OO-STYLE INTERFACE
Text::MicroTemplate provides OO-style interface to handle more complex cases.
=head2 new($template)
=head2 new(%args)
=head2 new(\%args)
Constructs template renderer. In the second or third form, parameters below are recognized.
=head3 template
template string (mandatory)
=head3 escape_func
escape function (defaults to L<Text::MicroTemplate::escape_html>), no escape when set to undef
=head3 package_name
package under where the renderer is compiled (defaults to caller package)
=head3 prepend
Prepends Perl code to the template.
=head2 code()
returns perl code that renders the template when evaluated
=head2 filter(sub filter_func { ... })->(sub { template lines })
filters given template lines
? $_mt->filter(sub { s/Hello/Good bye/g })->(sub {
Hello, John!
? })
=head1 DEBUG
The C<MICRO_TEMPLATE_DEBUG> environment variable helps debugging.
The value C<1> extends debugging messages, C<2> reports compiled
Perl code with C<warn()>, C<3> is like C<2> but uses C<die()>.
=head1 SEE ALSO
L<Text::MicroTemplate::File>
L<Text::MicroTemplate::Extended>
=head1 AUTHOR
Kazuho Oku E<lt>kazuhooku gmail.comE<gt>
Tokuhiro Matsuno E<lt>tokuhirom AAJKLFJEF GMAIL COME<gt>
The module is based on L<Mojo::Template> by Sebastian Riedel.
=head1 LICENSE
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
|