File Coverage

blib/lib/Bryar/Renderer/TT.pm
Criterion Covered Total %
statement 38 43 88.3
branch 3 6 50.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 53 61 86.8


line stmt bran cond sub pod time code
1             package Bryar::Renderer::TT;
2 3     3   957 use 5.006;
  3         11  
  3         121  
3 3     3   18 use strict;
  3         3  
  3         100  
4 3     3   17 use warnings;
  3         5  
  3         99  
5 3     3   16 use Carp;
  3         4  
  3         262  
6 3     3   4833 use Template;
  3         77960  
  3         37  
7 3     3   2767 use Template::Provider::Encoding;
  3         55739  
  3         55  
8             our $VERSION = '1.2';
9              
10             =head1 NAME
11              
12             Bryar::Renderer::TT - Render a blog page with Template Toolkit
13              
14             =head1 SYNOPSIS
15              
16             my ($content_type, $output) = $self->generate_html(...);
17              
18             =head1 DESCRIPTION
19              
20             This takes a Bryar blog, and the documents selected by the
21             C, and turns them into a page.
22              
23             You'll probably want to read a Template Toolkit tutorial before mucking
24             with the templates. Take a look at http://www.template-toolkit.org/
25              
26             This module passes in an array called C, full of
27             L objects, and a C object which
28             is most useful for calling the C method on to extract things
29             from the L.
30              
31             =head1 METHODS
32              
33             =head2 generate
34              
35             $self->generate($format, $bryar, @documents)
36              
37             Returns a page from templates and documents provided by the Bryar object.
38              
39             =cut
40              
41             sub _tt {
42 1     1   2 my ($class, $bryar) = @_;
43 1         4 my @path = $bryar->{config}->datadir;
44 1 50       6 unshift @path, $bryar->{config}->datadir."/".$bryar->{config}->{arguments}->{subblog}
45             if exists $bryar->{config}->{arguments}->{subblog};
46            
47 1         2 @path = map { $_, $_."/templates" } @path;
  1         7  
48 1         41 return Template->new({
49             INCLUDE_PATH => \@path,
50             LOAD_TEMPLATES => [
51             Template::Provider::Encoding->new({
52             INCLUDE_PATH => \@path,
53             }),
54             ],
55             ABSOLUTE => 1,
56             EVAL_PERL => 1,
57             RELATIVE => 1,
58             PRE_CHOMP => 1,
59             POST_CHOMP => 1,
60             });
61             }
62              
63             sub _tt_process {
64 1     1   3 my ($class, $filename, $bryar, @documents) = @_;
65 1         2 my $output;
66 1         8 my $tt = $class->_tt($bryar);
67 1         258051 $tt->process($filename, {
68             documents => \@documents,
69             bryar => $bryar,
70             }, \$output);
71 1 50       133 if (!$output) {
72 0         0 my $error = join("\n", $tt->error);
73 0         0 $error =~ s/&/&/g;
74 0         0 $error =~ s/
75 0         0 $error =~ s/>/>/g;
76 0         0 $bryar->{config}->frontend->report_error_html("Template Error",
77             <
78            

79             An error occurred while processing the templates for the blog. The
80             error as reported by Template Toolkit was:
81            

82            
 
83             $error
84            
85             EOF
86             );
87             }
88 1         25 return $output;
89             }
90              
91             our %formats;
92              
93             =head2 register_format
94              
95             $class->register_format($format, $filename, $content_type);
96              
97             Registers a new format that Bryar is capable of producing. This can be used
98             both for spitting out RSS/RDF/ATOM/whatever and for skinning the blog with
99             alternate templates.
100              
101             =cut
102              
103             sub register_format {
104 33     33 1 61 my ($self, $format, $filename, $content_type) = @_;
105 33         152 $formats{$format} = [$filename, $content_type];
106             }
107              
108             # display formats
109             __PACKAGE__->register_format( html => "template.html", "text/html" );
110              
111             # TODO: put formats in a formats/ folder by default, but add a config option
112             # for their location.
113              
114             # syndication formats (full posts)
115             __PACKAGE__->register_format( atom => "template.atom", "application/atom+xml" ); # "application/atom+xml" );
116             __PACKAGE__->register_format( xml => "template.rss", "application/rdf+xml");
117             __PACKAGE__->register_format( rss => "template.rss", "application/rdf+xml");
118             __PACKAGE__->register_format( rdf => "template.rss", "application/rdf+xml");
119             __PACKAGE__->register_format( rss2 => "template.rss2", "application/rss+xml");
120              
121             # syndication formats (excerpted posts)
122             __PACKAGE__->register_format( atomexcerpt => "template.atomexcerpt", "application/atom+xml" );
123             __PACKAGE__->register_format( xmlexcerpt => "template.rssexcerpt", "application/rdf+xml");
124             __PACKAGE__->register_format( rssexcerpt => "template.rssexcerpt", "application/rdf+xml");
125             __PACKAGE__->register_format( rdfexcerpt => "template.rssexcerpt", "application/rdf+xml");
126             __PACKAGE__->register_format( rss2excerpt => "template.rss2excerpt", "application/rss+xml");
127              
128             sub generate {
129 1     1 1 2 my $class = shift;
130 1         5 my $format = shift;
131 1 50       5 $_[0]->config->frontend->report_error("Unknown format",
132             "Can't output a blog in format '$format', don't know what it is.")
133             if !exists $formats{$format};
134              
135 1         2 my ($file, $ct) = @{$formats{$format}};
  1         7  
136 1         5 return ($ct, $class->_tt_process($file, @_));
137             }
138              
139             =head1 LICENSE
140              
141             This module is free software, and may be distributed under the same
142             terms as Perl itself.
143              
144             =head1 AUTHOR
145              
146             Copyright (C) 2003, Simon Cozens C
147              
148             some parts Copyright 2007 David Cantrell C
149              
150              
151             =head1 SEE ALSO
152              
153             =cut
154              
155             1;