Blog Update

It's been a while (4 months) since I created this blogging software, and I'm still happy with it. I've made a few updates to the code since I originally posted. So I thought I'd post an update.

I didn't post the RSS last time, so here's the current copy:

package Blog::RSS;

use strict;
use warnings;

use XML::Atom::SimpleFeed;
use File::Grep 'fgrep';
use Path::Class;
use Data::Dumper;
use Text::Markdown 'markdown';
use DateTime;
use Template;
use DateTime::Format::W3CDTF;
use Apache2::Const;

sub handler
{
    my $r = shift;

    my $filedir = dir($r->dir_config('files'));

    my $feed = XML::Atom::SimpleFeed->new(
        title => $r->dir_config('blog_title'),
        link => $r->dir_config('blog_link'),
        id => $r->dir_config('blog_id'),
        author => $r->dir_config('author'),
        );

    ## All articles which are public, and not in-progress (~, .# etc)
    my @articles = 
        fgrep { /\@.*public/ } 
        map { "$_" }
        sort { 
               ($a->stat->ctime <=> $b->stat->ctime) or
               ($a->stringify   cmp $b->stringify)
      }
      grep { !$_->is_dir && $_->stringify =~ m{.*/[-\w]+\.txt$} } 
      $filedir->children;

    @articles = @articles[-18 .. -1];
   # print STDERR Dumper(\@articles);

    my $macros = $r->dir_config('macros');
    my $tt = Template->new({
        ABSOLUTE => 1,
#        ENCODING => 'utf8',
#        WRAPPER => $wrapper,
        PRE_PROCESS => $macros,
#        INCLUDE_PATH => $filedir 
                            });

    foreach my $art (@articles) {
        next if(!$art->{count});
        my $f = file($art->{filename});
        (my $fname = $f->basename) =~ s/\.txt$/.html/;
        my $dt_formatter = DateTime::Format::W3CDTF->new();
        my $dt_updated = DateTime->from_epoch(epoch=> $f->stat->ctime);

        ## Assuming we always put all the tags on one line
        my ($category_line) = fgrep { /^\@.*public/} "$f";
        $category_line = (values %{ $category_line->{matches} })[0];
        chomp($category_line);
        my @categories = split(/,/, substr($category_line, 1));

        my $tt_processed ='';
        my $fh = $f->openr();
        binmode($fh, ':utf8');
        $tt->process($fh, { photouri => $r->dir_config('blog_photo_base') }, \$tt_processed);
        ## According to the spec "author" is picked up from the header if not in
        ## the entry. Not all atom parsers do this (see plagger!)
        $feed->add_entry(
            title => "Under the palm tree ($fname)",
            link => file($r->dir_config('blog_link'), $fname)->stringify,
            id => 'http://desert-island.me.uk:8888/~castaway/blog/' . $fname,
            content => markdown($tt_processed),
            updated => $dt_formatter->format_datetime($dt_updated),
            author => $r->dir_config('author'),
            (map { (category => $_) } @categories),
            );
    }

    ## XML::Atom::SimpleFeed outputs as us-ascii without asking!
#    $r->content_type('application/atom+xml;charset=UTF-8');
    $r->content_type('application/atom+xml;charset=us-ascii');
    $feed->print;

    return OK;

}

1;

I've just (literally minutes ago) added some extras: The link/id/author texts were in the code, I moved them into the apache location section, so now (in theory) I could have multiple blogs with different names and ids.

I also added categories (tags), which I'd failed to do originally. I caught myself mumbling at Ironman users who miss them out, then complain that their entries don't show up, so I guess I should tag my own entries!

Since there are a fair few entries now, I've added sorting and reduced the output to the most recent twenty. I hope that's enough to give new users something to read, while reducing the overall server load somewhat.

A recent entry contains a GBP currency symbol (£), which as you'll note, shows up ok on the actual entry page, but not in the RSS. I tried to fix this, but as you see from my note, XML::Atom::SimpleFeed outputs it's XML as "us-ascii" no matter what the user may want. So I may have to go nudge Aristotle with a patch sometime soon.

Also I need to go patch or nudge the [File::Grep]((http://search.cpan.org/dist/File-Grep) author, as the docs claim "fgrep returns an array of matches", but actually it returns a hashref of data.

I did get around to adding images a while ago, an image can now be linked as a public thumbnail to another of my softwares, PhotoOp, using a TT macro.

Here's the apache config:

<Location /~castaway/blog>
    SetHandler  perl-script
    PerlHandler Blog::Handler
    PerlHandler Blog::RSS
    PerlSetVar files /mnt/nessie/projects/gtd/stikkit/
    PerlSetVar wrapper /mnt/nessie/projects/blog/wrapper.html
    PerlSetVar macros /mnt/nessie/projects/blog/macros.tt
    PerlSetVar author 'Jess Robinson'
    PerlSetVar blog_title 'Under the palm tree'
    PerlSetVar blog_link 'http://desert-island.me.uk/~castaway/blog'
    PerlSetVar blog_id 'http://desert-island.me.uk/blog'
    PerlSetVar blog_photo_base 'http://desert-island.me.uk:5005'
</Location>

TODO

@public,perl,blog

Last modified: 2009-05-23T18:07:12

WTF