/usr/share/perl5/Text/MicroMason/HTMLTemplate.pm is in libtext-micromason-perl 2.16-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 | package Text::MicroMason::HTMLTemplate;
require Text::MicroMason::Base;
require Text::MicroMason::TemplateDir;
require Text::MicroMason::StoreOne;
require Text::MicroMason::HasParams;
push @ISA, map "Text::MicroMason::$_", qw( TemplateDir StoreOne HasParams );
use strict;
######################################################################
my %param_mapping = ( ### <<== INCOMPLETE ###
global_vars => 'loop_global_vars',
cache => '-CompileCache',
path => '-TemplatePaths',
);
######################################################################
sub output { (shift)->execute_again( @_ ) }
######################################################################
my $prefix_re = '[tT][mM][pP][lL]_';
sub lex_token {
# warn " Lexer: " . pos($_) . " of " . length($_) . "\n";
# Tags in format "<TMPL_FOO>", "<TMPL_FOO NAME=VAR>", or "</TMPL_FOO>"
/\G \<(\/?)($prefix_re\w+)\s*(.*?)\> /gcxs
? ( ( $1 ? "tmpl_end" : lc($2) ) => { $_[0]->parse_args($3) } ) :
# Things that don't match the above
/\G ( (?: [^<] | <(?!\/?$prefix_re) )+ ) /gcxs ? ( 'text' => $1 ) :
# Lexer error
()
}
sub parse_args {
my $self = shift;
my $args = "$_[0]";
return () unless length($args);
return ( name => $args ) unless ( $args =~ /=/ );
my @tokens;
until ( $args =~ /\G\z/gc ) {
push ( @tokens,
$args =~ /\G \s* (\w+) \= (?: \"([^\"]+)\" | ( \w+ ) ) (?= \s | \z ) /gcxs
? ( lc($1) => ( defined($2) ? $2 : $3 ) ) :
$args =~ /\G ( .{0,20} ) /gcxs
&& die "Couldn't find applicable parsing rule at '$1'\n"
);
}
@tokens;
}
######################################################################
sub assemble_tmpl_var {
my ($self, $args) = @_;
my $output = "\$m->param( '$args->{name}' )";
if ( defined $args->{default} ) {
$output = "local \$_ = $output; defined ? \$_ : '$args->{default}'"
}
if ( $args->{escape} ) {
$output = "\$m->filter( $output, '$args->{escape}' )"
}
expr => "$output;"
}
sub assemble_tmpl_include {
my ($self, $args) = @_;
file => $args->{name}
}
sub assemble_tmpl_loop {
my ($self, $args) = @_;
if ( ! $self->{loop_context_vars} ) {
perl => q/foreach my $args ( $m->param( '/ . $args->{name} . q/' ) ) {
local $m->{params} = [ $args, $m->{loop_global_vars} ? @{$m->{params}} : () ];/
} else {
perl => q/my @loop = $m->param( '/ . $args->{name} . q/' );
foreach my $count ( 0 .. $#loop ) {
my $args = $loop[ $count ];
my %loop_context = (
__counter__ => $count,
__odd__ => ( $count % 2 ),
__first__ => ( $count == 0 ),
__inner__ => ( $count > 0 and $count < $#loop ),
__last__ => ( $count == $#loop ),
);
local $m->{params} = [ $args, \%loop_context, $m->{loop_global_vars} ? @{$m->{params}} : () ];
/
}
}
sub assemble_tmpl_if {
my ($self, $args) = @_;
perl => q/if ( $m->param( '/ . $args->{name} . q/' ) ) { /
}
sub assemble_tmpl_unless {
my ($self, $args) = @_;
perl => q/if ( ! $m->param( '/ . $args->{name} . q/' ) ) { /
}
sub assemble_tmpl_else {
perl => "} else {"
}
sub assemble_tmpl_end {
perl => "}"
}
######################################################################
use vars qw( %Filters );
sub defaults {
(shift)->NEXT('defaults'), filters => \%Filters,
}
# Output filtering
$Filters{1} = $Filters{html} = \&HTML::Entities::encode
if eval { require HTML::Entities};
$Filters{url} = \&URI::Escape::uri_escape if eval { require URI::Escape };
# $result = $mason->filter( @filters, $content );
sub filter {
my $self = shift;
my $content = pop;
foreach my $filter ( @_ ) {
my $function = ( ref $filter eq 'CODE' ) ? $filter :
$self->{filters}{ $filter } ||
$self->croak_msg("No definition for a filter named '$filter'" );
$content = &$function($content)
}
$content
}
######################################################################
1;
__END__
######################################################################
=head1 NAME
Text::MicroMason::HTMLTemplate - Alternate Syntax like HTML::Template
=head1 SYNOPSIS
Instead of using this class directly, pass its name to be mixed in:
use Text::MicroMason;
my $mason = Text::MicroMason::Base->new( -HTMLTemplate );
Use the standard compile and execute methods to parse and evalute templates:
print $mason->compile( text=>$template )->( @%args );
print $mason->execute( text=>$template, @args );
Or use HTML::Template's calling conventions:
$template = Text::MicroMason->new( -HTMLTemplate, filename=>'simple.tmpl' );
$template->param( %arguments );
print $template->output();
HTML::Template provides a syntax to embed values into a text template:
<TMPL_IF NAME="user_is_dave">
I'm sorry <TMPLVAR NAME="name">, I'm afraid I can't do that right now.
<TMPL_ELSE>
<TMPL_IF NAME="daytime_is_morning">
Good morning, <TMPLVAR NAME="name">!
<TMPL_ELSE>
Good afternoon, <TMPLVAR NAME="name">!
</TMPL_IF>
</TMPL_IF>
=head1 DESCRIPTION
This mixin class overrides several methods to allow MicroMason to emulate
the template syntax and some of the other features of HTML::Template.
This class automatically includes the following other mixins: TemplateDir, HasParams, and StoreOne.
=head2 Compatibility with HTML::Template
This is not a drop-in replacement for HTML::Template, as the implementation is quite different, but it should be able to process most existing templates without major changes.
This should allow current HTML::Template users to take advantage of
MicroMason's one-time compilation feature, which in theory could be faster
than HTML::Template's run-time interpretation. (No benchmarking yet.)
The following features of HTML::Template are not supported yet:
=over 4
=item *
Search path for files. (Candidate for separate mixin class or addition to TemplateDir.)
=item *
Many HTML::Template options are either unsupported or have different names and need to be mapped to equivalent sets of attributes. (Transform these in the new() method or croak if they're unsupported.)
=back
The following features of HTML::Template will likely never be supported due to fundamental differences in implementation:
=over 4
=item *
query() method
=back
Contributed patches to more closely support the behavior of HTML::Template
would be welcomed by the author.
=head2 Template Syntax
The following elements are recognized by the HTMLTemplate lexer:
=over 4
=item *
I<literal_text>
Anything not specifically parsed by the below rule is interpreted as literal text.
=item *
E<lt>TMPL_I<tagname>E<gt>
A template tag with no attributes.
=item *
E<lt>TMPL_I<tagname> I<varname>E<gt>
A template tag with a name attribute.
=item *
E<lt>TMPL_I<tagname> NAME=I<varname> I<option>=I<value> ...E<gt>
A template tag with one or more attributes.
=item *
E<lt>/TMPL_I<tagname>E<gt>
A closing template tag.
=back
The following tags are supported by the HTMLTemplate assembler:
=over 4
=item tmpl_var
E<lt>tmpl_var name=... ( default=... ) ( escape=... ) E<gt>
=item tmpl_include
E<lt>tmpl_include name=... E<gt>
=item tmpl_if
E<lt>tmpl_if name=... E<gt> ... E<lt>/tmpl_ifE<gt>
=item tmpl_unless
E<lt>tmpl_unless name=...E<gt> ... E<lt>/tmpl_unlessE<gt>
=item tmpl_else
E<lt>tmpl_elseE<gt>
=item tmpl_loop
E<lt>tmpl_loop name=...E<gt> ... E<lt>/tmpl_loopE<gt>
=back
=head2 Supported Attributes
=over 4
=item associate
Optional reference to a CGI parameter object or other object with a similar param() method.
=item loop_global_vars (HTML::Template's "global_vars")
If set to true, don't hide external parameters inside a loop scope.
=item loop_context_vars
If set to true, defines additional variables within each <TMPL_LOOP>: __counter__, which specifies the row index, and four boolean flags, __odd__, __first__, __inner__, and __last__.
=back
=head2 Public Methods
=over 4
=item new()
Creates a new Mason object. If a filename parameter is supplied, the corresponding file is compiled.
=item param()
Gets and sets parameter arguments. Similar to the param() method provied by HTML::Template and the CGI module.
=item output()
Executes the most-recently compiled template and returns the results.
Optionally accepts a filehandle to print the results to.
$template->output( print_to => *STDOUT );
=back
=head2 Private Methods
=over 4
=item lex_token
( $type, $value ) = $mason->lex_token();
Lexer for <TMPL_x> tags.
Attempts to parse a token from the template text stored in the global $_ and returns a token type and value. Returns an empty list if unable to parse further due to an error.
=item parse_args()
Lexer for arguments within a tag.
=item assemble_tmpl_*()
These methods define the mapping from the template tags to the equivalent Perl code.
=item filter()
Used to implement the escape option for tmpl_var.
=back
=head1 SEE ALSO
The interface being emulated is described in L<HTML::Template>.
For an overview of this templating framework, see L<Text::MicroMason>.
This is a mixin class intended for use with L<Text::MicroMason::Base>.
For distribution, installation, support, copyright and license
information, see L<Text::MicroMason::Docs::ReadMe>.
=cut
|