2015-03-08 22:11:15 +00:00
|
|
|
package App::BlogAlba;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
use App::BlogAlba::Article;
|
|
|
|
use Data::Paginated;
|
2015-03-08 22:11:15 +00:00
|
|
|
|
2015-03-11 22:43:15 +00:00
|
|
|
use Time::HiRes qw/gettimeofday tv_interval/;
|
2015-03-11 21:32:50 +00:00
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
use Sys::Hostname;
|
|
|
|
my $HOST = hostname; $HOST =~ s/\..*$//;
|
2015-03-09 19:42:05 +00:00
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
use Dancer2;
|
|
|
|
use Dancer2::Plugin::Feed;
|
2015-03-08 22:11:15 +00:00
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
#Sanitise our base URL
|
|
|
|
config->{url} =~ /\/$/ or config->{url} .= '/';
|
2015-03-08 22:11:15 +00:00
|
|
|
|
|
|
|
sub pagination_calc {
|
2015-11-15 22:23:12 +00:00
|
|
|
my $rem=$nposts % config->{conf}->{per_page};
|
|
|
|
$npages=($nposts-$rem)/config->{conf}->{per_page};
|
2015-03-08 22:11:15 +00:00
|
|
|
$npages++ if $rem>0 or $npages<1;
|
|
|
|
}
|
|
|
|
sub get_index {
|
|
|
|
my @iposts = @_;
|
2015-11-15 22:23:12 +00:00
|
|
|
$page->param(pagetitle => config->{name}, INDEX => 1, POSTS => [@iposts]);
|
2015-03-08 22:11:15 +00:00
|
|
|
return $page->output;
|
|
|
|
}
|
|
|
|
sub paginate {
|
2015-11-15 22:23:12 +00:00
|
|
|
my $pagenum = shift; my $offset = ($pagenum-1)*config->{conf}->{per_page};
|
|
|
|
my $offset_to = $offset+(config->{conf}->{per_page}-1); $offset_to = $#posts if $offset_to > $#posts;
|
2015-03-08 22:11:15 +00:00
|
|
|
$page->param(PAGINATED => 1, prevlink => ($pagenum>1? 1 : 0), prevpage => $pagenum-1, nextlink => ($pagenum<$npages? 1 : 0), nextpage => $pagenum+1);
|
2015-11-15 22:23:12 +00:00
|
|
|
return get_index @posts[$offset..(($offset+config->{conf}->{per_page})>$#posts? $#posts : ($offset+(config->{conf}->{per_page}-1)))];
|
2015-03-08 22:11:15 +00:00
|
|
|
}
|
|
|
|
sub get_post {
|
|
|
|
my ($y,$m,$slug) = @_;
|
|
|
|
for my $r (@posts) {
|
|
|
|
my %post = %$r;
|
|
|
|
next unless $post{slug} eq $slug and timefmt($post{time},'writepost') eq "$y-$m";
|
2015-11-15 22:23:12 +00:00
|
|
|
$page->param(pagetitle => $post{title}." - ".config->{name},%post);
|
2015-03-08 22:11:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
sub get_page {
|
|
|
|
my $pname = shift;
|
|
|
|
for my $r (@pages) {
|
|
|
|
my %cpage = %$r;
|
|
|
|
next unless $cpage{filename} eq $pname;
|
2015-11-16 00:16:32 +00:00
|
|
|
$page->param(pagetitle => $cpage{title}." - ".config->{name},%cpage);
|
2015-03-08 22:11:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
sub do_cache {
|
|
|
|
return if $lastcache > (time - 3600);
|
2015-03-11 22:43:15 +00:00
|
|
|
$lastcache = time;my $st=[gettimeofday];
|
2015-11-06 16:31:38 +00:00
|
|
|
undef @posts;undef @pages;$nposts=0;
|
2015-03-08 22:11:15 +00:00
|
|
|
opendir POSTS, "$basedir/posts/" or die "Couldn't open posts directory $basedir/posts/";
|
|
|
|
while(readdir POSTS) {
|
2015-03-08 23:51:30 +00:00
|
|
|
next unless /\.md$/;
|
|
|
|
warn "Error reading post $_\n" and next unless readpost("$basedir/posts/$_",1);
|
2015-03-08 22:11:15 +00:00
|
|
|
$nposts++;
|
|
|
|
}
|
|
|
|
closedir POSTS;
|
|
|
|
@posts = map {$_->[1]} sort {$b->[0] <=> $a->[0]} map {[$_->{time},$_]} @posts;
|
|
|
|
|
|
|
|
opendir PAGES, "$basedir/pages/" or die "Couldn't open pages directory $basedir/pages/";
|
|
|
|
while(readdir PAGES) {
|
2015-03-08 23:51:30 +00:00
|
|
|
next unless /\.md$/;
|
2015-03-09 02:37:05 +00:00
|
|
|
warn "Error reading page $_\n" and next unless readpost("$basedir/pages/$_",2);
|
2015-03-08 22:11:15 +00:00
|
|
|
}
|
|
|
|
closedir PAGES;
|
|
|
|
|
|
|
|
my @nav;
|
2015-11-15 22:23:12 +00:00
|
|
|
push @nav, {navname => $_->{title}, navurl => config->{url}.$_->{filename},} for @pages;
|
|
|
|
push @nav, {navname => $_, navurl => config->{links}->{$_},} for sort { $b cmp $a } keys config->{links};
|
2015-03-11 22:24:30 +00:00
|
|
|
generate_feed;
|
2015-03-08 22:11:15 +00:00
|
|
|
%defparams = (
|
2015-11-15 22:23:12 +00:00
|
|
|
INDEX => 0, NAV => [@nav], url => config->{url}, recent => [@posts[0 .. ($#posts > (config->{conf}->{recent_posts}-1)? (config->{conf}->{recent_posts}-1) : $#posts)]],
|
|
|
|
gentime => timefmt($lastcache, '%H:%M %e/%-m/%y %Z'), genworktime => sprintf("%.2f ms", tv_interval($st)*100), host => $HOST, rss_enabled => config->{rss_publish},
|
|
|
|
about => config->{about}, author => config->{author}, name => config->{name}, tagline => config->{tagline}, keywords => config->{keywords},
|
|
|
|
robots => config->{conf}->{indexable}? '<meta name="ROBOTS" content="INDEX, FOLLOW" />' : '<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />',
|
2015-03-08 22:11:15 +00:00
|
|
|
);
|
|
|
|
pagination_calc;
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
sub GetPost {
|
|
|
|
my $params = shift or return undef;
|
|
|
|
return undef unless
|
|
|
|
$params->{year} =~ /^[0-9]{4}$/ and
|
|
|
|
$params->{month} =~ /^(0[1-9]|1[0-2])$/ and
|
|
|
|
$params->{slug};
|
|
|
|
|
|
|
|
for my $article (@articles) {
|
|
|
|
next unless
|
|
|
|
$article->{slug} eq lc $params->{slug} and
|
|
|
|
$article->{yyyymm} eq $params->{year}.$params->{month};
|
|
|
|
return $article;
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2015-03-08 22:11:15 +00:00
|
|
|
hook 'before' => sub {
|
2015-03-08 22:43:02 +00:00
|
|
|
do_cache;
|
2015-03-08 22:11:15 +00:00
|
|
|
page_init;
|
|
|
|
};
|
|
|
|
|
2015-12-28 20:09:51 +00:00
|
|
|
#Indexes
|
2015-03-08 22:11:15 +00:00
|
|
|
get '/' => sub {
|
|
|
|
return get_index @posts if $npages==1;
|
|
|
|
return paginate 1;
|
|
|
|
};
|
2015-12-28 20:09:51 +00:00
|
|
|
get '/page/:num' => sub {
|
2015-03-08 22:11:15 +00:00
|
|
|
pass unless params->{id} =~ /^[0-9]+$/ and params->{id} <= $npages;
|
|
|
|
return redirect '/' unless $npages > 1 and params->{id} > 1;
|
|
|
|
return paginate params->{id};
|
|
|
|
};
|
2015-12-28 20:09:51 +00:00
|
|
|
|
|
|
|
#Published articles
|
|
|
|
get '/:page' => sub {
|
2015-03-15 01:26:57 +00:00
|
|
|
pass unless params->{extpage} =~ /^[a-z0-9\-]+(?:\.md)?$/i;
|
2015-03-15 02:15:11 +00:00
|
|
|
if (params->{extpage} =~ s/\.md$//) { $page->param(SOURCEVIEW => 1); header('Content-Type' => 'text/plain'); }
|
2015-03-08 22:11:15 +00:00
|
|
|
$page->param(ISPOST => 0);
|
|
|
|
get_page params->{extpage} or pass;
|
|
|
|
return $page->output;
|
|
|
|
};
|
2015-12-28 20:09:51 +00:00
|
|
|
get '/wrote/:yyyy/:mm/:slug' => sub {
|
|
|
|
my $article = GetPost scalar params 'route';
|
|
|
|
pass unless $article;
|
|
|
|
content_tyle 'text/plain' and return $article->{raw}
|
|
|
|
if params->{slug} =~ /\.md$/;
|
|
|
|
|
|
|
|
return template 'post', { page => $article };
|
|
|
|
};
|
|
|
|
|
|
|
|
#Feeds
|
|
|
|
get '/feed/:type' => sub {
|
|
|
|
pass unless params->{type} =~ /^(rss|atom)$/i;
|
|
|
|
create_feed
|
|
|
|
format => lc params->{type},
|
|
|
|
title => config->{blogtitle},
|
|
|
|
link => request->base,
|
|
|
|
entries => \@articles;
|
|
|
|
}
|
|
|
|
get qr{/feed-(atom|rss2).xml} => sub { redirect '/feed/'.splat; }
|
|
|
|
|
2015-03-15 01:26:57 +00:00
|
|
|
# 404
|
2015-11-15 22:23:12 +00:00
|
|
|
any qr/.*/ => sub {
|
2015-12-28 20:09:51 +00:00
|
|
|
redirect '/' if request->path =~ /index\.(html?|pl)$/;
|
2015-11-15 22:23:12 +00:00
|
|
|
return send_error('The page you seek cannot be found.', 404);
|
2015-03-15 01:26:57 +00:00
|
|
|
};
|
2015-03-08 22:11:15 +00:00
|
|
|
|
2015-11-15 22:23:12 +00:00
|
|
|
1;
|
|
|
|
__END__
|