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   3750 use strict;
  9         16  
  9         273  
2 9     9   40 use warnings;
  9         14  
  9         305  
3             # ABSTRACT: the rendering interface for Rubric
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod Rubric::Renderer provides a simple interface for rendering entries, entry sets,
8             #pod and other things collected by Rubric::WebApp.
9             #pod
10             #pod =cut
11              
12             use Carp;
13 9     9   78 use File::ShareDir;
  9         111  
  9         454  
14 9     9   2570 use File::Spec;
  9         134470  
  9         475  
15 9     9   60 use HTML::Widget::Factory 0.03;
  9         15  
  9         227  
16 9     9   3024 use Rubric;
  9         32114  
  9         234  
17 9     9   2635 use Rubric::Config;
  9         19  
  9         178  
18 9     9   42 use Template 2.00;
  9         24  
  9         55  
19 9     9   3540 use Template::Filters;
  9         131882  
  9         221  
20 9     9   3575  
  9         33635  
  9         2875  
21             #pod =head1 METHODS
22             #pod
23             #pod =head2 register_type($type => \%arg)
24             #pod
25             #pod This method registers a format type by providing a little data needed to render
26             #pod to it. The hashref of arguments must include C<content_type>, used to set the
27             #pod MIME type of the returned ouput; and C<extension>, used to find the primary
28             #pod template.
29             #pod
30             #pod This method returns a Template object, which is registered as the renderer for
31             #pod this type. This return value may change in the future.
32             #pod
33             #pod =cut
34              
35             my %renderer;
36              
37             my ($class, $type, $arg) = @_;
38             $renderer{$type} = $arg;
39 36     36 1 126680 $renderer{$type}{renderer} = Template->new({
40 36         76 PROCESS => ("template.$arg->{extension}"),
41 36         180 INCLUDE_PATH => [
42             Rubric::Config->template_path,
43             File::Spec->catdir(File::ShareDir::dist_dir('Rubric'), 'templates'),
44             ],
45             });
46             }
47              
48             __PACKAGE__->register_type(@$_) for (
49             [ html => { content_type => 'text/html; charset="utf-8"', extension => 'html' } ],
50             [ rss => { content_type => 'application/rss+xml', extension => 'rss' } ],
51             [ txt => { content_type => 'text/plain', extension => 'txt' } ],
52             [ api => { content_type => 'text/xml', extension => 'api' } ],
53             );
54              
55             #pod =head2 process($template, $type, \%stash)
56             #pod
57             #pod This method renders the named template using the registered renderer for the
58             #pod given type, using the passed stash variables.
59             #pod
60             #pod The type must be rendered with Rubric::Renderer before this method is called.
61             #pod
62             #pod In list context, this method returns the content type and output document as a
63             #pod two-element list. In scalar context, it returns the output document.
64             #pod
65             #pod =cut
66              
67             my $xml_escape = sub {
68             for (shift) {
69             s/&/&amp;/g;
70             s/</&lt;/g;
71             s/>/&gt;/g;
72             s/"/&quot;/g;
73             s/'/&apos;/g;
74             return $_;
75             }
76             };
77              
78             my ($class, $template, $type, $stash) = @_;
79             return unless $renderer{$type};
80              
81 46     46 1 1957 $stash->{xml_escape} = $xml_escape;
82 46 100       216 $stash->{version} = Rubric->VERSION || 0;
83             $stash->{widget} = HTML::Widget::Factory->new;
84 45         128 $stash->{basename} = Rubric::Config->basename;
85 45   50     482 # 2007-05-07
86 45         475 # XXX: we only should create one factory per request, tops -- rjbs,
87 45         96646  
88             $template .= '.' . $renderer{$type}{extension};
89             $renderer{$type}{renderer}->process($template, $stash, \(my $output))
90             or die "Couldn't render template: " . $renderer{$type}{renderer}->error;
91 45         213  
92             die "template produced no content" unless $output;
93 45 100       337  
94             return wantarray
95 44 50       5159 ? ($renderer{$type}{content_type}, $output)
96             : $output;
97             }
98 44 100       367  
99             1;
100              
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Rubric::Renderer - the rendering interface for Rubric
109              
110             =head1 VERSION
111              
112             version 0.157
113              
114             =head1 DESCRIPTION
115              
116             Rubric::Renderer provides a simple interface for rendering entries, entry sets,
117             and other things collected by Rubric::WebApp.
118              
119             =head1 PERL VERSION
120              
121             This code is effectively abandonware. Although releases will sometimes be made
122             to update contact info or to fix packaging flaws, bug reports will mostly be
123             ignored. Feature requests are even more likely to be ignored. (If someone
124             takes up maintenance of this code, they will presumably remove this notice.)
125             This means that whatever version of perl is currently required is unlikely to
126             change -- but also that it might change at any new maintainer's whim.
127              
128             =head1 METHODS
129              
130             =head2 register_type($type => \%arg)
131              
132             This method registers a format type by providing a little data needed to render
133             to it. The hashref of arguments must include C<content_type>, used to set the
134             MIME type of the returned ouput; and C<extension>, used to find the primary
135             template.
136              
137             This method returns a Template object, which is registered as the renderer for
138             this type. This return value may change in the future.
139              
140             =head2 process($template, $type, \%stash)
141              
142             This method renders the named template using the registered renderer for the
143             given type, using the passed stash variables.
144              
145             The type must be rendered with Rubric::Renderer before this method is called.
146              
147             In list context, this method returns the content type and output document as a
148             two-element list. In scalar context, it returns the output document.
149              
150             =head1 AUTHOR
151              
152             Ricardo SIGNES <rjbs@semiotic.systems>
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is copyright (c) 2004 by Ricardo SIGNES.
157              
158             This is free software; you can redistribute it and/or modify it under
159             the same terms as the Perl 5 programming language system itself.
160              
161             =cut