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   6700 use strict;
  9         16  
  9         317  
2 9     9   50 use warnings;
  9         18  
  9         462  
3             package Rubric::Renderer;
4             # ABSTRACT: the rendering interface for Rubric
5             $Rubric::Renderer::VERSION = '0.155';
6             # =head1 DESCRIPTION
7             #
8             # Rubric::Renderer provides a simple interface for rendering entries, entry sets,
9             # and other things collected by Rubric::WebApp.
10             #
11             # =cut
12              
13 9     9   51 use Carp;
  9         19  
  9         644  
14 9     9   6168 use File::ShareDir;
  9         42013  
  9         595  
15 9     9   73 use File::Spec;
  9         18  
  9         235  
16 9     9   8144 use HTML::Widget::Factory 0.03;
  9         76636  
  9         263  
17 9     9   4479 use Rubric;
  9         25  
  9         219  
18 9     9   50 use Rubric::Config;
  9         286  
  9         80  
19 9     9   8307 use Template 2.00;
  9         204536  
  9         303  
20 9     9   8647 use Template::Filters;
  9         44101  
  9         4032  
21              
22             # =head1 METHODS
23             #
24             # =head2 register_type($type => \%arg)
25             #
26             # This method registers a format type by providing a little data needed to render
27             # to it. The hashref of arguments must include C, used to set the
28             # MIME type of the returned ouput; and C, used to find the primary
29             # template.
30             #
31             # This method returns a Template object, which is registered as the renderer for
32             # this type. This return value may change in the future.
33             #
34             # =cut
35              
36             my %renderer;
37              
38             sub register_type {
39 36     36 1 191922 my ($class, $type, $arg) = @_;
40 36         86 $renderer{$type} = $arg;
41 36         266 $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             # =head2 process($template, $type, \%stash)
58             #
59             # This method renders the named template using the registered renderer for the
60             # given type, using the passed stash variables.
61             #
62             # The type must be rendered with Rubric::Renderer before this method is called.
63             #
64             # In list context, this method returns the content type and output document as a
65             # two-element list. In scalar context, it returns the output document.
66             #
67             # =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 1827 my ($class, $template, $type, $stash) = @_;
82 46 100       442 return unless $renderer{$type};
83              
84 45         187 $stash->{xml_escape} = $xml_escape;
85 45   50     949 $stash->{version} = Rubric->VERSION || 0;
86 45         688 $stash->{widget} = HTML::Widget::Factory->new;
87 45         154933 $stash->{basename} = Rubric::Config->basename;
88             # 2007-05-07
89             # XXX: we only should create one factory per request, tops -- rjbs,
90              
91 45         222 $template .= '.' . $renderer{$type}{extension};
92 45 100       435 $renderer{$type}{renderer}->process($template, $stash, \(my $output))
93             or die "Couldn't render template: " . $renderer{$type}{renderer}->error;
94              
95 44 50       6138 die "template produced no content" unless $output;
96              
97             return wantarray
98 44 100       648 ? ($renderer{$type}{content_type}, $output)
99             : $output;
100             }
101              
102             1;
103              
104             __END__