Apache-Blog
view release on metacpan or search on metacpan
# the rest is the entry
my $entry;
{ local $/=undef;
$entry = <$fh>;
};
# get the unixtime of the entry too (%s is the Date::Manip way
# of saying "seconds since the epoch")
my $unixtime = Date::Manip::UnixDate($date ,'%s');
# and fix up the date
$date = POSIX::strftime( '%a %d %b %y %H:%M', localtime $unixtime );
# see if we can get any comments
my $comments_ref = [];
if (-d "$filename-comment") {
my @comments = Apache::Blog::Entry->get_all( "$filename-comment" );
$comments_ref = \@comments;
} # end comment if
# store those results (wonder if there's a better way to count
# words than scalar( @{[split /\s+/, $entry]} )
my %self = ( date => $date,
unixtime => $unixtime,
entry => $entry,
short_name => $short_name,
filepath => $filename,
filename => basename( $filename ),
wc => scalar( @{[split /\s+/, $entry]} ),
comments => $comments_ref,
);
return bless(\%self, $class);
} # end new
sub date { return shift->{date} };
sub unixtime { return shift->{unixtime} };
sub short_name { return shift->{short_name} };
sub filename { return shift->{filename} };
sub filepath { return shift->{filepath} };
sub wc { return shift->{wc} };
sub comments { return @{ shift->{comments} } };
# does simple html-formatting of the plain text
sub entry {
my $self = shift;
my $text = $self->{'entry'};
# make UL lists work (perhaps)
# there's bound to be a better way of doing this
$text =~ s/^(\s*)\* (.*)$/$1<li>$2<\/li>/mg;
if ($text =~ /<li>/) {
$text =~ s/<li>/<ul><li>/;
$text = reverse $text;
$text =~ s/>il\/</>lu\/<>il\/</;
$text = reverse $text;
} # end it
# bold?
$text =~ s/\*([^*]+)\*/<b>$1<\/b>/g;
# blank lines -> <p>
$text =~ s/^\s*$/\n<p>\n/mg;
return $text;
} # end entry
# gets all of the entries in a directory
sub get_all {
my ($class, $dir) = @_;
# get all of the details of all of the entries
opendir DIR, $dir;
my @entries = map { Apache::Blog::Entry->new( $dir."/".$_ ) }
grep !/\.html$/ && !/^\./ && !(-d $dir."/".$_), readdir DIR;
closedir DIR;
# now sort those
my @out_entries;
foreach my $entry (sort { $b->unixtime <=> $a->unixtime } @entries) {
push @out_entries, $entry;
} # end foreach
return @out_entries;
} # end get_all
package Apache::Blog;
use strict;
use vars qw( $VERSION );
$VERSION = '0.03';
use Apache::Constants; # qw(:common);
use Apache::Request;
use HTML::Template;
use File::Basename;
use strict;
# this pretty much just dispatches the request to a different handler,
# depending on what's actually been requested.
sub handler {
my $r = shift;
return &handle_comment($r) if ($r->filename =~ /post-comment$/ );
return &handle_older($r) if ( $r->filename =~ /older\.html$/ );
if (-d $r->filename) {
# they've just asked for the directory - need to send newest entry
my @entries = Apache::Blog::Entry->get_all( $r->filename );
my $latest = $entries[0];
$r->filename( $latest->filepath );
}
return handle_file($r);
# construed as a bug.
$r->header_out( 'Location' => $r->header_in( 'Referer' ));
return 302;
} # end handle_comment
1;
__END__
=head1 NAME
Apache::Blog - mod_perl weblog handler
=head1 SYNOPSIS
In httpd.conf
Alias /blog/ /home/daniel/blog/
<Location /blog>
SetHandler perl-script
PerlHandler +Apache::Blog
</Location>
=head1 DESCRIPTION
Apache::Blog is a simple handler for online diaries. At the moment it
works on the one-entry-one-page paradigm, but would be easy to apapt to
multiple entries per page if this is prefered. In the future this will
be a configuration option.
It is inspired by the service offered at http://www.diaryland.com/
It uses HTML::Template, so it is easy to design new layouts. There are
some samples included with the distribution.
All diary entries are stored as plain text files, there's no database
stuff going on here. This is to make it simple to add entries - any
editor can be used to write entries.
To use, decide on a directory which is to hold your weblog entries,
and set PerlHandler like in the example. The alias isn't nessasary,
but I like it that way.
Also in that directory need to be two template files, one called
entry-template.html, and one called older.html.
Entries take the format:
Short Title
Thu Jun 20 17:24:52 BST 2002
The entry down here.
The date can be in any format that Date::Manip likes. In vim I do :r!date
to add the date line.
Apache::Blog does some simple manipulation on the text. It will turn
lines which start with a * into bullet lists, and blank lines are turned
into <p> tags. You can also *bold* text. It doesn't highlight links or
anything. A more sophisticated text->html converter may be included in
the future.
The module can also allow comments on entries, for this to work properly
the webserver must have write access to the directory containing your
entries.
Entry filenames can be anything you like, as long as it doesn't end in
.html or start with a period. I generally go with filenames like "2002-06-20"
See the sample layouts, especially the "simple" layout, to see how to
create your own.
Someone should write a blogger -> Apache::Blog template converter, one
for diaryland templates as well. Best way to do that would probably be
an HTML::Template filter so it's transparent. There's a lot of templates
for the different services out there so that's probably a good itch to
get scratched.
=head1 AUTHOR
Daniel Gardner <daniel@danielgardner.org>
=head1 SEE ALSO
HTML::Template, Date::Manip
=cut
( run in 0.574 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )