#! /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. use strict; use warnings; use lib 'lib'; use data; use Mojolicious::Lite; # 'app', 'post', 'get', 'app' is exported use Mojo::ByteStream 'b'; 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"); }; # 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 { $self->render(entry_infos => \@entry_infos, size => $size); } # Render index page } => 'index'; app->start; __DATA__ @@ index.html.ep % layout 'default'; <% my $lol; foreach my $entry_info (@$entry_infos) { %>

<%= $entry_info->{title} %> <%= $entry_info->{datetime} %>

<%== $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';
<%= $message %>
@@ style.css.ep h1 { border: 0; margin: 0; padding: 2em; } body { background: #ccc; /* font-family: sans-serif; */ font-family: 'Computer Modern Typewriter Light'; } h3 { padding: 0em 2em; margin: 0em 0em; /* padding: 1em 2em; */ background: #eee; } div.blog-entry { font-size: 80%; } span.date { float: right; font-style: italic; font-weight: normal; font-size: 60%; } /* small */ @media only screen and (max-width: 350px) { .middle { width: 100%; font-size: 60%; } } @media only screen and (min-width: 351px) and (max-width: 600px) { .middle { width: 90%; font-size: 80%; } } @media only screen and (min-width: 601px) and (max-width: 800px) { .middle { width: 80%; font-size: 100%; } } @media only screen and (min-width: 801px) and (max-width: 1000px) { .middle { width: 60%; font-size: 120%; } } @media only screen and (min-width: 1001px) { .middle { width: 50%; font-size: 140%; } } .middle { margin: 0 auto; } #container { background: #E4E4E4; border-radius: .5em; overflow: hidden; } #footer { font-size: 60%; text-align: right; padding-bottom: 2em; } #footer a { margin-right: 2em; } hr { border: 0; clear: both; } h1 { background: #ddd; } p { padding: 2em; } p.categories { font-size: 60%; text-align: right; } p.permalink { font-size: 60%; text-align: right; padding-top: 0; } .number { font-size: 60%; background: #eee; padding: 1em 2em; text-align: right; } @@ layouts/default.html.ep <%= title || "alphanet's blog" %>

<%= title || "alphanet's blog" %>

<%= content %>
@@ rssxml.rss.ep %# not completely RSS compliant (see http://feedvalidator.org/ or %# better http://validator.w3.org/feed/) alphanet's blog https://blog.alphanet.ch/ Random thoughts schaefer@alphanet.ch % foreach my $item (@$items) { <%= $item->{title} %> <%== $item->{message} %> <%= $item->{author_email} %> (<%= $item->{author_name} %>) <%= $item->{datetime} %> <%= $item->{permalink} %> % foreach my $category (@{$item->{categories}}) { <%= $category %> % } % }