Lemplate.pm

filter: Statementstmt 11.8% 20 / 169 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 26.3% 5 / 19 Podpod 0.0% 0 / 13 totaltotal 7.3% 25 / 342 CC 66 SCAR 61.0
CC
66
Cov
7%
get_options
1056.0
main
182.0
make_file_list
90.0
CRAP 445.0
↑ Summary Lemplate/Directive.pm »
1 # ToDo:
2 # - Use TT:Simple in Makefiles
3  
4 # ABSTRACT: compiles Perl TT2 templates to standalone Lua modules for OpenResty
5  
6 package Lemplate;
7  
8 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Template 2.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = '0.15';
14  
15 8 ▶use Lemplate::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #-------------------------------------------------------------------------------
18  
19 8 our %ExtraTemplates;
20 8 our %ProcessedTemplates;
21 8 our $TemplateName;
22  
23 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
24 0 <<'...';
25 Usage:
26  
27 lemplate --runtime [runtime-opt]
28  
29 lemplate --compile [compile-opt] <template-list>
30  
31 lemplate --runtime [runtime-opt] --compile [compile-opt] <template-list>
32  
33 lemplate --list <template-list>
34  
35 Where "--runtime" and "runtime-opt" can include:
36  
37 --runtime Equivalent to --ajax=ilinsky --json=json2
38 --runtime=standard
39  
40 --runtime=lite Same as --ajax=none --json=none
41 --runtime=jquery Same as --ajax=jquery --json=none
42 --runtime=yui Same as --ajax=yui --json=yui
43 --runtime=legacy Same as --ajax=gregory --json=json2
44  
45 --json By itself, equivalent to --json=json2
46 --json=json2 Include http://www.json.org/json2.js for parsing/stringifying
47 --json=yui Use YUI: YAHOO.lang.JSON (requires external YUI)
48 --json=none Doesn't provide any JSON functionality except a warning
49  
50 --ajax By itself, equivalent to --ajax=xhr
51 --ajax=jquery Use jQuery for Ajax get and post (requires external jQuery)
52 --ajax=yui Use YUI: yui/connection/connection.js (requires external YUI)
53 --ajax=xhr Use XMLHttpRequest (will automatically use --xhr=ilinsky if --xhr is not set)
54 --ajax=none Doesn't provide any Ajax functionality except a warning
55  
56 --xhr By itself, equivalent to --xhr=ilinsky
57 --xhr=ilinsky Include http://code.google.com/p/xmlhttprequest/
58 --xhr=gregory Include http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/
59  
60 --xxx Include XXX and JJJ helper functions
61  
62 --compact Use the YUICompressor compacted version of the runtime
63  
64 Where "compile-opt" can include:
65  
66 --include_path=DIR Add directory to INCLUDE_PATH
67  
68 --start-tag
69 --end-tag
70 --pre-chomp
71 --post-chomp
72 --trim
73 --any-case
74 --eval
75 --noeval
76 -s, --source
77 --exclude
78  
79 For more information use:
80 perldoc lemplate
81 ...
82 }
83  
84 0 ▶sub main {
Subroutine main: not called CC=13 SCAR=52.0
Pod undocumented
85 0 my $class = shift;
86  
87 0 my @argv = @_;
88  
89 0 my ($template_options, $lemplate_options) = get_options(@argv);
90 0 my ($runtime, $compile, $list) = @$lemplate_options{qw/runtime compile list/};
91  
92 0 ▶ if ($runtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime) 0 0 0
93 0 print runtime_source_code(@$lemplate_options{qw/runtime ajax json xhr xxx compact/});
94 0 ▶ return unless $compile;
Branch: 1 branch
BranchTrueFalseTotal
unless $compile 0 0 0
95 }
96  
97 0 my $templates = make_file_list($lemplate_options->{exclude}, @argv);
98 0 ▶ print_usage_and_exit() unless @$templates;
Branch: 1 branch
BranchTrueFalseTotal
unless @$templates 0 0 0
99  
100 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 0 0 0
101 0 foreach (@$templates) {
102 0 print STDOUT $_->{short} . "\n";
103 }
104 0 return;
105 }
106  
107 0 ▶ if ($compile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compile) 0 0 0
108 0 my $lemplate = Lemplate->new(%$template_options);
109 0 print STDOUT $lemplate->_preamble;
110 0 ▶ for (my $i = 0; $i < @$templates; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$templates) 0 0 0
111 0 my $template = $templates->[$i];
112 #warn "processing $template->{short}";
113 0 my $content = slurp($template->{full});
114 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
115 0 %ExtraTemplates = ();
116 print STDOUT $lemplate->compile_template_content(
117 $content,
118 $template->{short}
119 0 );
120 0 my @new_files;
121 0 for my $new_template (keys %ExtraTemplates) {
122 0 ▶ if (!$ProcessedTemplates{$new_template}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Lemplate::ProcessedTemplates{$new_template}) 0 0 0
123 0 ▶ if (!-f $new_template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $new_template) 0 0 0
124 0 $new_template = "t/data/" . $new_template;
125 }
126 #warn $new_template;
127 0 ▶ if (-f $new_template) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $new_template) 0 0 0
128 #warn "adding new template $new_template";
129 0 push @new_files, $new_template;
130 }
131 }
132 }
133 0 push @$templates, @{ make_file_list({}, @new_files) };
134 }
135 }
136 0 print STDOUT "return _M\n";
137 0 return;
138 }
139  
140 0 print_usage_and_exit();
141 }
142  
143 0 ▶sub get_options {
Subroutine get_options: not called CC=32 SCAR=69.6
Pod undocumented
144 0 local @ARGV = @_;
145  
146 0 my $runtime;
147 0 my $compile = 0;
148 0 my $list = 0;
149  
150 my $start_tag = exists $ENV{LEMPLATE_START_TAG}
151 ? $ENV{LEMPLATE_START_TAG}
152 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_START_TAG'} ? : 0 0 0
153 my $end_tag = exists $ENV{LEMPLATE_END_TAG}
154 ? $ENV{LEMPLATE_END_TAG}
155 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_END_TAG'} ? : 0 0 0
156 my $pre_chomp = exists $ENV{LEMPLATE_PRE_CHOMP}
157 ? $ENV{LEMPLATE_PRE_CHOMP}
158 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_PRE_CHOMP'} ? : 0 0 0
159 my $post_chomp = exists $ENV{LEMPLATE_POST_CHOMP}
160 ? $ENV{LEMPLATE_POST_CHOMP}
161 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_POST_CHOMP'} ? : 0 0 0
162 my $trim = exists $ENV{LEMPLATE_TRIM}
163 ? $ENV{LEMPLATE_TRIM}
164 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_TRIM'} ? : 0 0 0
165 my $anycase = exists $ENV{LEMPLATE_ANYCASE}
166 ? $ENV{LEMPLATE_ANYCASE}
167 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_ANYCASE'} ? : 0 0 0
168 my $eval_javascript = exists $ENV{LEMPLATE_EVAL_JAVASCRIPT}
169 ? $ENV{LEMPLATE_EVAL_JAVASCRIPT}
170 0 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'LEMPLATE_EVAL_JAVASCRIPT'} ? : 0 0 0
171  
172 0 my $source = 0;
173 0 my $exclude = 0;
174 0 my ($ajax, $json, $xxx, $xhr, $compact, $minify);
175  
176 0 my $help = 0;
177 0 my @include_paths;
178  
179 0 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("compile|c", \$compile, "list|l", \$list, "runtime|r:s", \$runtime, "start-tag=s", \$start_tag, "end-tag=s", \$end_tag, "trim=s", \$trim, "pre-chomp", \$pre_chomp, "post-chomp", \$post_chomp, "any-case", \$anycase, "eval!", \$eval_javascript, "source|s", \$source, "exclude=s", \$exclude, "ajax:s", \$ajax, "json:s", \$json, "xxx", \$xxx, "xhr:s", \$xhr, "include_path", \@include_paths, "compact", \$compact, "minify:s", \$minify, "help|?", \$help) or print_usage_and_exit() 0 0 0
180 "compile|c" => \$compile,
181 "list|l" => \$list,
182 "runtime|r:s" => \$runtime,
183  
184 "start-tag=s" => \$start_tag,
185 "end-tag=s" => \$end_tag,
186 "trim=s" => \$trim,
187 "pre-chomp" => \$pre_chomp,
188 "post-chomp" => \$post_chomp,
189 "any-case" => \$anycase,
190 "eval!" => \$eval_javascript,
191  
192 "source|s" => \$source,
193 "exclude=s" => \$exclude,
194  
195 "ajax:s" => \$ajax,
196 "json:s" => \$json,
197 "xxx" => \$xxx,
198 "xhr:s" => \$xhr,
199  
200 "include_path" => \@include_paths,
201 "compact" => \$compact,
202 "minify:s" => \$minify,
203  
204 "help|?" => \$help,
205 ) or print_usage_and_exit();
206  
207 0 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 0 0
208 0 print_usage_and_exit();
209 }
210  
211 0 ▶ ($runtime, $ajax, $json, $xxx, $xhr, $minify) = map { defined $_ && ! length $_ ? 1 : $_ } ($runtime, $ajax, $json, $xxx, $xhr, $minify);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !length($_) ? : 0 0 0
Condition0%0/3
defined $_ and not length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and not length $_
defined $_ not length $_
Truth table0%0/3
A and not(B)
A= defined $_B= not length $_
ABresult
0X0
100
111
212 0 ▶ $runtime = "standard" if $runtime && $runtime eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $runtime and $runtime eq 1 0 0 0
Condition0%0/3
$runtime and $runtime eq 1
!ll&&!rl&&r
000
MC/DC0%0/2
$runtime and $runtime eq 1
$runtime $runtime eq 1
Truth table0%0/3
A and B
A= $runtimeB= $runtime eq 1
ABresult
0X0
100
111
213  
214 0 ▶ print_usage_and_exit("Don't understand '--runtime $runtime'") if defined $runtime && ! grep { $runtime =~ m/$_/ } qw/standard lite jquery yui legacy/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy") 0 0 0
Condition0%0/3
defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $runtime and not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
defined $runtime not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
Truth table0%0/3
A and not(B)
A= defined $runtimeB= not grep({$runtime =~ /$_/;} "standard", "lite", "jquery", "yui", "legacy")
ABresult
0X0
100
111
215 0 ▶ print_usage_and_exit("Can't specify --list with a --runtime and/or the --compile option") if $list && ($runtime || $compile);
Branch: 1 branch
BranchTrueFalseTotal
if $list and $runtime || $compile 0 0 0
Condition0%0/6
$list and $runtime || $compile
!ll&&!rl&&r
000
$runtime or $compile
l!l&&r!l&&!r
000
MC/DC0%0/3
$list and $runtime || $compile
$list $runtime $compile
Truth table0%0/4
A and B or C
A= $listB= $runtimeC= $compile
ABCresult
0XX0
1000
11X1
1011
216 0 ▶ print_usage_and_exit() unless $list || $runtime || $compile;
Branch: 1 branch
BranchTrueFalseTotal
unless $list or $runtime or $compile 0 0 0
Condition0%0/6
$list or $runtime or $compile
l!l&&r!l&&!r
000
$list or $runtime
l!l&&r!l&&!r
000
MC/DC0%0/3
$list or $runtime or $compile
$list $runtime $compile
Truth table0%0/4
A or B or C
A= $listB= $runtimeC= $compile
ABCresult
1XX1
01X1
0011
0000
217  
218 0 ▶ my $command =
Branch: 3 branches
BranchTrueFalseTotal
$runtime ? : 0 0 0
$compile ? : 0 0 0
$list ? : 0 0 0
219 $runtime ? 'runtime' :
220 $compile ? 'compile' :
221 $list ? 'list' :
222 print_usage_and_exit();
223  
224 0 my $options = {};
225 0 ▶ $options->{START_TAG} = $start_tag if defined $start_tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start_tag 0 0 0
226 0 ▶ $options->{END_TAG} = $end_tag if defined $end_tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end_tag 0 0 0
227 0 ▶ $options->{PRE_CHOMP} = $pre_chomp if defined $pre_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pre_chomp 0 0 0
228 0 ▶ $options->{POST_CHOMP} = $post_chomp if defined $post_chomp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $post_chomp 0 0 0
229 0 ▶ $options->{TRIM} = $trim if defined $trim;
Branch: 1 branch
BranchTrueFalseTotal
if defined $trim 0 0 0
230 0 ▶ $options->{ANYCASE} = $anycase if defined $anycase;
Branch: 1 branch
BranchTrueFalseTotal
if defined $anycase 0 0 0
231 0 ▶ $options->{EVAL_JAVASCRIPT} = $eval_javascript if defined $eval_javascript;
Branch: 1 branch
BranchTrueFalseTotal
if defined $eval_javascript 0 0 0
232 0 $options->{INCLUDE_PATH} = \@include_paths;
233  
234 return (
235 0 $options,
236 { compile => $compile, runtime => $runtime, list => $list,
237 source => $source,
238 exclude => $exclude,
239 ajax => $ajax, json => $json, xxx => $xxx, xhr => $xhr,
240 compact => $compact, minify => $minify },
241 );
242 }
243  
244  
245 0 ▶sub slurp {
Subroutine slurp: not called CC=2 SCAR=17.9
Pod undocumented
246 0 my $filepath = shift;
247 0 ▶ open(F, '<', $filepath) or die "Can't open '$filepath' for input:\n$!";
Branch: 1 branch
BranchTrueFalseTotal
open F, "<", $filepath or die "Can't open '${filepath}' for input:\n$!" 0 0 0
248 0 my $contents = do {local $/; <F>};
249 0 close(F);
250 0 return $contents;
251 }
252  
253 0 ▶sub recurse_dir {
Subroutine recurse_dir: not called CC=3 SCAR=24.8
Pod undocumented
254 0 require File::Find::Rule;
255  
256 0 my $dir = shift;
257 0 my @files;
258 0 foreach ( File::Find::Rule->file->in( $dir ) ) {
259 0 ▶ if ( m{/\.[^\.]+} ) {} # Skip ".hidden" files or directories
Branch: 1 branch
BranchTrueFalseTotal
if (m[/\.[^\.]+]) { } 0 0 0
260 else {
261 0 push @files, $_;
262 }
263 }
264 0 return @files;
265 }
266  
267 0 ▶sub make_file_list {
Subroutine make_file_list: not called CC=9 SCAR=45.0
Pod undocumented
268 0 my ($exclude, @args) = @_;
269  
270 0 my @list;
271  
272 0 foreach my $arg (@args) {
273 0 ▶ unless (-e $arg) { next; } # file exists
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $arg) 0 0 0
274 0 ▶ unless (-s $arg or -d $arg) { next; } # file size > 0 or directory (for Win platform)
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $arg or -d $arg) 0 0 0
Condition0%0/3
-s $arg or -d $arg
l!l&&r!l&&!r
000
MC/DC0%0/2
-s $arg or -d $arg
-s $arg -d $arg
Truth table0%0/3
A or B
A= -s $argB= -d $arg
ABresult
1X1
011
000
275 0 ▶ if ($exclude and $arg =~ m/$exclude/) { next; } # file matches exclude regex
Branch: 1 branch
BranchTrueFalseTotal
if ($exclude and $arg =~ /$exclude/) 0 0 0
Condition0%0/3
$exclude and $arg =~ /$exclude/
!ll&&!rl&&r
000
MC/DC0%0/2
$exclude and $arg =~ /$exclude/
$exclude $arg =~ /$exclude/
Truth table0%0/3
A and B
A= $excludeB= $arg =~ /$exclude/
ABresult
0X0
100
111
276  
277 0 ▶ if (-d $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $arg) { } 0 0 0
278 0 foreach my $full ( recurse_dir($arg) ) {
279 0 $full =~ /$arg(\/|)(.*)/;
280 0 my $short = $2;
281 0 push(@list, {full=>$full, short=>$short} );
282 }
283 }
284 else {
285 0 my $full = $arg;
286 0 my $short = $full;
287 0 $short =~ s/.*[\/\\]//;
288 0 push(@list, {full=>$arg, short=>$short} );
289 }
290 }
291  
292 0 return [ sort { $a->{short} cmp $b->{short} } @list ];
293 }
294  
295 0 ▶sub print_usage_and_exit {
Subroutine print_usage_and_exit: not called CC=2 SCAR=17.9
Pod undocumented
296 0 ▶ print STDOUT join "\n", "", @_, "Aborting!", "\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
297 0 print STDOUT usage();
298 0 exit;
299 }
300  
301 0 ▶sub runtime_source_code {
Subroutine runtime_source_code: not called CC=1 SCAR=6.9
Pod undocumented
302 0 die "generating a separate runtime not supported yet";
303 }
304  
305 #-------------------------------------------------------------------------------
306  
307 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
308 0 my $class = shift;
309 0 return bless { @_ }, $class;
310 }
311  
312 0 ▶sub compile_module {
Subroutine compile_module: not called CC=3 SCAR=24.8
Pod undocumented
313 0 my ($self, $module_path, $template_file_paths) = @_;
314 0 ▶ my $result = $self->compile_template_files(@$template_file_paths)
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->compile_template_files(@$template_file_paths) or return 0 0 0
315 or return;
316 0 ▶ open MODULE, "> $module_path"
Branch: 1 branch
BranchTrueFalseTotal
open MODULE, "> $module_path" or die "Can't open '${module_path}' for output:\n$!" 0 0 0
317 or die "Can't open '$module_path' for output:\n$!";
318 0 print MODULE $result;
319 0 close MODULE;
320 0 return 1;
321 }
322  
323 0 ▶sub compile_module_cached {
Subroutine compile_module_cached: not called CC=2 SCAR=17.9
Pod undocumented
324 0 my ($self, $module_path, $template_file_paths) = @_;
325 0 my $m = -M $module_path;
326 0 ▶ return 0 unless grep { -M($_) < $m } @$template_file_paths;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {-M $_ < $m;} @$template_file_paths 0 0 0
327 0 return $self->compile_module($module_path, $template_file_paths);
328 }
329  
330 0 ▶sub compile_template_files {
Subroutine compile_template_files: not called CC=3 SCAR=24.8
Pod undocumented
331 0 my $self = shift;
332 0 my $output = $self->_preamble;
333 0 for my $filepath (@_) {
334 0 my $filename = $filepath;
335 0 $filename =~ s/.*[\/\\]//;
336 0 ▶ open FILE, $filepath
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $filepath or die "Can't open '${filepath}' for input:\n$!" 0 0 0
337 or die "Can't open '$filepath' for input:\n$!";
338 0 my $template_input = do {local $/; <FILE>};
339 0 close FILE;
340 0 $output .=
341 $self->compile_template_content($template_input, $filename);
342 }
343 0 return $output;
344 }
345  
346 0 ▶sub compile_template_content {
Subroutine compile_template_content: not called CC=6 SCAR=37.4
Pod undocumented
347 0 ▶ die "Invalid arguments in call to Lemplate->compile_template_content"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 0 0 0
348 unless @_ == 3;
349 0 my ($self, $template_content, $template_name) = @_;
350 0 $TemplateName = $template_name;
351 0 ▶ my $parser = Lemplate::Parser->new( ref($self) ? %$self : () );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
352 0 ▶ my $parse_tree = $parser->parse(
Branch: 1 branch
BranchTrueFalseTotal
my $parse_tree = $parser->parse($template_content, {"name", $template_name}) or die $parser->error 0 0 0
353 $template_content, {name => $template_name}
354 ) or die $parser->error;
355 my $output =
356 "-- $template_name\n" .
357 "template_map['$template_name'] = " .
358 $parse_tree->{BLOCK} .
359 0 "\n";
360 0 for my $function_name (sort keys %{$parse_tree->{DEFBLOCKS}}) {
361 0 my $name = "$template_name/$function_name";
362 0 ▶ next if $ProcessedTemplates{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $Lemplate::ProcessedTemplates{$name} 0 0 0
363 #warn "seen $name";
364 0 $ProcessedTemplates{$name} = 1;
365 $output .=
366 "template_map['$name'] = " .
367 0 $parse_tree->{DEFBLOCKS}{$function_name} .
368 "\n";
369 }
370 0 return $output;
371 }
372  
373 0 ▶sub _preamble {
Subroutine _preamble: not called CC=1 SCAR=6.9
374 0 return <<'...';
375 --[[
376 This Lua code was generated by Lemplate, the Lua
377 Template Toolkit. Any changes made to this file will be lost the next
378 time the templates are compiled.
379  
380 Copyright 2016 - Yichun Zhang (agentzh) - All rights reserved.
381  
382 Copyright 2006-2014 - Ingy döt Net - All rights reserved.
383 ]]
384  
385 local gsub = ngx.re.gsub
386 local concat = table.concat
387 local type = type
388 local math_floor = math.floor
389 local table_maxn = table.maxn
390  
391 local _M = {
392 version = '0.07'
393 }
394  
395 local template_map = {}
396  
397 local function tt2_true(v)
398 return v and v ~= 0 and v ~= "" and v ~= '0'
399 end
400  
401 local function tt2_not(v)
402 return not v or v == 0 or v == "" or v == '0'
403 end
404  
405 local context_meta = {}
406  
407 function context_meta.plugin(context, name, args)
408 if name == "iterator" then
409 local list = args[1]
410 local count = table_maxn(list)
411 return { list = list, count = 1, max = count - 1, index = 0, size = count, first = true, last = false, prev = "" }
412 else
413 return error("unknown iterator: " .. name)
414 end
415 end
416  
417 function context_meta.process(context, file)
418 local f = template_map[file]
419 if not f then
420 return error("file error - " .. file .. ": not found")
421 end
422 return f(context)
423 end
424  
425 function context_meta.include(context, file)
426 local f = template_map[file]
427 if not f then
428 return error("file error - " .. file .. ": not found")
429 end
430 return f(context)
431 end
432  
433 context_meta = { __index = context_meta }
434  
435 -- XXX debugging function:
436 -- local function xxx(data)
437 -- io.stderr:write("\n" .. require("cjson").encode(data) .. "\n")
438 -- end
439  
440 local function stash_get(stash, expr)
441 local result
442  
443 if type(expr) ~= "table" then
444 result = stash[expr]
445 if type(result) == "function" then
446 return result()
447 end
448 return result or ''
449 end
450  
451 result = stash
452 for i = 1, #expr, 2 do
453 local key = expr[i]
454 if type(key) == "number" and key == math_floor(key) and key >= 0 then
455 key = key + 1
456 end
457 local val = result[key]
458 local args = expr[i + 1]
459 if args == 0 then
460 args = {}
461 end
462  
463 if val == nil then
464 if not _M.vmethods[key] then
465 if type(expr[i + 1]) == "table" then
466 return error("virtual method " .. key .. " not supported")
467 end
468 return ''
469 end
470 val = _M.vmethods[key]
471 args = {result, unpack(args)}
472 end
473  
474 if type(val) == "function" then
475 val = val(unpack(args))
476 end
477  
478 result = val
479 end
480  
481 return result
482 end
483  
484 local function stash_set(stash, k, v, default)
485 if default then
486 local old = stash[k]
487 if old == nil then
488 stash[k] = v
489 end
490 else
491 stash[k] = v
492 end
493 end
494  
495 _M.vmethods = {
496 join = function (list, delim)
497 delim = delim or ' '
498 local out = {}
499 local size = #list
500 for i = 1, size, 1 do
501 out[i * 2 - 1] = list[i]
502 if i ~= size then
503 out[i * 2] = delim
504 end
505 end
506 return concat(out)
507 end,
508  
509 first = function (list)
510 return list[1]
511 end,
512  
513 keys = function (list)
514 local out = {}
515 i = 1
516 for key in pairs(list) do
517 out[i] = key
518 i = i + 1
519 end
520 return out
521 end,
522  
523 last = function (list)
524 return list[#list]
525 end,
526  
527 push = function(list, ...)
528 local n = select("#", ...)
529 local m = #list
530 for i = 1, n do
531 list[m + i] = select(i, ...)
532 end
533 return ''
534 end,
535  
536 size = function (list)
537 if type(list) == "table" then
538 return #list
539 else
540 return 1
541 end
542 end,
543  
544 sort = function (list)
545 local out = { unpack(list) }
546 table.sort(out)
547 return out
548 end,
549  
550 split = function (str, delim)
551 delim = delim or ' '
552 local out = {}
553 local start = 1
554 local sub = string.sub
555 local find = string.find
556 local sstart, send = find(str, delim, start)
557 local i = 1
558 while sstart do
559 out[i] = sub(str, start, sstart-1)
560 i = i + 1
561 start = send + 1
562 sstart, send = find(str, delim, start)
563 end
564 out[i] = sub(str, start)
565 return out
566 end,
567 }
568  
569 _M.filters = {
570 html = function (s, args)
571 s = gsub(s, "&", '&amp;', "jo")
572 s = gsub(s, "<", '&lt;', "jo");
573 s = gsub(s, ">", '&gt;', "jo");
574 s = gsub(s, '"', '&quot;', "jo"); -- " end quote for emacs
575 return s
576 end,
577  
578 lower = function (s, args)
579 return string.lower(s)
580 end,
581  
582 upper = function (s, args)
583 return string.upper(s)
584 end,
585 }
586  
587 function _M.process(file, params)
588 local stash = params
589 local context = {
590 stash = stash,
591 filter = function (bits, name, params)
592 local s = concat(bits)
593 local f = _M.filters[name]
594 if f then
595 return f(s, params)
596 end
597 return error("filter '" .. name .. "' not found")
598 end
599 }
600 context = setmetatable(context, context_meta)
601 local f = template_map[file]
602 if not f then
603 return error("file error - " .. file .. ": not found")
604 end
605 return f(context)
606 end
607 ...
608 }
609  
610 8 1;
611  
612 __END__
↑ Summary Lemplate/Directive.pm »