aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso/TT.pm
blob: 8c4b216d459d5f1dc337fe58a8d9af450f957fee (plain)
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
package WebCoso::TT; 
use strict;
use warnings;
use WebCoso::Common;
use Path::Class;
use Template;
use Log::Log4perl ':easy';
 
sub new {
    my ($class,%opts)=@_;
 
    $opts{stash}||={
        dstUriFor => \&WebCoso::Common::dstUriFor,
        isLang => \&WebCoso::Common::isLang,
    };
 
    my $self={%opts};
 
    $self->{template_provider}=Template::Provider->new({
        INCLUDE_PATH=> $self->{TMPLPATH},
        ABSOLUTE=>1,
        RELATIVE=>1,
    });
    $self->{template}=Template->new({
        LOAD_TEMPLATES=>[$self->{template_provider}],
    });
 
    $self->{fc}->add_parser(
        qr{\.tt2?$} =>
               sub {
                   $self->{template}->context->template($_[0]);
               }); #$}); 
 
    $self->{expander}=sub {
        my ($maker,$target,$deps,$matches)=@_;
 
        DEBUG("expandTT($target,@$deps,@$matches)");
 
        my $tmpl=$self->{fc}->get($deps->[-1]);
        my $vars={ path=> $matches->[0],
                   language => $matches->[1],
                   %{$self->{stash}},
               };
        if (@$deps>1) {
            INFO("tagging as $deps->[0]");
            $vars->{tagged}=$self->{fc}->get($deps->[0]);
        }
        push @{$self->{template_provider}->include_path},
            file($deps->[-1])->parent;
        $self->{fc}->put($target,
                         $self->{template}->context->process($tmpl,
                                                             $vars));
        pop @{$self->{template_provider}->include_path};
    };
 
    bless $self,$class;
}
 
 
sub expandTT {
    my ($self)=@_;
 
    return $self->{expander}
 }
 
1;