File Coverage

blib/lib/Ado/Control/Articles.pm
Criterion Covered Total %
statement 29 30 96.6
branch 4 4 100.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 38 39 97.4


line stmt bran cond sub pod time code
1             package Ado::Control::Articles;
2 2     2   13387 use Mojo::Base 'Ado::Control';
  2         4  
  2         11  
3             File::Spec::Functions->import(qw(catfile splitpath catdir));
4             Mojo::ByteStream->import(qw(b));
5 2     2   378 use File::Path qw(make_path);
  2         4  
  2         112  
6 2     2   12 use Mojo::File qw(path);
  2         4  
  2         771  
7              
8             sub show {
9 4     4 1 45 my ($c) = @_;
10 4         18 state $config = $c->app->config('Ado::Plugin::MarkdownRenderer');
11 4         42 state $root = $config->{md_articles_root};
12 4         19 my ($html_content, $html_file) = ('', $c->stash->{html_file});
13 4         48 my $md_file = $html_file;
14 4         65 $md_file =~ s/(\.[^\.]+)?$/$config->{md_file_sufixes}[0]/; #switch file extension
15 4         37 my $file_path = catfile($root, $md_file);
16              
17 4 100       124 if (-s $file_path) {
18 3         15 my $markdown = b(path($file_path)->slurp)->decode->to_string;
19              
20 3 100       736 if ($config->{md_reuse_produced_html}) {
21 1         24 $html_content = $c->render_to_string('articles/show',
22             html => $c->markdown($markdown, {self_url => $c->url_for->to_string}));
23              
24             #save file to disk for later requests and redirect to the generated static file
25 1         363 my $html_path = catfile($root, $html_file);
26 1         15 make_path(catdir((splitpath($html_path))[0 .. -2]));
27 1         92 path($html_path)->spurt(b($html_content)->encode);
28 1         371 $c->redirect_to($c->url_for('/articles/' . $html_file));
29 1         60 return;
30             }
31             else {
32 2         21 $c->render(html => $c->markdown($markdown, {self_url => $c->url_for->to_string}));
33 2         121 return;
34             }
35             }
36             else {
37 1         7 $c->res->code(404);
38 1         28 return;
39             }
40 0           Carp::croak('Should never get here!');
41             }
42              
43             1;
44              
45             =pod
46              
47             =encoding utf8
48              
49             =head1 NAME
50              
51             Ado::Control::Articles - display markdown documents.
52              
53             =head1 SYNOPSIS
54              
55             #in your browser go to
56             http://your-host/articles
57              
58              
59             =head1 DESCRIPTION
60              
61             Ado::Control::Articles is a controller that generates full static HTML
62             documents for markdown files found in the folder specified in
63             C in C. It allows
64             you to have a simple static blog on Ado in no time, i.e. install Ado and you
65             have a personal blog.
66              
67             =head1 METHODS/ACTIONS
68              
69             L inherits all the methods from
70             L and defines the following.
71              
72             =head2 show
73              
74             Renders the file found in C<$c-Estash('html_file')> but with extension
75             C<.md>. If C<$config-E{md_reuse_produced_html}> is set, the produced html
76             file is saved in C<$config-E{md_articles_root}>. This way the next time
77             the resource is requested L renders the produced static file.
78              
79             =head1 SEE ALSO
80              
81             L, L,
82             L, L,
83             L
84             L, L.
85              
86             =head1 AUTHOR
87              
88             Красимир Беров (Krasimir Berov)
89              
90             =cut
91