| 1 | package Yeb::Plugin::Xslate; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $Yeb::Plugin::Xslate::AUTHORITY = 'cpan:GETTY'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: Yeb Plugin for Text::Xslate | ||||||||||
| 6 | 1 | $Yeb::Plugin::Xslate::VERSION = '0.100'; | |||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Text::Xslate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | has app => ( is => 'ro', required => 1 ); | |||||||||
| 12 | 1 | has class => ( is => 'ro', required => 1 ); | |||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | has path => ( is => 'ro', lazy => 1, builder => sub {[]} ); | ||||||||
|
Subroutine
_build_path: not called CC=1 SCAR=1.2
| |||||||||||
| 15 | 0 | ▶ | has function => ( is => 'ro', lazy => 1, builder => sub {{}} ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 16 | 0 | ▶ | has content_type => ( is => 'ro', lazy => 1, builder => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||
| 17 | 0 | my ( $self ) = @_; | |||||||||
| 18 | 0 | ▶ | my $type = $self->has_type ? $self->type : 'html'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 0 | ▶ | return 'text/plain' if $type eq 'text'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | ▶ | return 'text/xml' if $type eq 'xml'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | ▶ | return 'text/html' if $type eq 'html'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 0 | croak __PACKAGE__." Unknown type ".$type; | |||||||||
| 23 | 1 | } ); | |||||||||
| 24 | 0 | ▶ | has suffix => ( is => 'ro', lazy => 1, builder => sub {'.tx'} ); | ||||||||
|
Subroutine
_build_suffix: not called CC=1 SCAR=1.2
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | my @xslate_attributes = qw( | |||||||||
| 27 | cache | ||||||||||
| 28 | cache_dir | ||||||||||
| 29 | module | ||||||||||
| 30 | html_builder_module | ||||||||||
| 31 | input_layer | ||||||||||
| 32 | verbose | ||||||||||
| 33 | syntax | ||||||||||
| 34 | type | ||||||||||
| 35 | line_start | ||||||||||
| 36 | tag_start | ||||||||||
| 37 | tag_end | ||||||||||
| 38 | header | ||||||||||
| 39 | footer | ||||||||||
| 40 | pre_process_handler | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 1 | for (@xslate_attributes) { | |||||||||
| 44 | 14 | has $_ => ( is => 'ro', predicate => 1 ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | has xslate => ( | ||||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | lazy => 1, | ||||||||||
| 50 | predicate => 1, | ||||||||||
| 51 | 0 | ▶ | builder => sub { | ||||||||
|
Subroutine
_build_xslate: not called CC=3 SCAR=24.8
| |||||||||||
| 52 | 0 | my ( $self ) = @_; | |||||||||
| 53 | 0 | ▶ | croak __PACKAGE__." Xslate needs at least one path" unless @{$self->path}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | Text::Xslate->new( | ||||||||||
| 55 | path => $self->path, | ||||||||||
| 56 | function => $self->all_functions, | ||||||||||
| 57 | suffix => $self->suffix, | ||||||||||
| 58 | map { | ||||||||||
| 59 | 0 | my $predicate = 'has_'.$_; | |||||||||
| 60 | 0 | ▶ | $self->$predicate ? ( $_ => $self->$_ ) : () | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } @xslate_attributes, | ||||||||||
| 62 | ); | ||||||||||
| 63 | }, | ||||||||||
| 64 | 1 | ); | |||||||||
| 65 | |||||||||||
| 66 | has all_functions => ( | ||||||||||
| 67 | is => 'ro', | ||||||||||
| 68 | lazy => 1, | ||||||||||
| 69 | 0 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 70 | 0 | my ( $self ) = @_; | |||||||||
| 71 | return { | ||||||||||
| 72 | 0 | %{$self->app->yeb_functions}, | |||||||||
| 73 | 0 | %{$self->class->yeb_class_functions}, | |||||||||
| 74 | 0 | %{$self->app->functions}, | |||||||||
| 75 | 0 | %{$self->function} | |||||||||
| 76 | } | ||||||||||
| 77 | }, | ||||||||||
| 78 | 1 | ); | |||||||||
| 79 | |||||||||||
| 80 | has base_functions => ( | ||||||||||
| 81 | is => 'ro', | ||||||||||
| 82 | lazy => 1, | ||||||||||
| 83 | 0 | ▶ | builder => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 84 | 0 | my ( $self ) = @_; | |||||||||
| 85 | return { | ||||||||||
| 86 | 0 | ▶ | current_file => sub { $self->xslate->current_file }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 87 | 0 | ▶ | current_line => sub { $self->xslate->current_line }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 88 | 0 | ▶ | call => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my ( $thing, $func, @args ) = @_; | |||||||||
| 90 | 0 | $thing->$func(@args); | |||||||||
| 91 | }, | ||||||||||
| 92 | 0 | ▶ | call_if => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 93 | 0 | my ( $thing, $func, @args ) = @_; | |||||||||
| 94 | 0 | ▶ | $thing->$func(@args) if $thing; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | }, | ||||||||||
| 96 | 0 | ▶ | replace => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 97 | 0 | my ( $source, $from, $to ) = @_; | |||||||||
| 98 | 0 | $source =~ s/$from/$to/g; | |||||||||
| 99 | 0 | return $source; | |||||||||
| 100 | }, | ||||||||||
| 101 | } | ||||||||||
| 102 | 0 | }, | |||||||||
| 103 | 1 | ); | |||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | sub get_vars { | ||||||||
|
Subroutine
get_vars: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 106 | 0 | my ( $self, $user_vars ) = @_; | |||||||||
| 107 | 0 | my %stash = %{$self->app->cc->stash}; | |||||||||
| 108 | 0 | ▶ | my %user = defined $user_vars ? %{$user_vars} : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | return $self->app->merge_hashs( | |||||||||
| 110 | $self->app->cc->export, | ||||||||||
| 111 | $self->app->cc->stash, | ||||||||||
| 112 | \%user | ||||||||||
| 113 | ); | ||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 117 | 0 | my ( $self ) = @_; | |||||||||
| 118 | 0 | ▶ | $self->app->register_function('xslate_path',sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 119 | 0 | ▶ | croak __PACKAGE__." Xslate already instantiated, no path can be added" if $self->has_xslate; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | unshift @{$self->path}, "$_" for (@_); | |||||||||
| 121 | 0 | }); | |||||||||
| 122 | 0 | ▶ | $self->app->register_function('xslate_function',sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 123 | 0 | ▶ | croak __PACKAGE__." Xslate already instantiated, no function can be added" if $self->has_xslate; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | my ( $name, $function ) = @_; | |||||||||
| 125 | 0 | $self->function->{$name} = $function; | |||||||||
| 126 | 0 | }); | |||||||||
| 127 | 0 | ▶ | $self->app->register_function('xslate',sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 128 | 0 | my ( $file, $user_vars ) = @_; | |||||||||
| 129 | 0 | my $vars = $self->get_vars($user_vars); | |||||||||
| 130 | 0 | my $template_file = $file.$self->suffix; | |||||||||
| 131 | 0 | $self->app->cc->content_type($self->content_type); | |||||||||
| 132 | 0 | $self->app->cc->body($self->xslate->render($template_file,$vars)); | |||||||||
| 133 | 0 | $self->app->cc->response; | |||||||||
| 134 | 0 | }); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 1 | 1; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||