Muster/Assemble.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Muster.pm ↑ Summary Muster/Hook/Img.pm »
1 package Muster::Assemble;
2 $Muster::Assemble::VERSION = '0.96';
3 #ABSTRACT: Muster::Assemble - page rendering
4 =head1 NAME
5  
6 Muster::Assemble - page rendering
7  
8 =head1 VERSION
9  
10 version 0.96
11  
12 =head1 SYNOPSIS
13  
14 use Muster::Assemble;
15  
16 =head1 DESCRIPTION
17  
18 Content Management System - page rendering
19  
20 =cut
21 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Muster::MetaDb;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
28 use YAML::Any;
29 use Module::Pluggable search_path => ['Muster::Hook'], instantiate => 'new';
30  
31 =head1 Methods
32  
33 =head2 init
34  
35 Initialize.
36  
37 =cut
38 sub init {
39 my $self = shift;
40 my $c = shift;
41 my $app = $c->app;
42  
43 if (!$self->{metadb})
44 {
45 $self->{metadb} = Muster::MetaDb->new(%{$app->config});
46 $self->{metadb}->init();
47 }
48 $self->{hookmaster} = Muster::Hooks->new();
49 $self->{hookmaster}->init($app->config);
50  
51 $self->{file_magic} = File::LibMagic->new();
52  
53 return $self;
54 } # init
55  
56 =head2 serve_page
57  
58 Serve one page (or a file)
59  
60 =cut
61 sub serve_page {
62 my $self = shift;
63 my $c = shift;
64 my $app = $c->app;
65  
66 $self->init($c);
67  
68 # If a page is requested, there ought to be a trailing slash in the cpath.
69 # If there isn't, either this isn't canonical, or it isn't a page request.
70 # However, pagenames don't have a trailing slash.
71 # Yes, this is confusing.
72 # So why do we care if a URL is canonical? Because if it isn't canonical,
73 # it messes up relative page links. Ugh.
74 my $pagename = $c->param('cpath') // 'index';
75 my $has_trailing_slash = 0;
76 my $is_source_file_request = 0;
77 if ($pagename =~ m!/$!)
78 {
79 $has_trailing_slash = 1;
80 $pagename =~ s!/$!!;
81 }
82 elsif ($pagename =~ m!(.*)\.\w+$!) # source-file or file requests have .ext suffixes
83 {
84 $pagename = $1;
85 $is_source_file_request = 1;
86 }
87  
88 # now we need to find if this page exists, and what type it is
89 my $info = $self->{metadb}->page_or_file_info($pagename);
90 unless (defined $info and defined $info->{filename} and -f -r $info->{filename})
91 {
92 $c->reply->not_found;
93 return;
94 }
95  
96 if ($is_source_file_request)
97 {
98 return $self->_serve_file(controller=>$c, meta=>$info);
99 }
100 elsif (!$has_trailing_slash and $pagename ne 'index') # non-canonical
101 {
102 my $redir = "/${pagename}/";
103 return $c->redirect_to($redir);
104 }
105  
106 # Make sure that "head_append" is defined
107 $c->stash('head_append' => "");
108  
109 my $leaf = $self->_create_and_process_leaf(controller=>$c,meta=>$info);
110  
111 my $html = $leaf->html();
112 unless (defined $html)
113 {
114 say STDERR "404: html not rendered for '$pagename'";
115 $c->reply->not_found;
116 return;
117 }
118 my $filtered_html = $self->{hookmaster}->run_filters(
119 html=>$html,
120 controller=>$c,
121 phase=>$Muster::Hooks::PHASE_FILTER);
122  
123 $c->stash('title' => $leaf->title);
124 $c->stash('pagename' => $pagename);
125 $c->stash('content' => $filtered_html);
126 $c->render(template => 'page',
127 format => ($info->{render_format} ? $info->{render_format} : 'html'));
128 } # serve_page
129  
130 =head2 serve_meta
131  
132 Serve the meta-data for a page (for debugging purposes)
133  
134 =cut
135 sub serve_meta {
136 my $self = shift;
137 my $c = shift;
138 my $app = $c->app;
139  
140 $self->init($c);
141  
142 my $pagename = $c->param('cpath') // 'index';
143 $pagename =~ s!/$!!; # remove trailing slash
144  
145 my $info = $self->{metadb}->page_or_file_info($pagename);
146 unless (defined $info)
147 {
148 $c->reply->not_found;
149 return;
150 }
151  
152 my $html = "<pre>\n" . Dump($info) . "\n</pre>\n";
153  
154 $c->stash('title' => $info->{title});
155 $c->stash('pagename' => $pagename);
156 $c->stash('content' => $html);
157 $c->render(template => 'page');
158 }
159  
160 =head1 Helper Functions
161  
162 =head2 _serve_file
163  
164 Serve a file rather than a page.
165
166 $self->_serve_file(controller=>$c, meta=>$meta);
167  
168 =cut
169  
170 sub _serve_file {
171 my $self = shift;
172 my %args = @_;
173 my $c = $args{controller};
174 my $meta = $args{meta};
175  
176 my $filename = $meta->{filename};
177 if (!-f $filename)
178 {
179 # not found
180 return;
181 }
182 # Figure out the correct mime-type to give
183 my $file_info = $self->{file_magic}->info_from_filename($filename);
184 $c->res->headers->content_type($file_info->{mime_type});
185 $c->reply->file($filename);
186  
187 } # _serve_file
188  
189 =head2 _create_and_process_leaf
190  
191 Create and process a leaf (which contains meta-data and content).
192 This leaf data comes from the database (apart from the content).
193
194 $leaf = $self->_create_and_process_leaf(controller=>$c,meta=>$meta);
195  
196 =cut
197  
198 sub _create_and_process_leaf {
199 my $self = shift;
200 my %args = @_;
201 my $c = $args{controller};
202 my $meta = $args{meta};
203  
204 my $leaf = Muster::LeafFile->new(
205 pagename=>$meta->{pagename},
206 pagesrcname=>$meta->{pagesrcname},
207 parent_page=>$meta->{parent_page},
208 grandparent_page=>$meta->{grandparent_page},
209 filename=>$meta->{filename},
210 filetype=>$meta->{filetype},
211 is_binary=>$meta->{is_binary},
212 extension=>$meta->{extension},
213 bald_name=>$meta->{bald_name},
214 hairy_name=>$meta->{hairy_name},
215 title=>$meta->{title},
216 date=>$meta->{date},
217 meta=>$meta,
218 );
219 $leaf = $leaf->reclassify();
220 if (!$leaf)
221 {
222 croak "ERROR: leaf did not reclassify\n";
223 }
224  
225 return $self->{hookmaster}->run_hooks(leaf=>$leaf,
226 controller=>$c,
227 phase=>$Muster::Hooks::PHASE_BUILD);
228 } # _create_and_process_leaf
229 1;
230 # end of Muster::Assemble
« Muster.pm ↑ Summary Muster/Hook/Img.pm »