forked from ingydotnet/jemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jemplate
executable file
·20473 lines (18645 loc) · 434 KB
/
jemplate
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#BOOTSTRAP-BEGIN
# This is the standalone Jemplate compiler.
#
# All you need is this program and the program called `perl`. You don't need
# to install any Perl modules.
#
# If you downloaded this program from the internet, don't forget to put it in
# your path and make sure it is executable. Like this:
#
# mv jemplate /usr/local/bin/
# chmod +x /usr/local/bin/jemplate
#
# Try this command to make sure it works:
#
# jemplate --help
use Config;
BEGIN {
@INC = (
$Config::Config{archlib},
$Config::Config{privlib},
);
}
use strict;
use warnings;
#
# Inline include of Number/Compare.pm
#
BEGIN { $INC{'Number/Compare.pm'} = 'dummy/Number/Compare.pm'; }
BEGIN {
#line 0 "Number/Compare.pm"
package Number::Compare;
use strict;
use Carp qw(croak);
use vars qw/$VERSION/;
$VERSION = '0.03';
sub new {
my $referent = shift;
my $class = ref $referent || $referent;
my $expr = $class->parse_to_perl( shift );
bless eval "sub { \$_[0] $expr }", $class;
}
sub parse_to_perl {
shift;
my $test = shift;
$test =~ m{^
([<>]=?)? # comparison
(.*?) # value
([kmg]i?)? # magnitude
$}ix
or croak "don't understand '$test' as a test";
my $comparison = $1 || '==';
my $target = $2;
my $magnitude = $3 || '';
$target *= 1000 if lc $magnitude eq 'k';
$target *= 1024 if lc $magnitude eq 'ki';
$target *= 1000000 if lc $magnitude eq 'm';
$target *= 1024*1024 if lc $magnitude eq 'mi';
$target *= 1000000000 if lc $magnitude eq 'g';
$target *= 1024*1024*1024 if lc $magnitude eq 'gi';
return "$comparison $target";
}
sub test { $_[0]->( $_[1] ) }
1;
}
#
# Inline include of Text/Glob.pm
#
BEGIN { $INC{'Text/Glob.pm'} = 'dummy/Text/Glob.pm'; }
BEGIN {
#line 0 "Text/Glob.pm"
package Text::Glob;
use strict;
use Exporter;
use vars qw/$VERSION @ISA @EXPORT_OK
$strict_leading_dot $strict_wildcard_slash/;
$VERSION = '0.09';
@ISA = 'Exporter';
@EXPORT_OK = qw( glob_to_regex glob_to_regex_string match_glob );
$strict_leading_dot = 1;
$strict_wildcard_slash = 1;
use constant debug => 0;
sub glob_to_regex {
my $glob = shift;
my $regex = glob_to_regex_string($glob);
return qr/^$regex$/;
}
sub glob_to_regex_string
{
my $glob = shift;
my ($regex, $in_curlies, $escaping);
local $_;
my $first_byte = 1;
for ($glob =~ m/(.)/gs) {
if ($first_byte) {
if ($strict_leading_dot) {
$regex .= '(?=[^\.])' unless $_ eq '.';
}
$first_byte = 0;
}
if ($_ eq '/') {
$first_byte = 1;
}
if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' ||
$_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) {
$regex .= "\\$_";
}
elsif ($_ eq '*') {
$regex .= $escaping ? "\\*" :
$strict_wildcard_slash ? "[^/]*" : ".*";
}
elsif ($_ eq '?') {
$regex .= $escaping ? "\\?" :
$strict_wildcard_slash ? "[^/]" : ".";
}
elsif ($_ eq '{') {
$regex .= $escaping ? "\\{" : "(";
++$in_curlies unless $escaping;
}
elsif ($_ eq '}' && $in_curlies) {
$regex .= $escaping ? "}" : ")";
--$in_curlies unless $escaping;
}
elsif ($_ eq ',' && $in_curlies) {
$regex .= $escaping ? "," : "|";
}
elsif ($_ eq "\\") {
if ($escaping) {
$regex .= "\\\\";
$escaping = 0;
}
else {
$escaping = 1;
}
next;
}
else {
$regex .= $_;
$escaping = 0;
}
$escaping = 0;
}
print "# $glob $regex\n" if debug;
return $regex;
}
sub match_glob {
print "# ", join(', ', map { "'$_'" } @_), "\n" if debug;
my $glob = shift;
my $regex = glob_to_regex $glob;
local $_;
grep { $_ =~ $regex } @_;
}
1;
}
#
# Inline include of File/Find/Rule.pm
#
BEGIN { $INC{'File/Find/Rule.pm'} = 'dummy/File/Find/Rule.pm'; }
BEGIN {
#line 0 "File/Find/Rule.pm"
package File::Find::Rule;
use strict;
use File::Spec;
use Text::Glob 'glob_to_regex';
use Number::Compare;
use Carp qw/croak/;
use File::Find (); # we're only wrapping for now
our $VERSION = '0.33';
sub import {
my $pkg = shift;
my $to = caller;
for my $sym ( qw( find rule ) ) {
no strict 'refs';
*{"$to\::$sym"} = \&{$sym};
}
for (grep /^:/, @_) {
my ($extension) = /^:(.*)/;
eval "require File::Find::Rule::$extension";
croak "couldn't bootstrap File::Find::Rule::$extension: $@" if $@;
}
}
*rule = \&find;
sub find {
my $object = __PACKAGE__->new();
my $not = 0;
while (@_) {
my $method = shift;
my @args;
if ($method =~ s/^\!//) {
# jinkies, we're really negating this
unshift @_, $method;
$not = 1;
next;
}
unless (defined prototype $method) {
my $args = shift;
@args = ref $args eq 'ARRAY' ? @$args : $args;
}
if ($not) {
$not = 0;
@args = $object->new->$method(@args);
$method = "not";
}
my @return = $object->$method(@args);
return @return if $method eq 'in';
}
$object;
}
sub new {
my $referent = shift;
my $class = ref $referent || $referent;
bless {
rules => [],
subs => {},
iterator => [],
extras => {},
maxdepth => undef,
mindepth => undef,
}, $class;
}
sub _force_object {
my $object = shift;
$object = $object->new()
unless ref $object;
$object;
}
sub _flatten {
my @flat;
while (@_) {
my $item = shift;
ref $item eq 'ARRAY' ? push @_, @{ $item } : push @flat, $item;
}
return @flat;
}
sub name {
my $self = _force_object shift;
my @names = map { ref $_ eq "Regexp" ? $_ : glob_to_regex $_ } _flatten( @_ );
push @{ $self->{rules} }, {
rule => 'name',
code => join( ' || ', map { "m{$_}" } @names ),
args => \@_,
};
$self;
}
use vars qw( %X_tests );
%X_tests = (
-r => readable => -R => r_readable =>
-w => writeable => -W => r_writeable =>
-w => writable => -W => r_writable =>
-x => executable => -X => r_executable =>
-o => owned => -O => r_owned =>
-e => exists => -f => file =>
-z => empty => -d => directory =>
-s => nonempty => -l => symlink =>
=> -p => fifo =>
-u => setuid => -S => socket =>
-g => setgid => -b => block =>
-k => sticky => -c => character =>
=> -t => tty =>
-M => modified =>
-A => accessed => -T => ascii =>
-C => changed => -B => binary =>
);
for my $test (keys %X_tests) {
my $sub = eval 'sub () {
my $self = _force_object shift;
push @{ $self->{rules} }, {
code => "' . $test . ' \$_",
rule => "'.$X_tests{$test}.'",
};
$self;
} ';
no strict 'refs';
*{ $X_tests{$test} } = $sub;
}
use vars qw( @stat_tests );
@stat_tests = qw( dev ino mode nlink uid gid rdev
size atime mtime ctime blksize blocks );
{
my $i = 0;
for my $test (@stat_tests) {
my $index = $i++; # to close over
my $sub = sub {
my $self = _force_object shift;
my @tests = map { Number::Compare->parse_to_perl($_) } @_;
push @{ $self->{rules} }, {
rule => $test,
args => \@_,
code => 'do { my $val = (stat $_)['.$index.'] || 0;'.
join ('||', map { "(\$val $_)" } @tests ).' }',
};
$self;
};
no strict 'refs';
*$test = $sub;
}
}
sub any {
my $self = _force_object shift;
# compile all the subrules to code fragments
push @{ $self->{rules} }, {
rule => "any",
code => '(' . join( ' || ', map '( ' . $_->_compile . ' )', @_ ). ')',
args => \@_,
};
# merge all the subs hashes of the kids into ourself
%{ $self->{subs} } = map { %{ $_->{subs} } } $self, @_;
$self;
}
*or = \&any;
sub not {
my $self = _force_object shift;
push @{ $self->{rules} }, {
rule => 'not',
args => \@_,
code => '(' . join ( ' && ', map { "!(". $_->_compile . ")" } @_ ) . ")",
};
# merge all the subs hashes into us
%{ $self->{subs} } = map { %{ $_->{subs} } } $self, @_;
$self;
}
*none = \¬
sub prune () {
my $self = _force_object shift;
push @{ $self->{rules} },
{
rule => 'prune',
code => '$File::Find::prune = 1'
};
$self;
}
sub discard () {
my $self = _force_object shift;
push @{ $self->{rules} }, {
rule => 'discard',
code => '$discarded = 1',
};
$self;
}
sub exec {
my $self = _force_object shift;
my $code = shift;
push @{ $self->{rules} }, {
rule => 'exec',
code => $code,
};
$self;
}
sub grep {
my $self = _force_object shift;
my @pattern = map {
ref $_
? ref $_ eq 'ARRAY'
? map { [ ( ref $_ ? $_ : qr/$_/ ) => 0 ] } @$_
: [ $_ => 1 ]
: [ qr/$_/ => 1 ]
} @_;
$self->exec( sub {
local *FILE;
open FILE, $_ or return;
local ($_, $.);
while (<FILE>) {
for my $p (@pattern) {
my ($rule, $ret) = @$p;
return $ret
if ref $rule eq 'Regexp'
? /$rule/
: $rule->(@_);
}
}
return;
} );
}
for my $setter (qw( maxdepth mindepth extras )) {
my $sub = sub {
my $self = _force_object shift;
$self->{$setter} = shift;
$self;
};
no strict 'refs';
*$setter = $sub;
}
sub relative () {
my $self = _force_object shift;
$self->{relative} = 1;
$self;
}
sub DESTROY {}
sub AUTOLOAD {
our $AUTOLOAD;
$AUTOLOAD =~ /::not_([^:]*)$/
or croak "Can't locate method $AUTOLOAD";
my $method = $1;
my $sub = sub {
my $self = _force_object shift;
$self->not( $self->new->$method(@_) );
};
{
no strict 'refs';
*$AUTOLOAD = $sub;
}
&$sub;
}
sub in {
my $self = _force_object shift;
my @found;
my $fragment = $self->_compile;
my %subs = %{ $self->{subs} };
warn "relative mode handed multiple paths - that's a bit silly\n"
if $self->{relative} && @_ > 1;
my $topdir;
my $code = 'sub {
(my $path = $File::Find::name) =~ s#^(?:\./+)+##;
my @args = ($_, $File::Find::dir, $path);
my $maxdepth = $self->{maxdepth};
my $mindepth = $self->{mindepth};
my $relative = $self->{relative};
# figure out the relative path and depth
my $relpath = $File::Find::name;
$relpath =~ s{^\Q$topdir\E/?}{};
my $depth = scalar File::Spec->splitdir($relpath);
#print "name: \'$File::Find::name\' ";
#print "relpath: \'$relpath\' depth: $depth relative: $relative\n";
defined $maxdepth && $depth >= $maxdepth
and $File::Find::prune = 1;
defined $mindepth && $depth < $mindepth
and return;
#print "Testing \'$_\'\n";
my $discarded;
return unless ' . $fragment . ';
return if $discarded;
if ($relative) {
push @found, $relpath if $relpath ne "";
}
else {
push @found, $path;
}
}';
#use Data::Dumper;
#print Dumper \%subs;
#warn "Compiled sub: '$code'\n";
my $sub = eval "$code" or die "compile error '$code' $@";
for my $path (@_) {
# $topdir is used for relative and maxdepth
$topdir = $path;
# slice off the trailing slash if there is one (the
# maxdepth/mindepth code is fussy)
$topdir =~ s{/?$}{}
unless $topdir eq '/';
$self->_call_find( { %{ $self->{extras} }, wanted => $sub }, $path );
}
return @found;
}
sub _call_find {
my $self = shift;
File::Find::find( @_ );
}
sub _compile {
my $self = shift;
return '1' unless @{ $self->{rules} };
my $code = join " && ", map {
if (ref $_->{code}) {
my $key = "$_->{code}";
$self->{subs}{$key} = $_->{code};
"\$subs{'$key'}->(\@args) # $_->{rule}\n";
}
else {
"( $_->{code} ) # $_->{rule}\n";
}
} @{ $self->{rules} };
#warn $code;
return $code;
}
sub start {
my $self = _force_object shift;
$self->{iterator} = [ $self->in( @_ ) ];
$self;
}
sub match {
my $self = _force_object shift;
return shift @{ $self->{iterator} };
}
1;
}
#
# Inline include of Template/Constants.pm
#
BEGIN { $INC{'Template/Constants.pm'} = 'dummy/Template/Constants.pm'; }
BEGIN {
#line 0 "Template/Constants.pm"
package Template::Constants;
require Exporter;
use strict;
use warnings;
use Exporter;
use vars qw( @EXPORT_OK %EXPORT_TAGS );
use vars qw( $DEBUG_OPTIONS @STATUS @ERROR @CHOMP @DEBUG @ISA );
@ISA = qw( Exporter );
our $VERSION = 2.75;
use constant STATUS_OK => 0; # ok
use constant STATUS_RETURN => 1; # ok, block ended by RETURN
use constant STATUS_STOP => 2; # ok, stoppped by STOP
use constant STATUS_DONE => 3; # ok, iterator done
use constant STATUS_DECLINED => 4; # ok, declined to service request
use constant STATUS_ERROR => 255; # error condition
use constant ERROR_RETURN => 'return'; # return a status code
use constant ERROR_FILE => 'file'; # file error: I/O, parse, recursion
use constant ERROR_VIEW => 'view'; # view error
use constant ERROR_UNDEF => 'undef'; # undefined variable value used
use constant ERROR_PERL => 'perl'; # error in [% PERL %] block
use constant ERROR_FILTER => 'filter'; # filter error
use constant ERROR_PLUGIN => 'plugin'; # plugin error
use constant CHOMP_NONE => 0; # do not remove whitespace
use constant CHOMP_ALL => 1; # remove whitespace up to newline
use constant CHOMP_ONE => 1; # new name for CHOMP_ALL
use constant CHOMP_COLLAPSE => 2; # collapse whitespace to a single space
use constant CHOMP_GREEDY => 3; # remove all whitespace including newlines
use constant DEBUG_OFF => 0; # do nothing
use constant DEBUG_ON => 1; # basic debugging flag
use constant DEBUG_UNDEF => 2; # throw undef on undefined variables
use constant DEBUG_VARS => 4; # general variable debugging
use constant DEBUG_DIRS => 8; # directive debugging
use constant DEBUG_STASH => 16; # general stash debugging
use constant DEBUG_CONTEXT => 32; # context debugging
use constant DEBUG_PARSER => 64; # parser debugging
use constant DEBUG_PROVIDER => 128; # provider debugging
use constant DEBUG_PLUGINS => 256; # plugins debugging
use constant DEBUG_FILTERS => 512; # filters debugging
use constant DEBUG_SERVICE => 1024; # context debugging
use constant DEBUG_ALL => 2047; # everything
use constant DEBUG_CALLER => 4096; # add caller file/line
use constant DEBUG_FLAGS => 4096; # bitmask to extraxt flags
$DEBUG_OPTIONS = {
&DEBUG_OFF => off => off => &DEBUG_OFF,
&DEBUG_ON => on => on => &DEBUG_ON,
&DEBUG_UNDEF => undef => undef => &DEBUG_UNDEF,
&DEBUG_VARS => vars => vars => &DEBUG_VARS,
&DEBUG_DIRS => dirs => dirs => &DEBUG_DIRS,
&DEBUG_STASH => stash => stash => &DEBUG_STASH,
&DEBUG_CONTEXT => context => context => &DEBUG_CONTEXT,
&DEBUG_PARSER => parser => parser => &DEBUG_PARSER,
&DEBUG_PROVIDER => provider => provider => &DEBUG_PROVIDER,
&DEBUG_PLUGINS => plugins => plugins => &DEBUG_PLUGINS,
&DEBUG_FILTERS => filters => filters => &DEBUG_FILTERS,
&DEBUG_SERVICE => service => service => &DEBUG_SERVICE,
&DEBUG_ALL => all => all => &DEBUG_ALL,
&DEBUG_CALLER => caller => caller => &DEBUG_CALLER,
};
@STATUS = qw( STATUS_OK STATUS_RETURN STATUS_STOP STATUS_DONE
STATUS_DECLINED STATUS_ERROR );
@ERROR = qw( ERROR_FILE ERROR_VIEW ERROR_UNDEF ERROR_PERL
ERROR_RETURN ERROR_FILTER ERROR_PLUGIN );
@CHOMP = qw( CHOMP_NONE CHOMP_ALL CHOMP_ONE CHOMP_COLLAPSE CHOMP_GREEDY );
@DEBUG = qw( DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS
DEBUG_DIRS DEBUG_STASH DEBUG_CONTEXT DEBUG_PARSER
DEBUG_PROVIDER DEBUG_PLUGINS DEBUG_FILTERS DEBUG_SERVICE
DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS );
@EXPORT_OK = ( @STATUS, @ERROR, @CHOMP, @DEBUG );
%EXPORT_TAGS = (
'all' => [ @EXPORT_OK ],
'status' => [ @STATUS ],
'error' => [ @ERROR ],
'chomp' => [ @CHOMP ],
'debug' => [ @DEBUG ],
);
sub debug_flags {
my ($self, $debug) = @_;
my (@flags, $flag, $value);
$debug = $self unless defined($debug) || ref($self);
if ($debug =~ /^\d+$/) {
foreach $flag (@DEBUG) {
next if $flag =~ /^DEBUG_(OFF|ALL|FLAGS)$/;
# don't trash the original
my $copy = $flag;
$flag =~ s/^DEBUG_//;
$flag = lc $flag;
return $self->error("no value for flag: $flag")
unless defined($value = $DEBUG_OPTIONS->{ $flag });
$flag = $value;
if ($debug & $flag) {
$value = $DEBUG_OPTIONS->{ $flag };
return $self->error("no value for flag: $flag") unless defined $value;
push(@flags, $value);
}
}
return wantarray ? @flags : join(', ', @flags);
}
else {
@flags = split(/\W+/, $debug);
$debug = 0;
foreach $flag (@flags) {
$value = $DEBUG_OPTIONS->{ $flag };
return $self->error("unknown debug flag: $flag") unless defined $value;
$debug |= $value;
}
return $debug;
}
}
1;
}
#
# Inline include of Template/Base.pm
#
BEGIN { $INC{'Template/Base.pm'} = 'dummy/Template/Base.pm'; }
BEGIN {
#line 0 "Template/Base.pm"
package Template::Base;
use strict;
use warnings;
use Template::Constants;
our $VERSION = 2.78;
sub new {
my $class = shift;
my ($argnames, @args, $arg, $cfg);
{ no strict 'refs';
no warnings 'once';
$argnames = \@{"$class\::BASEARGS"} || [ ];
}
# shift off all mandatory args, returning error if undefined or null
foreach $arg (@$argnames) {
return $class->error("no $arg specified")
unless ($cfg = shift);
push(@args, $cfg);
}
# fold all remaining args into a hash, or use provided hash ref
$cfg = defined $_[0] && ref($_[0]) eq 'HASH' ? shift : { @_ };
my $self = bless {
(map { ($_ => shift @args) } @$argnames),
_ERROR => '',
DEBUG => 0,
}, $class;
return $self->_init($cfg) ? $self : $class->error($self->error);
}
sub error {
my $self = shift;
my $errvar;
{
no strict qw( refs );
$errvar = ref $self ? \$self->{ _ERROR } : \${"$self\::ERROR"};
}
if (@_) {
$$errvar = ref($_[0]) ? shift : join('', @_);
return undef;
}
else {
return $$errvar;
}
}
sub _init {
my ($self, $config) = @_;
return $self;
}
sub debug {
my $self = shift;
my $msg = join('', @_);
my ($pkg, $file, $line) = caller();
unless ($msg =~ /\n$/) {
$msg .= ($self->{ DEBUG } & Template::Constants::DEBUG_CALLER)
? " at $file line $line\n"
: "\n";
}
print STDERR "[$pkg] $msg";
}
sub module_version {
my $self = shift;
my $class = ref $self || $self;
no strict 'refs';
return ${"${class}::VERSION"};
}
1;
}
#
# Inline include of Template/Config.pm
#
BEGIN { $INC{'Template/Config.pm'} = 'dummy/Template/Config.pm'; }
BEGIN {
#line 0 "Template/Config.pm"
package Template::Config;
use strict;
use warnings;
use base 'Template::Base';
use vars qw( $VERSION $DEBUG $ERROR $INSTDIR
$PARSER $PROVIDER $PLUGINS $FILTERS $ITERATOR
$LATEX_PATH $PDFLATEX_PATH $DVIPS_PATH
$STASH $SERVICE $CONTEXT $CONSTANTS @PRELOAD );
$VERSION = 2.75;
$DEBUG = 0 unless defined $DEBUG;
$ERROR = '';
$CONTEXT = 'Template::Context';
$FILTERS = 'Template::Filters';
$ITERATOR = 'Template::Iterator';
$PARSER = 'Template::Parser';
$PLUGINS = 'Template::Plugins';
$PROVIDER = 'Template::Provider';
$SERVICE = 'Template::Service';
$STASH = 'Template::Stash::XS';
$CONSTANTS = 'Template::Namespace::Constants';
@PRELOAD = ( $CONTEXT, $FILTERS, $ITERATOR, $PARSER,
$PLUGINS, $PROVIDER, $SERVICE, $STASH );
$INSTDIR = '';
sub preload {
my $class = shift;
foreach my $module (@PRELOAD, @_) {
$class->load($module) || return;
};
return 1;
}
sub load {
my ($class, $module) = @_;
$module =~ s[::][/]g;
$module .= '.pm';
eval { require $module; };
return $@ ? $class->error("failed to load $module: $@") : 1;
}
sub parser {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($PARSER);
return $PARSER->new($params)
|| $class->error("failed to create parser: ", $PARSER->error);
}
sub provider {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($PROVIDER);
return $PROVIDER->new($params)
|| $class->error("failed to create template provider: ",
$PROVIDER->error);
}
sub plugins {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($PLUGINS);
return $PLUGINS->new($params)
|| $class->error("failed to create plugin provider: ",
$PLUGINS->error);
}
sub filters {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($FILTERS);
return $FILTERS->new($params)
|| $class->error("failed to create filter provider: ",
$FILTERS->error);
}
sub iterator {
my $class = shift;
my $list = shift;
return undef unless $class->load($ITERATOR);
return $ITERATOR->new($list, @_)
|| $class->error("failed to create iterator: ", $ITERATOR->error);
}
sub stash {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($STASH);
return $STASH->new($params)
|| $class->error("failed to create stash: ", $STASH->error);
}
sub context {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($CONTEXT);
return $CONTEXT->new($params)
|| $class->error("failed to create context: ", $CONTEXT->error);
}
sub service {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($SERVICE);
return $SERVICE->new($params)
|| $class->error("failed to create context: ", $SERVICE->error);
}
sub constants {
my $class = shift;
my $params = defined($_[0]) && ref($_[0]) eq 'HASH'
? shift : { @_ };
return undef unless $class->load($CONSTANTS);
return $CONSTANTS->new($params)
|| $class->error("failed to create constants namespace: ",
$CONSTANTS->error);
}
sub instdir {
my ($class, $dir) = @_;
my $inst = $INSTDIR
|| return $class->error("no installation directory");
$inst =~ s[/$][]g;