File Coverage

blib/lib/Text/Xslate/Syntax/FiltersAsTags.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package Text::Xslate::Syntax::FiltersAsTags;
2              
3             =head1 NAME
4              
5             Text::Xslate::Syntax::FiltersAsTags - easily add more template tags which are mapped onto filters
6              
7             =head1 VERSION
8              
9             version 0.1
10              
11             =cut
12              
13             our $VERSION = '0.1';
14              
15             =head1 SYNOPSIS
16              
17             package My::Own::Syntax;
18             use Mouse;
19              
20             extends 'Text::Xslate::Syntax::TTerse'; # or ::Kolon
21             with 'Text::Xslate::Syntax::FiltersAsTags'; # this module!
22              
23             my @more_symbols = qw(TL R_table R_vtable R_svg R_collect_tables); # additional tags
24             sub more_symbols { @more_symbols } # you must provide this method
25              
26             no Mouse;
27             __PACKAGE__->meta->make_immutable;
28             1
29              
30             in your controller:
31              
32             my $tx = Text::Xslate->new(
33             syntax => 'My::Own::Syntax',
34             function => +{
35             tl => html_builder(\&translate_switch),
36             r_table => html_builder { &fixup_filter_parser; make_R_table($_[0]) },
37             r_svg => html_builder { &fixup_filter_parser; make_R_svg($_[0]); },
38             r_collect_tables =>
39             html_builder { &fixup_filter_parser;
40             eval_R_lines('r=list()');
41             eval_R_lines($_[0]);
42             R_merge_tables()
43             },
44             },
45             );
46              
47             sub fixup_filter_parser {
48             for (@_) {
49             s/^\s*\s*$/$1/s;
50             s/(\n\s*)*$//; s/^(\s*\n)*//;
51             }
52             }
53              
54             in your template:
55              
56             [% TL %][_en] observation period [_de] Beobachtungszeitraum[% END %]
57              
58             [% R_table %]
59             summary(md_real$region)
60             [% END %]
61              
62             [% R_svg %]
63             boxplot(md_real$[% data %], log="y", main="[% data %]")
64             [% END %]
65              
66             [% R_collect_tables %]
67             r$`[% TL %] at beginning [% END %]` <- table(md_real[md_real$appeared.on$mday<4,]$appeared.on$hour)
68             r$`[% TL %] not at beginning [% END %]` <- table(md_real[md_real$appeared.on$mday>=4,]$appeared.on$hour)
69             ]]>[% END %]
70              
71              
72             =head1 DESCRIPTION
73              
74             this L helps you to quickly add more template tags
75             to your Xslate template, which are then mapped to filters in your
76             parse run.
77              
78             You only need to provide a single method in your own syntax class,
79             C. It must return a list of additional tags allowed in
80             the template. You connect to those tags, which are a simple
81             convenience for a filter call, from your controller.
82              
83             Note that you do need to make an own class for every domain specific
84             template, and it does need to use L because that's what
85             L uses. You will get weird errors when trying to use
86             Moo.
87              
88             =cut
89              
90 2     2   5082499 use Mouse::Role;
  2         88183  
  2         11  
91             requires 'init_symbols';
92             requires 'more_symbols';
93              
94             after init_symbols => sub {
95             my($parser) = @_;
96             my @intros = grep { $parser->symbol_table->{$_}->is_block_end &&
97             $parser->symbol_table->{$_}->id !~ /^\(/ &&
98             length $parser->symbol_table->{$_}->counterpart }
99             keys %{ $parser->symbol_table };
100             my ($intro, $outro);
101             $intro = $parser->symbol_table->{ $intros[0] }->counterpart if @intros;
102             $outro = $parser->symbol_table->{ $intros[0] }->id if @intros;
103              
104             my $std_sb = sub {
105             my($parser, $symbol) = @_;
106              
107             my $filter = lc $symbol->id;
108              
109             my $proc = $parser->lambda($symbol);
110              
111             $proc->second([]);
112             $parser->advance($intro) if length $intro;
113             $proc->third( $parser->statements() );
114             $parser->advance(length $outro ? $outro : 'END');
115              
116             my $callmacro = $parser->call($proc->first);
117              
118             my $callfilter = $parser->call($filter, $callmacro);
119             return( $proc, $parser->print($callfilter) );
120             };
121              
122             $parser->symbol($_)->set_std($std_sb)
123             for $parser->more_symbols;
124            
125             return;
126             };
127              
128             =head1 AUTHOR
129              
130             Ailin Nemui Eailin at devio dot usE
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2013 by Ailin Nemui.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut
140              
141 2     2   1270 no Mouse::Role;
  2         5  
  2         12  
142             1