line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Semantic; |
2
|
12
|
|
|
12
|
|
8898
|
use strict; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
440
|
|
3
|
12
|
|
|
12
|
|
65
|
use warnings; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
324
|
|
4
|
12
|
|
|
12
|
|
327
|
use 5.008000; |
|
12
|
|
|
|
|
55
|
|
|
12
|
|
|
|
|
630
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
6
|
12
|
|
|
12
|
|
66
|
use Carp; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
1163
|
|
7
|
12
|
|
|
12
|
|
74
|
use Scalar::Util qw/blessed/; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
1397
|
|
8
|
12
|
|
|
12
|
|
13755
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Template::Semantic::Document; |
10
|
|
|
|
|
|
|
use Template::Semantic::Filter; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
|
|
|
|
|
|
my ($class, %opt) = @_; |
14
|
|
|
|
|
|
|
my $self = bless { }, $class; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$self->{parser} = delete $opt{parser}; |
17
|
|
|
|
|
|
|
$self->{parser} ||= do { |
18
|
|
|
|
|
|
|
my $parser = XML::LibXML->new; |
19
|
|
|
|
|
|
|
$parser->no_network(1); # faster |
20
|
|
|
|
|
|
|
$parser->recover(2); # = recover_silently(1) = no warnings |
21
|
|
|
|
|
|
|
$parser->$_($opt{$_}) for keys %opt; |
22
|
|
|
|
|
|
|
$parser; |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
for (@Template::Semantic::Filter::EXPORT_OK) { |
26
|
|
|
|
|
|
|
$self->define_filter($_ => \&{'Template::Semantic::Filter::' . $_}); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
$self; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub define_filter { |
33
|
|
|
|
|
|
|
my ($self, $name, $code) = @_; |
34
|
|
|
|
|
|
|
$self->{filter}{$name} ||= $code; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub call_filter { |
38
|
|
|
|
|
|
|
my ($self, $name) = @_; |
39
|
|
|
|
|
|
|
$name ||= ""; |
40
|
|
|
|
|
|
|
my $filter = ref($self) ? $self->{filter}{$name} |
41
|
|
|
|
|
|
|
: Template::Semantic::Filter->can($name); |
42
|
|
|
|
|
|
|
$filter or croak "Filter $name not defined."; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub process { |
46
|
|
|
|
|
|
|
my $self = ref $_[0] ? shift : shift->new; |
47
|
|
|
|
|
|
|
my ($template, $vars) = @_; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $source; |
50
|
|
|
|
|
|
|
if (ref($template) eq 'SCALAR') { |
51
|
|
|
|
|
|
|
$source = $$template; |
52
|
|
|
|
|
|
|
} elsif (ref($template) eq 'GLOB' |
53
|
|
|
|
|
|
|
or blessed($template) && $template->isa('GLOB')) { |
54
|
|
|
|
|
|
|
$source = do { local $/; <$template> }; |
55
|
|
|
|
|
|
|
} else { |
56
|
|
|
|
|
|
|
open(my $fh, '<', $template) or croak $!; |
57
|
|
|
|
|
|
|
$source = do { local $/; <$fh> }; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $doc = Template::Semantic::Document->new( |
61
|
|
|
|
|
|
|
engine => $self, |
62
|
|
|
|
|
|
|
source => $source || "", |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
$doc->process($vars || {}); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
__END__ |