| 1 | package XML::XSS::Template; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | $XML::XSS::Template::AUTHORITY = 'cpan:YANICK'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | $XML::XSS::Template::VERSION = '0.3.4'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: XML::XSS templates | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 15 | with qw(MooseX::Clone); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | ▶ | '&{}' => sub { $_[0]->compiled }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | 'bool' => sub { length $_[0]->code }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 15 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 15 | our @sigils = qw/ = ~ @ /; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | Moose::Exporter->setup_import_methods( as_is => ['xsst'], ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | sub xsst($) { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 18 | ▶ | my $template = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xsst: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 18 | my ( undef, $filename, $line ) = caller; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 18 | return XML::XSS::Template->new( | |||||||||||||||||||||||||||||||||||||||||
| 36 | _filename => $filename, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | _line => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | template => $template, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 15 | has template => ( | |||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | has code => ( isa => 'Str', is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 52 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 15 | has compiled => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 56 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 15 | has _filename => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 60 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 15 | has _line => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 63 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 18 | $self->_parse_template; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 18 | my $sub = <<"END_SUB"; | |||||||||||||||||||||||||||||||||||||||||
| 72 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 73 | my ( \$style, \$node, \$args ) = \@_; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | local *STDOUT; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | my \$output; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | open STDOUT, '>', \\\$output or die; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 18 | @{[ $self->code ]} | |||||||||||||||||||||||||||||||||||||||||
| 78 | return \$output; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | END_SUB | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 18 | $self->set_code($sub); | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 18 | $self->set_compiled( eval $sub ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 18 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | sub _parse_template { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_template: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 18 | my $sigil_re = '[' . join( '', @sigils ) . ']'; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 18 | my @tokens = split /(<-?%$sigil_re?|%-?>)/, $self->template; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 18 | my @parsed; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | TOKEN: | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 18 | ▶ | while (@tokens) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 43 | my $token = shift @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 43 | ▶ | if ( $token =~ s/<(-?)%// ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 16 | ▶ | if ( $1 and @parsed and $parsed[-1][0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$1 and @parsed and $parsed[-1][0]
$1 and @parsed
MC/DC33%1/3
$1 and @parsed and $parsed[-1][0]
$1
@parsed
$parsed[-1][0]
Truth table50%2/4
A and B and C
A= $1B= @parsedC= $parsed[-1][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | $parsed[-1][1] =~ s/\s+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 16 | $self->_parse_block( $token, \@tokens, \@parsed ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # it's a verbatim block | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 27 | my ( $f, $l ) = ( $self->_filename, $self->_line ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 27 | $self->_set_line( $l + $token =~ y/\n// ); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 27 | ▶ | if ( @parsed and $parsed[-1][2] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@parsed and $parsed[-1][2]
MC/DC100%2/2
@parsed and $parsed[-1][2]
@parsed
$parsed[-1][2]
Truth table100%3/3
A and B
A= @parsedB= $parsed[-1][2]
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 3 | $token =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 27 | push @parsed, [ 1, $token, undef, $f, $l ]; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 18 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 18 | my ( $pf, $pl ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 18 | for my $block (@parsed) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 43 | ▶ | $code .= join( ' ', "\n#line ", $block->[4], $block->[3] ) . "\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$block->[4] == $pl and $block->[3] eq $pf
MC/DC50%1/2
$block->[4] == $pl and $block->[3] eq $pf
$block->[4] == $pl
$block->[3] eq $pf
Truth table66%2/3
A and B
A= $block->[4] == $plB= $block->[3] eq $pf
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | unless $block->[4] == $pl and $block->[3] eq $pf; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 43 | ( $pf, $pl ) = ( $block->[3], $block->[4] ); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 43 | ▶ | if ( $block->[0] and length $block->[1] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$block->[0] and length $block->[1]
MC/DC100%2/2
$block->[0] and length $block->[1]
$block->[0]
length $block->[1]
Truth table100%3/3
A and B
A= $block->[0]B= length $block->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 20 | $block->[1] =~ s/\|/\\\|/g; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 20 | $block->[1] = 'print(qq|' . quotemeta($block->[1]) . '|);'; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 43 | $code .= $block->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 18 | return $self->set_code($code); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | sub _parse_block { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_block: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 16 | my ( $token, $tokens, $parsed ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 16 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 144 | my $closing_tag; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 16 | my $level = 1; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 16 | ▶ | while (@$tokens) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 32 | my $t = shift @$tokens; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 32 | ▶ | $level++ if $t =~ /\A<-?%/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 32 | ▶ | $level-- if $t =~ /\A%-?>/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 32 | ▶ | if ( $level == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 16 | $closing_tag = $t; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 16 | last; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 16 | $code .= $t; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 16 | my ( $f, $l ) = ( $self->_filename, $self->_line ); | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 16 | $self->_set_line( $l + $code =~ y/\n// ); | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 16 | ▶ | die "stylesheet <% %>s are unbalanced: <%$token $code\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | unless $closing_tag; | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 16 | given ($token) { | |||||||||||||||||||||||||||||||||||||||||
| 165 | 16 | when ('=') { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | $code = 'print(' . $code . ');'; | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 12 | when ('~') { | |||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | $code =~ s/\A\s+|\s+Z//g; # trim | |||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | $code =~ s/'/\\'/g; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | $code = | |||||||||||||||||||||||||||||||||||||||||
| 172 | qq{eval { print \$style->render(\$node->findnodes('$code'), \$args) } or warn $@;}; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 9 | when ('@') { | |||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | $code =~ s/\A\s+|\s+Z//g; # trim | |||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | $code =~ s/'/\\'/g; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | $code = qq{eval { print \$node->findvalue('$code') } or warn $@;}; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | default { | |||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # add a semi-colon if there is none | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 8 | ▶ | $code .= ';' unless $code =~ /;\s*\Z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 16 | push @$parsed, [ 0, $code, !!( $closing_tag =~ /-/ ), $f, $l ]; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | XML::XSS::Template - XML::XSS templates | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | version 0.3.4 | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | use XML::XSS; | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | my $xss = XML::XSS->new; | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | $xss.'chapter'.'content' *= xsst q{ | ||||||||||||||||||||||||||||||||||||||||||
| 211 | <%~ title %> | ||||||||||||||||||||||||||||||||||||||||||
| 212 | <%~ para %> | ||||||||||||||||||||||||||||||||||||||||||
| 213 | <%~ note %> | ||||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | XML::XSS::Template provides a templating markup language to ease the | ||||||||||||||||||||||||||||||||||||||||||
| 219 | writing of rules for XML::XSS stylesheet rules. So that the style | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | $xss.'chapter'.'content' *= sub { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | my ( $style, $node, $args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 223 | my $output; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | $output .= $style->render( $node->findnodes( 'title' ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | $output .= $style->render( $node->findnodes( 'para' ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | if ( my @notes = $node->findnodes('note') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 230 | $output .= '<div class="notes">' | ||||||||||||||||||||||||||||||||||||||||||
| 231 | . $style->render( @notes ) | ||||||||||||||||||||||||||||||||||||||||||
| 232 | . '</div>'; | ||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | return $output; | ||||||||||||||||||||||||||||||||||||||||||
| 236 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | can be written | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | $xss.'chapter'.'content' *= xsst q{ | ||||||||||||||||||||||||||||||||||||||||||
| 241 | <%~ title %> | ||||||||||||||||||||||||||||||||||||||||||
| 242 | <%~ para %> | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | <% if ( my @notes = $node->findnodes('note') ) { %> | ||||||||||||||||||||||||||||||||||||||||||
| 245 | <div class="notes"> | ||||||||||||||||||||||||||||||||||||||||||
| 246 | <%= $style->render( @notes ) %> | ||||||||||||||||||||||||||||||||||||||||||
| 247 | </div> | ||||||||||||||||||||||||||||||||||||||||||
| 248 | <% } %> | ||||||||||||||||||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =head1 TEMPLATE SYNTAX | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | The template directives are surrounded by '<%', '%>' delimiters. | ||||||||||||||||||||||||||||||||||||||||||
| 254 | An optional dash can be squeezed in ('<-%', '%->'), which will | ||||||||||||||||||||||||||||||||||||||||||
| 255 | cause all preceding or following whitespaces (including carriage returns) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | to be squished from the rendered document. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | This is useful to keep a stylesheet readable without | ||||||||||||||||||||||||||||||||||||||||||
| 258 | generating transformed document with many whitespace gaps. The dash can be | ||||||||||||||||||||||||||||||||||||||||||
| 259 | added independently to the right and left delimiter. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | For example | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | <h1> | ||||||||||||||||||||||||||||||||||||||||||
| 264 | <-%@ /doc/title %-> | ||||||||||||||||||||||||||||||||||||||||||
| 265 | </h1> | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | will be rendered as | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | <h1>A Tale of Two Cities</h1> | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | As an empty directive is an no-op, one can | ||||||||||||||||||||||||||||||||||||||||||
| 272 | take advantage of it and use '<-%%->' as | ||||||||||||||||||||||||||||||||||||||||||
| 273 | a magic template compacter. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =head2 Template Directives | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | =head3 <% %> | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | Evaluates the code enclosed without printing anything. | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | <% my $now = localtime %> | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | To make the directive output something, simply C<print> it. | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | <% print "oooh, shiny" if $thingy->albedo_index > 50 %> | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | To create a loop in your template, use two directives to wrap the opening and | ||||||||||||||||||||||||||||||||||||||||||
| 290 | closing pieces of code: | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | <% for my $item ( @shopping_list ) { %> | ||||||||||||||||||||||||||||||||||||||||||
| 293 | <p>I need a <%= $item %>.</p> | ||||||||||||||||||||||||||||||||||||||||||
| 294 | <% } %> | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =head3 <%= %> | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | Evaluates the enclosed code and prints its result. | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | Author: <%= 'Hi ' + $name %> | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =head3 <%# %> | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | Comments out the enclosed text, which will neither be executed or | ||||||||||||||||||||||||||||||||||||||||||
| 305 | show in the rendered document. | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | =head3 <%~ $xpath %> | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | Takes the XPath expression, applies it on the current | ||||||||||||||||||||||||||||||||||||||||||
| 310 | node and renders the resulting nodes. Equivalent of doing | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | <%= $style->render( $node->findnodes( $xpath ), $args ) %> | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | $xss->set( chapter => { content => <<'END_CONTENT' } ); | ||||||||||||||||||||||||||||||||||||||||||
| 317 | <%~ title %> <%# process the title node %> | ||||||||||||||||||||||||||||||||||||||||||
| 318 | <%~ para %> <%# ... and then the paragraphs %> | ||||||||||||||||||||||||||||||||||||||||||
| 319 | <%~ note %> <%# ... and the notes %> | ||||||||||||||||||||||||||||||||||||||||||
| 320 | END_CONTENT | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | =head3 <%@ $xpath %> | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | Takes the XPath expression and prints its value. Equivalent of doing | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | <%= $node->findvalue( $xpath ) %> | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =head2 xsst( $template ) | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | Takes the template given as a string and convert it as a | ||||||||||||||||||||||||||||||||||||||||||
| 333 | C<XML::XSS::Template> object ready to be used by a style attribute | ||||||||||||||||||||||||||||||||||||||||||
| 334 | of the stylesheet. | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | my $template = xsst q{ | ||||||||||||||||||||||||||||||||||||||||||
| 337 | <div> | ||||||||||||||||||||||||||||||||||||||||||
| 338 | <h2>List of stuff</h2> | ||||||||||||||||||||||||||||||||||||||||||
| 339 | <%~ item %> | ||||||||||||||||||||||||||||||||||||||||||
| 340 | </div> | ||||||||||||||||||||||||||||||||||||||||||
| 341 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | $xss->set( list => { content => $template } ); | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | From the point of view of the stylesheet, the template object created by | ||||||||||||||||||||||||||||||||||||||||||
| 346 | C<xsst> is just another coderef, and will be passed the usual rendering node, | ||||||||||||||||||||||||||||||||||||||||||
| 347 | xml node and option hashref arguments. For convenience, those are already | ||||||||||||||||||||||||||||||||||||||||||
| 348 | made available as C<$style>, C<$node> and C<$args>. | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | my $template = xstt q{ | ||||||||||||||||||||||||||||||||||||||||||
| 351 | <h2><%= $style->stylesheet->stash->{section_nbr}++ %>. <%~ title %></h2> | ||||||||||||||||||||||||||||||||||||||||||
| 352 | <% for my $child ( $node->childNodes ) { %> | ||||||||||||||||||||||||||||||||||||||||||
| 353 | do something... | ||||||||||||||||||||||||||||||||||||||||||
| 354 | <% } %> | ||||||||||||||||||||||||||||||||||||||||||
| 355 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | =head2 template | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | The original template string. | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =head2 code | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | The code generated out of the original template, as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | my $template = xsst q{ Hello <%= $style->stylesheet->stash->{name} %> }; | ||||||||||||||||||||||||||||||||||||||||||
| 368 | print $template->code; | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | This software is copyright (c) 2013 by Yanick Champoux. | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 379 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | =cut | ||||||||||||||||||||||||||||||||||||||||||