File Coverage

blib/lib/Rubric/Renderer.pm
Criterion Covered Total %
statement 43 43 100.0
branch 7 8 87.5
condition 1 2 50.0
subroutine 12 12 100.0
pod 2 2 100.0
total 65 67 97.0


line stmt bran cond sub pod time code
1 9     9   5378 use strict;
  9         16  
  9         235  
2 9     9   49 use warnings;
  9         20  
  9         451  
3             package Rubric::Renderer;
4             # ABSTRACT: the rendering interface for Rubric
5             $Rubric::Renderer::VERSION = '0.156';
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod Rubric::Renderer provides a simple interface for rendering entries, entry sets,
9             #pod and other things collected by Rubric::WebApp.
10             #pod
11             #pod =cut
12              
13 9     9   49 use Carp;
  9         16  
  9         562  
14 9     9   5211 use File::ShareDir;
  9         41605  
  9         435  
15 9     9   61 use File::Spec;
  9         17  
  9         347  
16 9     9   7073 use HTML::Widget::Factory 0.03;
  9         50535  
  9         254  
17 9     9   3969 use Rubric;
  9         68  
  9         238  
18 9     9   51 use Rubric::Config;
  9         270  
  9         73  
19 9     9   6939 use Template 2.00;
  9         190273  
  9         285  
20 9     9   7202 use Template::Filters;
  9         48948  
  9         3973  
21              
22             #pod =head1 METHODS
23             #pod
24             #pod =head2 register_type($type => \%arg)
25             #pod
26             #pod This method registers a format type by providing a little data needed to render
27             #pod to it. The hashref of arguments must include C, used to set the
28             #pod MIME type of the returned ouput; and C, used to find the primary
29             #pod template.
30             #pod
31             #pod This method returns a Template object, which is registered as the renderer for
32             #pod this type. This return value may change in the future.
33             #pod
34             #pod =cut
35              
36             my %renderer;
37              
38             sub register_type {
39 36     36 1 182971 my ($class, $type, $arg) = @_;
40 36         82 $renderer{$type} = $arg;
41 36         247 $renderer{$type}{renderer} = Template->new({
42             PROCESS => ("template.$arg->{extension}"),
43             INCLUDE_PATH => [
44             Rubric::Config->template_path,
45             File::Spec->catdir(File::ShareDir::dist_dir('Rubric'), 'templates'),
46             ],
47             });
48             }
49              
50             __PACKAGE__->register_type(@$_) for (
51             [ html => { content_type => 'text/html; charset="utf-8"', extension => 'html' } ],
52             [ rss => { content_type => 'application/rss+xml', extension => 'rss' } ],
53             [ txt => { content_type => 'text/plain', extension => 'txt' } ],
54             [ api => { content_type => 'text/xml', extension => 'api' } ],
55             );
56              
57             #pod =head2 process($template, $type, \%stash)
58             #pod
59             #pod This method renders the named template using the registered renderer for the
60             #pod given type, using the passed stash variables.
61             #pod
62             #pod The type must be rendered with Rubric::Renderer before this method is called.
63             #pod
64             #pod In list context, this method returns the content type and output document as a
65             #pod two-element list. In scalar context, it returns the output document.
66             #pod
67             #pod =cut
68              
69             my $xml_escape = sub {
70             for (shift) {
71             s/&/&/g;
72             s/
73             s/>/>/g;
74             s/"/"/g;
75             s/'/'/g;
76             return $_;
77             }
78             };
79              
80             sub process {
81 46     46 1 1079 my ($class, $template, $type, $stash) = @_;
82 46 100       200 return unless $renderer{$type};
83              
84 45         147 $stash->{xml_escape} = $xml_escape;
85 45   50     959 $stash->{version} = Rubric->VERSION || 0;
86 45         622 $stash->{widget} = HTML::Widget::Factory->new;
87 45         147524 $stash->{basename} = Rubric::Config->basename;
88             # 2007-05-07
89             # XXX: we only should create one factory per request, tops -- rjbs,
90              
91 45         173 $template .= '.' . $renderer{$type}{extension};
92             $renderer{$type}{renderer}->process($template, $stash, \(my $output))
93 45 100       430 or die "Couldn't render template: " . $renderer{$type}{renderer}->error;
94              
95 44 50       5115 die "template produced no content" unless $output;
96              
97             return wantarray
98 44 100       420 ? ($renderer{$type}{content_type}, $output)
99             : $output;
100             }
101              
102             1;
103              
104             __END__