File Coverage

blib/lib/Ado/Control/Articles.pm
Criterion Covered Total %
statement 26 27 96.3
branch 4 4 100.0
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 34 35 97.1


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