File Coverage

blib/lib/Dancer2/Template/TTLogReport.pm
Criterion Covered Total %
statement 21 58 36.2
branch 0 16 0.0
condition 0 13 0.0
subroutine 7 15 46.6
pod 1 5 20.0
total 29 107 27.1


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Dancer2-Plugin-LogReport version 2.02.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2015-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12             #oodist: This file contains OODoc-style documentation which will get stripped
13             #oodist: during its release in the distribution. You can use this file for
14             #oodist: testing, however the code of this development version may be broken!
15              
16             #XXX rework of Dancer2::Template::TemplateToolkit 1.1.2
17              
18             package Dancer2::Template::TTLogReport;{
19             our $VERSION = '2.02';
20             }
21              
22              
23 1     1   1627 use Log::Report 'dancer2-plugin-logreport';
  1         1  
  1         5  
24              
25 1     1   212 use Moo;
  1         2  
  1         5  
26 1     1   468 use Dancer2::Core::Types;
  1         2  
  1         10  
27 1     1   10071 use Dancer2::FileUtils qw/path/;
  1         1  
  1         77  
28 1     1   5 use Scalar::Util qw/weaken/;
  1         3  
  1         37  
29              
30 1     1   665 use Log::Report::Template ();
  1         29056  
  1         53  
31 1     1   10 use Log::Report::Util qw/parse_locale/;
  1         2  
  1         630  
32              
33             with 'Dancer2::Core::Role::Template';
34              
35              
36 0     0     sub _build_engine { $_[0]->tt; $_[0] }
  0            
37              
38             #--------------------
39              
40             has tt => ( is => 'rw', isa => InstanceOf ['Template'], builder => 1 );
41              
42             sub _build_tt()
43 0     0     { my $self = shift;
44 0           my %config = %{$self->config};
  0            
45 0           my $charset = $self->charset;
46 0   0       my $templater = delete $config{templater} || 'Log::Report::Template';
47              
48 0 0         $Template::Stash::PRIVATE = undef if delete $config{show_private_variables};
49              
50 0           weaken(my $ttt = $self);
51 0           my $include_path = delete $config{include_path};
52              
53             $templater->new(
54             ANYCASE => 1,
55             ABSOLUTE => 1,
56             START_TAG => delete $config{start_tag} || '\[\%',
57             END_TAG => delete $config{end_tag} || delete $config{stop_tag} || '\%\]',
58 0 0 0 0     INCLUDE_PATH => [ (defined $include_path ? $include_path : ()), sub { [ $ttt->views ] } ],
  0 0 0        
59             (length $charset) ? (ENCODING => $charset) : (),
60             %config,
61             );
62             }
63              
64             #--------------------
65              
66             sub addTextdomain(%)
67 0     0 0   { my $self = shift;
68 0           $self->tt->addTextdomain(@_);
69             }
70              
71              
72             sub render($$)
73 0     0 1   { my ($self, $template, $tokens) = @_;
74 0           my $content = '';
75 0           my $charset = $self->charset;
76 0 0         my @options = (length $charset) ? (binmode => ":encoding($charset)") : ();
77 0           my $tt = $self->tt;
78              
79 0 0         if(my $lang = $tokens->{translate_to}) {
80 0           $tt->translateTo($lang);
81             }
82              
83 0   0       local $tokens->{locale} = my $locale = $tt->translateTo || '';
84 0           my ($lang, $terr) = parse_locale $locale;
85 0   0       local $tokens->{language} = $lang // '';
86 0 0 0       local $tokens->{language_territory} = defined $lang && defined $terr ? $lang . '_' . $terr : '';
87              
88 0 0         $tt->process($template, $tokens, \$content, @options)
89             or $self->log_cb->(core => 'Failed to render template: ' . $tt->error);
90              
91 0           $content;
92             }
93              
94             #### The next is reworked from Dancer2::Template::TemplateToolkit. No idea
95             #### whether it is reasonable.
96              
97             # Override *_pathname methods from Dancer2::Core::Role::Template
98             # Let TT2 do the concatenation of paths to template names.
99             #
100             # TT2 will look in a its INCLUDE_PATH for templates.
101             # Typically $self->views is an absolute path, and we set ABSOLUTE => 1 above.
102             # In that case TT2 does NOT iterate through what is set for INCLUDE_PATH
103             # However, if its not absolute, we want to allow TT2 iterate through the
104             # its INCLUDE_PATH, which we set to be $self->views.
105              
106             sub view_pathname($)
107 0     0 0   { my ($self, $view) = @_;
108 0           $self->_template_name($view);
109             }
110              
111             sub layout_pathname($)
112 0     0 0   { my ($self, $layout) = @_;
113 0           path $self->layout_dir, $self->_template_name($layout);
114             }
115              
116             sub pathname_exists($)
117 0     0 0   { my ($self, $pathname) = @_;
118              
119             # dies if pathname can not be found via TT2's INCLUDE_PATH search
120 0           my $exists = eval { $self->engine->service->context->template($pathname); 1 };
  0            
  0            
121 0 0         $exists or $self->log_cb->(debug => $@);
122 0           $exists;
123             }
124              
125             1;
126              
127             #-----------