| 1 | # Copyrights 2003,2007-2013 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.00. | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | package OODoc::Template; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '0.16'; | ||
| 11 | |||
| 12 | |||
| 13 | use Log::Report 'oodoc-template'; | ||
| 14 | |||
| 15 | use IO::File (); | ||
| 16 | use File::Spec (); | ||
| 17 | use Data::Dumper qw(Dumper); | ||
| 18 | use Scalar::Util qw(weaken); | ||
| 19 | |||
| 20 | my @default_markers = ('<!--{', '}-->', '<!--{/', '}-->'); | ||
| 21 | |||
| 22 | |||
| 23 | sub new(@) | ||
| 24 | { my ($class, %args) = @_; | ||
| 25 | (bless {}, $class)->init(\%args); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub init($) | ||
| 29 | { my ($self, $args) = @_; | ||
| 30 | |||
| 31 | $self->{cached} = {}; | ||
| 32 | $self->{macros} = {}; | ||
| 33 | |||
| 34 | my $s = $self; weaken $s; # avoid circular ref | ||
| 35 | $args->{template} ||= sub { $s->includeTemplate(@_) }; | ||
| 36 | $args->{macro} ||= sub { $s->defineMacro(@_) }; | ||
| 37 | |||
| 38 | $args->{search} ||= '.'; | ||
| 39 | $args->{markers} ||= \@default_markers; | ||
| 40 | $args->{define} ||= sub { shift; (1, @_) }; | ||
| 41 | |||
| 42 | $self->pushValues($args); | ||
| 43 | $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | sub process($) | ||
| 48 | { my ($self, $templ) = (shift, shift); | ||
| 49 | |||
| 50 | my $values = @_==1 ? shift : @_ ? {@_} : {}; | ||
| 51 | |||
| 52 | my $tree # parse with real copy | ||
| 53 | = ref $templ eq 'SCALAR' ? $self->parseTemplate($$templ) | ||
| 54 | : ref $templ eq 'ARRAY' ? $templ | ||
| 55 | : $self->parseTemplate("$templ"); | ||
| 56 | |||
| 57 | defined $tree | ||
| 58 | or return (); | ||
| 59 | |||
| 60 | $self->pushValues($values) | ||
| 61 | if keys %$values; | ||
| 62 | |||
| 63 | my @output; | ||
| 64 | foreach my $node (@$tree) | ||
| 65 | { unless(ref $node) | ||
| 66 | { push @output, $node; | ||
| 67 | next; | ||
| 68 | } | ||
| 69 | |||
| 70 | my ($tag, $attr, $then, $else) = @$node; | ||
| 71 | |||
| 72 | my %attrs; | ||
| 73 | while(my($k, $v) = each %$attr) | ||
| 74 | { $attrs{$k} = ref $v ne 'ARRAY' ? $v | ||
| 75 | : @$v==1 ? scalar $self->valueFor(@{$v->[0]}) | ||
| 76 | : join '', | ||
| 77 | map {ref $_ eq 'ARRAY' ? scalar $self->valueFor(@$_) : $_} | ||
| 78 | @$v; | ||
| 79 | } | ||
| 80 | |||
| 81 | (my $value, my $attrs, $then, $else) | ||
| 82 | = $self->valueFor($tag, \%attrs, $then, $else); | ||
| 83 | |||
| 84 | unless(defined $then || defined $else) | ||
| 85 | { defined $value | ||
| 86 | or next; | ||
| 87 | |||
| 88 | ref $value ne 'ARRAY' && ref $value ne 'HASH' | ||
| 89 | or error __x"value for {tag} is {value}, must be single" | ||
| 90 | , tag => $tag, value => $value; | ||
| 91 | |||
| 92 | push @output, $value; | ||
| 93 | next; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $take_else | ||
| 97 | = !defined $value || (ref $value eq 'ARRAY' && @$value==0); | ||
| 98 | |||
| 99 | my $container = $take_else ? $else : $then; | ||
| 100 | |||
| 101 | defined $container | ||
| 102 | or next; | ||
| 103 | |||
| 104 | $self->pushValues($attrs) if keys %$attrs; | ||
| 105 | |||
| 106 | if($take_else) | ||
| 107 | { my ($nest_out, $nest_tree) = $self->process($container); | ||
| 108 | push @output, $nest_out; | ||
| 109 | $node->[3] = $nest_tree; | ||
| 110 | } | ||
| 111 | elsif(ref $value eq 'HASH') | ||
| 112 | { my ($nest_out, $nest_tree) = $self->process($container, $value); | ||
| 113 | push @output, $nest_out; | ||
| 114 | $node->[2] = $nest_tree; | ||
| 115 | } | ||
| 116 | elsif(ref $value eq 'ARRAY') | ||
| 117 | { foreach my $data (@$value) | ||
| 118 | { my ($nest_out, $nest_tree) = $self->process($container, $data); | ||
| 119 | push @output, $nest_out; | ||
| 120 | $node->[2] = $nest_tree; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | else | ||
| 124 | { my ($nest_out, $nest_tree) = $self->process($container); | ||
| 125 | push @output, $nest_out; | ||
| 126 | $node->[2] = $nest_tree; | ||
| 127 | } | ||
| 128 | |||
| 129 | $self->popValues if keys %$attrs; | ||
| 130 | } | ||
| 131 | |||
| 132 | $self->popValues if keys %$values; | ||
| 133 | |||
| 134 | wantarray ? (join('', @output), $tree) # LIST context | ||
| 135 | : defined wantarray ? join('', @output) # SCALAR context | ||
| 136 | : print @output; # VOID context | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub processFile($;@) | ||
| 141 | { my ($self, $filename) = (shift, shift); | ||
| 142 | |||
| 143 | my $values = @_==1 ? shift : {@_}; | ||
| 144 | $values->{source} ||= $filename; | ||
| 145 | |||
| 146 | my $cache = $self->{cached}; | ||
| 147 | |||
| 148 | my ($output, $tree, $template); | ||
| 149 | if(exists $cache->{$filename}) | ||
| 150 | { $tree = $cache->{$filename}; | ||
| 151 | $output = $self->process($tree, $values) | ||
| 152 | if defined $tree; | ||
| 153 | } | ||
| 154 | elsif($template = $self->loadFile($filename)) | ||
| 155 | { ($output, $tree) = $self->process($template, $values); | ||
| 156 | $cache->{$filename} = $tree; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | { $tree = $cache->{$filename} = undef; | ||
| 160 | } | ||
| 161 | |||
| 162 | defined $tree || defined wantarray | ||
| 163 | or error __x"cannot find template file {fn}", fn => $filename; | ||
| 164 | |||
| 165 | wantarray ? ($output, $tree) # LIST context | ||
| 166 | : defined wantarray ? $output # SCALAR context | ||
| 167 | : print $output; # VOID context | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | sub defineMacro($$$$) | ||
| 172 | { my ($self, $tag, $attrs, $then, $else) = @_; | ||
| 173 | my $name = delete $attrs->{name} | ||
| 174 | or error __x"macro requires a name"; | ||
| 175 | |||
| 176 | defined $else | ||
| 177 | and error __x"macros cannot have an else part ({macro})",macro => $name; | ||
| 178 | |||
| 179 | my %attrs = %$attrs; # for closure | ||
| 180 | $attrs{markers} = $self->valueFor('markers'); | ||
| 181 | |||
| 182 | $self->{macros}{$name} = | ||
| 183 | sub { my ($tag, $at) = @_; | ||
| 184 | $self->process($then, +{%attrs, %$at}); | ||
| 185 | }; | ||
| 186 | |||
| 187 | (); | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub valueFor($;$$$) | ||
| 193 | { my ($self, $tag, $attrs, $then, $else) = @_; | ||
| 194 | |||
| 195 | #warn "Looking for $tag"; | ||
| 196 | #warn Dumper $self->{values}; | ||
| 197 | for(my $set = $self->{values}; defined $set; $set = $set->{NEXT}) | ||
| 198 | { my $v = $set->{$tag}; | ||
| 199 | |||
| 200 | if(defined $v) | ||
| 201 | { # HASH defines container | ||
| 202 | # ARRAY defines container loop | ||
| 203 | # object or other things can be stored as well, but may get | ||
| 204 | # stringified. | ||
| 205 | return wantarray ? ($v, $attrs, $then, $else) : $v | ||
| 206 | if ref $v ne 'CODE'; | ||
| 207 | |||
| 208 | return wantarray | ||
| 209 | ? $v->($tag, $attrs, $then, $else) | ||
| 210 | : ($v->($tag, $attrs, $then, $else))[0] | ||
| 211 | } | ||
| 212 | |||
| 213 | return wantarray ? (undef, $attrs, $then, $else) : undef | ||
| 214 | if exists $set->{$tag}; | ||
| 215 | |||
| 216 | my $code = $set->{DYNAMIC}; | ||
| 217 | if(defined $code) | ||
| 218 | { my ($value, @other) = $code->($tag, $attrs, $then, $else); | ||
| 219 | return wantarray ? ($value, @other) : $value | ||
| 220 | if defined $value; | ||
| 221 | # and continue the search otherwise | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | wantarray ? (undef, $attrs, $then, $else) : undef; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | sub allValuesFor($;$$$) | ||
| 230 | { my ($self, $tag, $attrs, $then, $else) = @_; | ||
| 231 | my @values; | ||
| 232 | |||
| 233 | for(my $set = $self->{values}; defined $set; $set = $set->{NEXT}) | ||
| 234 | { | ||
| 235 | if(defined(my $v = $set->{$tag})) | ||
| 236 | { my $t = ref $v eq 'CODE' ? $v->($tag, $attrs, $then, $else) : $v; | ||
| 237 | push @values, $t if defined $t; | ||
| 238 | } | ||
| 239 | |||
| 240 | if(defined(my $code = $set->{DYNAMIC})) | ||
| 241 | { my $t = $code->($tag, $attrs, $then, $else); | ||
| 242 | push @values, $t if defined $t; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | @values; | ||
| 247 | } | ||
| 248 | |||
| 249 | |||
| 250 | sub pushValues($) | ||
| 251 | { my ($self, $attrs) = @_; | ||
| 252 | |||
| 253 | if(my $markers = $attrs->{markers}) | ||
| 254 | { my @markers = ref $markers eq 'ARRAY' ? @$markers | ||
| 255 | : map {s/\\\,//g; $_} split /(?!<\\)\,\s*/, $markers; | ||
| 256 | |||
| 257 | push @markers, $markers[0] . '/' | ||
| 258 | if @markers==2; | ||
| 259 | |||
| 260 | push @markers, $markers[1] | ||
| 261 | if @markers==3; | ||
| 262 | |||
| 263 | $attrs->{markers} | ||
| 264 | = [ map { ref $_ eq 'Regexp' ? $_ : qr/\Q$_/ } @markers ]; | ||
| 265 | } | ||
| 266 | |||
| 267 | if(my $search = $attrs->{search}) | ||
| 268 | { $attrs->{search} = [ split /\:/, $search ] | ||
| 269 | if ref $search ne 'ARRAY'; | ||
| 270 | } | ||
| 271 | |||
| 272 | $self->{values} = { %$attrs, NEXT => $self->{values} }; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | sub popValues() | ||
| 277 | { my $self = shift; | ||
| 278 | $self->{values} = $self->{values}{NEXT}; | ||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub includeTemplate($$$) | ||
| 283 | { my ($self, $tag, $attrs, $then, $else) = @_; | ||
| 284 | |||
| 285 | defined $then || defined $else | ||
| 286 | and error __x"template is not a container"; | ||
| 287 | |||
| 288 | if(my $fn = $attrs->{file}) | ||
| 289 | { my $output = $self->processFile($fn, $attrs); | ||
| 290 | $output = $self->processFile($attrs->{alt}, $attrs) | ||
| 291 | if !defined $output && $attrs->{alt}; | ||
| 292 | |||
| 293 | defined $output | ||
| 294 | or error __x"cannot find template file {fn}", fn => $fn; | ||
| 295 | |||
| 296 | return ($output); | ||
| 297 | } | ||
| 298 | |||
| 299 | if(my $name = $attrs->{macro}) | ||
| 300 | { my $macro = $self->{macros}{$name} | ||
| 301 | or error __x"cannot find macro {name}", name => $name; | ||
| 302 | |||
| 303 | return $macro->($tag, $attrs, $then, $else); | ||
| 304 | } | ||
| 305 | |||
| 306 | error __x"file or macro attribute required for template in {source}" | ||
| 307 | , source => $self->valueFor('source') || '??'; | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | sub loadFile($) | ||
| 312 | { my ($self, $relfn) = @_; | ||
| 313 | my $absfn; | ||
| 314 | |||
| 315 | if(File::Spec->file_name_is_absolute($relfn)) | ||
| 316 | { my $fn = File::Spec->canonpath($relfn); | ||
| 317 | $absfn = $fn if -f $fn; | ||
| 318 | } | ||
| 319 | |||
| 320 | unless($absfn) | ||
| 321 | { my @srcs = map { @$_ } $self->allValuesFor('search'); | ||
| 322 | foreach my $dir (@srcs) | ||
| 323 | { $absfn = File::Spec->rel2abs($relfn, $dir); | ||
| 324 | last if -f $absfn; | ||
| 325 | $absfn = undef; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | defined $absfn | ||
| 330 | or return undef; | ||
| 331 | |||
| 332 | my $in = IO::File->new($absfn, 'r'); | ||
| 333 | unless(defined $in) | ||
| 334 | { my $source = $self->valueFor('source') || '??'; | ||
| 335 | fault __x"Cannot read from {fn} in {file}", fn => $absfn, file=>$source; | ||
| 336 | } | ||
| 337 | |||
| 338 | \(join '', $in->getlines); # auto-close in | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | sub parse($@) | ||
| 343 | { my ($self, $template) = (shift, shift); | ||
| 344 | $self->process(\$template, @_); | ||
| 345 | } | ||
| 346 | |||
| 347 | |||
| 348 | sub parseTemplate($) | ||
| 349 | { my ($self, $template) = @_; | ||
| 350 | |||
| 351 | defined $template | ||
| 352 | or return undef; | ||
| 353 | |||
| 354 | my $markers = $self->valueFor('markers'); | ||
| 355 | |||
| 356 | # Remove white-space escapes | ||
| 357 | $template =~ s! \\ (?: \s* (?: \\ \s*)? \n)+ | ||
| 358 | (?: \s* (?= $markers->[0] | $markers->[3] ))? | ||
| 359 | !!mgx; | ||
| 360 | |||
| 361 | my @frags; | ||
| 362 | |||
| 363 | # NOT_$tag supported for backwards compat | ||
| 364 | while( $template =~ s!^(.*?) # text before container | ||
| 365 | $markers->[0] \s* | ||
| 366 | (?: IF \s* )? | ||
| 367 | (NOT (?:_|\s+) )? | ||
| 368 | ([\w.-]+) \s* # tag | ||
| 369 | (.*?) \s* # attributes | ||
| 370 | $markers->[1] | ||
| 371 | !!xs | ||
| 372 | ) | ||
| 373 | { push @frags, $1; | ||
| 374 | my ($not, $tag, $attr) = ($2, $3, $4); | ||
| 375 | my ($then, $else); | ||
| 376 | |||
| 377 | if($template =~ s! (.*?) # contained | ||
| 378 | ( $markers->[2] | ||
| 379 | \s* \Q$tag\E \s* # "our" tag | ||
| 380 | $markers->[3] | ||
| 381 | ) | ||
| 382 | !!xs) | ||
| 383 | { $then = $1; | ||
| 384 | my $endline = $2; | ||
| 385 | } | ||
| 386 | |||
| 387 | if($not) { ($then, $else) = (undef, $then) } | ||
| 388 | elsif(!defined $then) { } | ||
| 389 | elsif($then =~ s! $markers->[0] | ||
| 390 | \s* ELSE (?:_|\s+) | ||
| 391 | \Q$tag\E \s* | ||
| 392 | $markers->[1] | ||
| 393 | (.*) | ||
| 394 | !!xs) | ||
| 395 | { # ELSE_$tag for backwards compat | ||
| 396 | $else = $1; | ||
| 397 | } | ||
| 398 | |||
| 399 | push @frags, [$tag, $self->parseAttrs($attr), $then, $else]; | ||
| 400 | } | ||
| 401 | |||
| 402 | push @frags, $template; | ||
| 403 | \@frags; | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | sub parseAttrs($) | ||
| 408 | { my ($self, $string) = @_; | ||
| 409 | |||
| 410 | my %attrs; | ||
| 411 | while( $string =~ | ||
| 412 | s!^\s* (?: '([^']+)' # attribute name (might be quoted) | ||
| 413 | | "([^"]+)" | ||
| 414 | | (\w+) | ||
| 415 | ) | ||
| 416 | \s* (?: \= \>? \s* # an optional value | ||
| 417 | ( \"[^"]*\" # dquoted value | ||
| 418 | | \'[^']*\' # squoted value | ||
| 419 | | \$\{ [^}]+ \} # complex variable | ||
| 420 | | [^\s,]+ # unquoted value | ||
| 421 | ) | ||
| 422 | )? | ||
| 423 | \s* \,? # optionally separated by commas | ||
| 424 | !!xs) | ||
| 425 | { my ($k, $v) = ($1||$2||$3, $4); | ||
| 426 | unless(defined $v) | ||
| 427 | { $attrs{$k} = 1; | ||
| 428 | next; | ||
| 429 | } | ||
| 430 | |||
| 431 | if($v =~ m/^\'(.*)\'$/) | ||
| 432 | { # Single quoted parameter, no interpolation | ||
| 433 | $attrs{$k} = $1; | ||
| 434 | next; | ||
| 435 | } | ||
| 436 | |||
| 437 | $v =~ s/^\"(.*)\"$/$1/; | ||
| 438 | my @v = split /( \$\{[^\}]+\} | \$\w+ )/x, $v; | ||
| 439 | |||
| 440 | if(@v==1 && $v[0] !~ m/^\$/) | ||
| 441 | { $attrs{$k} = $v[0]; | ||
| 442 | next; | ||
| 443 | } | ||
| 444 | |||
| 445 | my @steps; | ||
| 446 | foreach (@v) | ||
| 447 | { if( m/^ (?: \$(\w+) | \$\{ (\w+) \s* \} ) $/x ) | ||
| 448 | { push @steps, [ $+ ]; | ||
| 449 | } | ||
| 450 | elsif( m/^ \$\{ (\w+) \s* ([^\}]+? \s* ) \} $/x ) | ||
| 451 | { push @steps, [ $1, $self->parseAttrs($2) ]; | ||
| 452 | } | ||
| 453 | else | ||
| 454 | { push @steps, $_ if length $_; | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | $attrs{$k} = \@steps; | ||
| 459 | } | ||
| 460 | |||
| 461 | error __x"attribute error in {tag}'", tag => $_[1] | ||
| 462 | if length $string; | ||
| 463 | |||
| 464 | \%attrs; | ||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 468 | 1; |