#! /usr/bin/env perl # forked from mojolicious-blog # BUGS # - auto-conversion to utf8?? # TODO # - set real URL + use it (redirect_to()) in config e.g. # - Model should cache data and provide OO interface use strict; use warnings; use lib 'lib'; use data; use Mojolicious::Lite; # 'app', 'post', 'get', 'app' is exported use Mojo::ByteStream 'b'; use Mojo::Util qw(url_escape); plugin 'RenderFile'; helper data => sub { state $data = data->new }; # argh use utf8; get '/blog.pl' => sub { my $self = shift; $self->render_file('filepath' => "blog.pl"); }; # http://ofosos.org/simple-mojolicious-rss-feed.html get '/rss.xml' => sub { my $self = shift; $self->stash('items', $self->data->entries(0, undef)); $self->render(format => "rss"); }; # https://www.sitemaps.org/protocol.html#sitemapXMLExample get '/sitemap.xml' => sub { my $self = shift; my %categories; foreach my $i (@{$self->data->entries(0, undef)}) { foreach my $c (@{$i->{categories}}) { $categories{$c} = undef; } } $self->stash('items', $self->data->entries(0, undef)); $self->stash('categories', [keys %categories]); $self->render(format => "xml"); }; # workaround for just => 'style'; which doesn't work get '/style.css' => sub { my $self = shift; $self->render('style', format => 'css'); }; get '/entry/' => sub { my $self = shift; $self->redirect_to("/"); }; # TODO # - use chains (D.R.Y with /) get '/entry/:entry' => sub { my $self = shift; if (my $entry = $self->param('entry')) { # must be a number $entry += 0; # BUGS # - 0 is seen as undef, so hack $entry -= 1; my $entries = $self->data->entries(1, undef); $self->stash('entry' => $entry); my $size = scalar(@{$entries}); if (($entry >= 0) && ($size < $entry)) { $self->render('noposts'); } else { $self->stash('title', "alphanet's blog: " . $entries->[$entry]->{title}); $self->render('index', entry_infos => [ $entries->[$entry] ], size => 1); } } else { $self->redirect_to('/'); } }; # TODO # - use chains (D.R.Y with /) get '/category/:category' => sub { my $self = shift; if (my $category = $self->param('category')) { # we do not validate category for now my @entry_infos = reverse @{$self->data->entries(1, $category)}; $self->stash('category' => $category); $self->stash('title' => 'alphanet\'s blog [' . $category . ']'); my $size = scalar(@entry_infos); if ($size == 0) { $self->render('noposts'); } else { $self->render('index', entry_infos => \@entry_infos, size => $size); } } else { $self->redirect_to('/'); } }; get '/' => sub { my $self = shift; my @entry_infos = reverse @{$self->data->entries(1, undef)}; my $size = scalar(@entry_infos); if ($size == 0) { $self->render('noposts'); } else { my %tags; map { my $e = $_; map { my $count = 0; if (exists($tags{$_})) { $count = $tags{$_}->{'count'}; } $tags{$_} = { 'url' => 'https://blog.alphanet.ch/category/' . url_escape($_), 'count' => $count + 1 }; } @{$e->{categories}}; } @entry_infos; if (keys %tags) { $self->render(entry_infos => \@entry_infos, size => $size, tags => \%tags); } else { $self->render(entry_infos => \@entry_infos, size => $size); } } # Render index page } => 'index'; app->start; __DATA__ @@ index.html.ep % use HTML::TagCloud; % my $cloud; % if (defined(stash("tags"))) { % my $t = stash("tags"); % $cloud = new HTML::TagCloud(levels => 20); % map { $cloud->add($_, $t->{$_}->{'url'}, $t->{$_}->{'count'}) } keys %{$t}; % } % layout 'default', style => defined($cloud) ? $cloud->css() : undef; % if (defined($cloud)) {
<%== $entry_info->{message} %>
% foreach my $c (@{$entry_info->{categories}}) { <%= $c %> % }
% if (!exists stash->{category}) { # not compatible counting % }<%= $size %> entries so far.
% if (exists stash->{category}) { unlimit categories (currently: <%= stash->{category} %>) % } % if (exists stash->{entry}) { full list % } @@ noposts.html.ep % layout 'default';No entries.
@@ error.html.ep % layout 'default'; % title 'Add post';