| 1 | 9 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 9 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package Rubric::Renderer 0.157; | ||||||||||||
| 4 | # ABSTRACT: the rendering interface for Rubric | ||||||||||||
| 5 | |||||||||||||
| 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 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 9 | ▶ | use File::ShareDir; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 9 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 9 | ▶ | use HTML::Widget::Factory 0.03; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 9 | ▶ | use Rubric; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 9 | ▶ | use Rubric::Config; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 9 | ▶ | use Template 2.00; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 9 | ▶ | use Template::Filters; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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<content_type>, used to set the | ||||||||||||
| 28 | #pod MIME type of the returned ouput; and C<extension>, 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 | 9 | my %renderer; | |||||||||||
| 37 | |||||||||||||
| 38 | 36 | ▶ | sub register_type { | ||||||||||
|
Subroutine
register_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 39 | 36 | my ($class, $type, $arg) = @_; | |||||||||||
| 40 | 36 | $renderer{$type} = $arg; | |||||||||||
| 41 | 36 | $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 | 9 | __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 | 67 | ▶ | my $xml_escape = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 70 | 67 | for (shift) { | |||||||||||
| 71 | 67 | s/&/&/g; | |||||||||||
| 72 | 67 | s/</</g; | |||||||||||
| 73 | 67 | s/>/>/g; | |||||||||||
| 74 | 67 | s/"/"/g; | |||||||||||
| 75 | 67 | s/'/'/g; | |||||||||||
| 76 | 67 | return $_; | |||||||||||
| 77 | } | ||||||||||||
| 78 | 9 | }; | |||||||||||
| 79 | |||||||||||||
| 80 | 46 | ▶ | sub process { | ||||||||||
|
Subroutine
process: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||
| 81 | 46 | my ($class, $template, $type, $stash) = @_; | |||||||||||
| 82 | 46 | ▶ | return unless $renderer{$type}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | |||||||||||||
| 84 | 45 | $stash->{xml_escape} = $xml_escape; | |||||||||||
| 85 | 45 | ▶ | $stash->{version} = Rubric->VERSION || 0; | ||||||||||
|
Condition50%1/2
'Rubric'->VERSION || 0
MC/DC0%0/1
'Rubric'->VERSION || 0
'Rubric'->VERSION
Truth table50%1/2
A
A= 'Rubric'->VERSION
| |||||||||||||
| 86 | 45 | $stash->{widget} = HTML::Widget::Factory->new; | |||||||||||
| 87 | 45 | $stash->{basename} = Rubric::Config->basename; | |||||||||||
| 88 | # 2007-05-07 | ||||||||||||
| 89 | # XXX: we only should create one factory per request, tops -- rjbs, | ||||||||||||
| 90 | |||||||||||||
| 91 | 45 | $template .= '.' . $renderer{$type}{extension}; | |||||||||||
| 92 | $renderer{$type}{renderer}->process($template, $stash, \(my $output)) | ||||||||||||
| 93 | 45 | ▶ | or die "Couldn't render template: " . $renderer{$type}{renderer}->error; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | |||||||||||||
| 95 | 44 | ▶ | die "template produced no content" unless $output; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | |||||||||||||
| 97 | return wantarray | ||||||||||||
| 98 | 44 | ▶ | ? ($renderer{$type}{content_type}, $output) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | : $output; | ||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 9 | 1; | |||||||||||
| 103 | |||||||||||||
| 104 | __END__ | ||||||||||||