File Coverage

blib/lib/Riji.pm
Criterion Covered Total %
statement 28 52 53.8
branch 1 10 10.0
condition 0 3 0.0
subroutine 10 16 62.5
pod 0 4 0.0
total 39 85 45.8


line stmt bran cond sub pod time code
1             package Riji;
2 5     5   9210 use 5.010;
  5         32  
3 5     5   30 use strict;
  5         10  
  5         129  
4 5     5   26 use warnings;
  5         16  
  5         168  
5 5     5   3068 use Puncheur::Lite;
  5         489290  
  5         34  
6              
7 5     5   15290 use Encode;
  5         11  
  5         462  
8 5     5   38 use File::Spec;
  5         13  
  5         102  
9 5     5   3107 use YAML::Tiny ();
  5         29639  
  5         180  
10              
11 5     5   41 use version 0.77; our $VERSION = version->declare("v1.0.0");
  5         85  
  5         40  
12              
13             __PACKAGE__->setting(
14             handle_static => 1,
15             );
16             __PACKAGE__->load_plugins(qw/Model ShareDir/);
17              
18 2     2 0 286 sub base_dir { state $b = File::Spec->rel2abs('./') }
19              
20             sub load_config {
21 1     1 0 217 my $self = shift;
22 1         44 my $file = File::Spec->catfile($self->base_dir, 'riji.yml');
23 1 50       54 unless (-e $file) {
24 0         0 die sprintf "config file: [%s] not found.\n", $file;
25             }
26 1         22 YAML::Tiny::LoadFile($file);
27             }
28              
29             get '/{match:(?:[-_a-zA-Z0-9]+(?:\.[0-9]+)?.html)?}' => sub {
30             my ($c, $args) = @_;
31              
32             my $match = $args->{match} || 'index.html';
33             my ($basename, $page) = $match =~ m!^([-_a-zA-Z0-9]+)(?:\.([0-9]+))?\.html$!;
34              
35             my $blog = $c->model('Blog');
36             my $article = $blog->article($basename, {$page ? (page => $page) : ()});
37              
38             if (!$article && $basename ne 'index') {
39             return $c->res_404;
40             }
41              
42             my $tmpl = $article && $article->template;
43             unless (defined $tmpl) {
44             $tmpl = $basename if $basename eq 'index';
45             $tmpl //= 'default';
46             }
47             $tmpl .= '.tx' unless $tmpl =~ /\.tx$/;
48              
49             $c->render($tmpl, {
50             blog => $blog,
51             page => $page,
52             article => $article,
53             });
54             };
55              
56             my $s = '[-_a-zA-Z0-9]+';
57             get "/entry/{name:$s(?:\.$s)*(?:/$s(?:\.$s)*)*}.html" => sub {
58             my ($c, $args) = @_;
59              
60             my $name = $args->{name};
61             my $blog = $c->model('Blog');
62             my $entry = $blog->entry($name);
63             return $c->res_404 unless $entry;
64              
65             my $tmpl = $entry->template // 'entry';
66             $tmpl .= '.tx' unless $tmpl =~ /\.tx$/;
67              
68             $c->render($tmpl, {
69             blog => $blog,
70             entry => $entry,
71             });
72             };
73              
74             get '/tag/:tag.html' => sub {
75             my ($c, $args) = @_;
76              
77             my $tag = $args->{tag};
78             my $blog = $c->model('Blog');
79             $tag = $blog->tag($tag);
80             return $c->res_404 unless $tag;
81              
82             $c->render('tag.tx', {
83             blog => $blog,
84             tag => $tag,
85             });
86             };
87              
88             get '/atom.xml' => sub {
89             my $c = shift;
90              
91             my $atom = $c->model('Blog')->atom;
92             my $xml = $atom->feed->to_string;
93             my $atom_url = $atom->site_url . '/atom.xml';
94             $atom_url =~ s{//atom\.xml$}{/atom.xml};
95             $xml =~ s{(\s+)(]+>)}{$1$2$1};
96             $c->create_response(200, ['Content-Type' => 'application/atom+xml'], [encode($c->encoding, $xml)]);
97             };
98              
99             sub get_functions {
100 0     0 0   my $self = shift;
101              
102 0           state %functions;
103 0           my $functionspl = File::Spec->catfile($self->base_dir, 'share', 'functions.pl');
104 0 0 0       if (-f -r $functionspl && !%functions) {
105 0           my $code = do {
106 0           local $/;
107 0 0         open my $fh, '<', $functionspl or die $!;
108             <$fh>
109 0           };
110 0           my $package = 'Riji::_Sandbox::Functions';
111 0           eval <<"..."; ## no critic
112             package $package;
113             use strict;
114             use warnings;
115             use utf8;
116              
117             $code
118             1;
119             ...
120 0 0         if (my $err = $@) {
121 0           die "$err\n";
122             }
123 0           require Module::Functions;
124 0           my @functions = Module::Functions::get_public_functions($package);
125 0           for my $func (@functions) {
126 0           $functions{$func} = $package->can($func);
127             }
128             }
129 0           %functions;
130             }
131              
132             sub create_view {
133 0     0 0   my $self = shift;
134              
135             Text::Xslate->new(
136             path => $self->template_dir,
137             module => [
138             'Text::Xslate::Bridge::Star',
139             ],
140             function => {
141 0     0     c => sub { $self->context },
142 0     0     uri_for => sub { $self->context->uri_for(@_) },
143 0     0     uri_with => sub { $self->context->req->uri_with(@_) },
144             $self->get_functions,
145             },
146             ($self->debug_mode ? ( warn_handler => sub {
147 0     0     Text::Xslate->print( # print method escape html automatically
148             '[[', @_, ']]',
149             );
150 0 0         } ) : () ),
151             );
152             }
153              
154             1;
155             __END__