File Coverage

lib/Web/DataService/Render.pm
Criterion Covered Total %
statement 9 35 25.7
branch 0 18 0.0
condition 0 6 0.0
subroutine 3 8 37.5
pod 0 5 0.0
total 12 72 16.6


line stmt bran cond sub pod time code
1             #
2             # Web::DataService::Render
3             #
4             # This module provides a role that is used by 'Web::DataService'. It
5             # implements routines for generating documentation pages and output pages by
6             # rendering templates.
7             #
8             # Author: Michael McClennen
9              
10 2     2   15 use strict;
  2         5  
  2         83  
11              
12             package Web::DataService::Render;
13              
14 2     2   12 use Carp 'croak';
  2         15  
  2         94  
15              
16 2     2   11 use Moo::Role;
  2         5  
  2         25  
17              
18              
19             # check_doc ( template )
20             #
21             # Return the given template filename if the corresponding file path exists and
22             # is readable under the documentation template directory. Throw an exception
23             # if the file exists but is not readable. Return undefined (false) if the
24             # file is not there.
25              
26             sub check_doc {
27              
28 0     0 0   my ($ds, $template_path) = @_;
29            
30 0 0 0       return unless defined $template_path && $template_path ne '';
31            
32 0           my $full_path = $Web::DataService::FOUNDATION->file_path($ds->{doc_template_dir}, $template_path);
33            
34 0 0         if ( -e $full_path )
35             {
36 0 0         return $template_path if -r $full_path;
37 0           croak "template $template_path: $!";
38             }
39            
40 0           return;
41             }
42              
43              
44             # read_doc_partial ( path )
45             #
46             # Read the first 2048 characters of the specified template file and return
47             # them.
48              
49             sub read_doc_partial {
50            
51 0     0 0   my ($ds, $template_path) = @_;
52            
53 0           my $full_path = $Web::DataService::FOUNDATION->file_path($ds->{doc_template_dir}, $template_path);
54            
55 0 0         open(my $fh, "<", $full_path) || die "cannot read documentation template '$full_path': $!";
56 0           sysread($fh, my $contents, 2048);
57            
58 0           return $contents;
59             }
60              
61              
62             # render_doc ( template, definitions, header, footer, vars )
63             #
64             # Render the specified template, with the specified parameters. If
65             # $definitions and/or $header are defined, process them before the main
66             # template. If $footer is defined, process it after the main template. If
67             # $vars is defined, it must be a hashref of variable definitions that will be
68             # passed to the template rendering engine.
69              
70             sub render_doc {
71            
72 0     0 0   my ($ds, $main, $defs, $header, $footer, $vars) = @_;
73            
74             # Throw an exception if no templating module was selected.
75            
76             croak "you must select a templating module"
77 0 0         unless defined $ds->{templating_plugin};
78            
79 0           my $templates = { defs => $defs, header => $header,
80             main => $main, footer => $footer };
81            
82 0           $ds->{templating_plugin}->render_template($ds, $ds->{doc_engine}, $vars, $templates);
83             }
84              
85              
86             # check_output ( template )
87             #
88             # Return the given template filename if the corresponding file path exists and
89             # is readable under the output template directory. Throw an exception if the
90             # file exists but is not readable. Return undefined (false) if the file is
91             # not there.
92              
93             sub check_output {
94              
95 0     0 0   my ($ds, $template) = @_;
96            
97 0 0 0       return unless defined $template && $template ne '';
98            
99 0           my $template_file = $Web::DataService::FOUNDATION->file_path($ds->{output_templates}, $template);
100            
101 0 0         if ( -e $template_file )
102             {
103 0 0         return $template if -r $template_file;
104 0           croak "template $template_file: $!";
105             }
106            
107 0           return;
108             }
109              
110              
111             # render_output ( template, definitions, header, footer, vars )
112             #
113             # Render the specified template, with the specified parameters. If
114             # $definitions and/or $header are defined, process them before the main
115             # template. If $footer is defined, process it after the main template. If
116             # $vars is defined, it must be a hashref of variable definitions that will be
117             # passed to the template rendering engine.
118              
119             sub render_output {
120            
121 0     0 0   my $ds = shift;
122            
123             # Throw an exception if no templating module was selected.
124            
125             croak "you must select a templating module"
126 0 0         unless defined $ds->{templating_plugin};
127            
128 0           $ds->{templating_plugin}->render_template($ds->{doc_templates}, @_);
129             }
130              
131              
132             1;