#!/usr/bin/perl # Blosxom # Author: Rael Dornfest # Version: 2.0b4 # Home/Docs/Licensing: http://www.raelity.org/apps/blosxom/ package blosxom; # --- Configurable variables ----- # What's this blog's title? $blog_title = "Sample Blog"; # What's this blog's description (for outgoing RSS feed)? $blog_description = "Yet another Blosxom blog."; # What's this blog's primary language (for outgoing RSS feed)? $blog_language = "en"; # Where are this blog's entries kept? $datadir = "/home/nukenuke/public_html/pollxn/sampleblog/docs"; # What's my preferred base URL for this blog (leave blank for automatic)? $url = ""; # Should I stick only to the datadir for items or travel down the # directory hierarchy looking for items? If so, to what depth? # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down $depth = 0; # How many entries should I show on the home page? $num_entries = 40; # What file extension signifies a blosxom entry? $file_extension = "txt"; # What is the default flavour? $default_flavour = "html"; # --- Plugins (Optional) ----- # Where are my plugins kept? $plugin_dir = "/home/nukenuke/public_html/pollxn/sampleblog/plugins"; # Where should my modules keep their state information? $plugin_state_dir = "$plugin_dir/state"; # --- Static Rendering ----- # Where are this blog's static files to be created? $static_dir = "/Library/WebServer/Documents/blog"; # What's my administrative password (you must set this for static rendering)? $static_password = ""; # What flavours should I generate statically? @static_flavours = qw/html rss/; # Should I statically generate individual entries? # 0 = no, 1 = yes $static_entries = 0; # -------------------------------- use vars qw! $version $blog_title $blog_description $blog_language $datadir $url $depth $num_entries $file_extension $default_flavour $static_or_dynamic $plugin_dir $plugin_state_dir @plugins $static_dir $static_password @static_flavours $static_entries $path_info $path_info_yr $path_info_mo $path_info_da $flavour $static_or_dynamic $output !; use strict; use FileHandle; use File::Find; use File::stat; use Time::localtime; use CGI qw/:standard :netscape/; $version = "2.0b4"; my $fh = new FileHandle; my %month2num = (nil=>'00', Jan=>'01', Feb=>'02', Mar=>'03', Apr=>'04', May=>'05', Jun=>'06', Jul=>'07', Aug=>'08', Sep=>'09', Oct=>'10', Nov=>'11', Dec=>'12'); my @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num; # Use the stated preferred URL or figure it out automatically $url ||= url(); $url =~ s/^included:/http:/; # Fix for Server Side Includes (SSI) $url =~ s!/$!!; # Drop ending any / from dir settings $datadir =~ s!/$!!; $plugin_dir =~ s!/$!!; $static_dir =~ s!/$!!; # Fix depth to take into account datadir's path $depth and $depth += ($datadir =~ tr[/][]) - 1; # Global variable to be used in head/foot.{flavour} templates $path_info = ''; $static_or_dynamic = (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) ? 'static' : 'dynamic'; $static_or_dynamic eq 'dynamic' and param(-name=>'-quiet', -value=>1); # Path Info Magic # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day my @path_info = split m{/}, path_info(); shift @path_info; while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; } # Flavour specified by ?flav={flav} or index.{flav} $flavour = ''; if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) { $flavour = $2; $1 ne 'index' and $path_info .= "/$1.$2"; pop @path_info; } else { $flavour = param('flav') || $default_flavour; } $path_info =~ s!^/!!; # Date fiddling ($path_info_yr,$path_info_mo,$path_info_da) = @path_info; my $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef; # Plugins: Start my %plugins = (); if ( $plugin_dir and opendir PLUGINS, $plugin_dir ) { foreach my $plugin ( grep { /^\w+$/ && -f "$plugin_dir/$_" } sort readdir(PLUGINS) ) { my($plugin_name) = $plugin =~ /^\d*(\w+)$/; require "$plugin_dir/$plugin"; $plugin_name->start() and push @plugins, $plugin_name; } closedir PLUGINS; } # Bring in the templates my %template = (); while () { last if /^(__END__)?$/; my($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/; $txt =~ s/\\n/\n/mg; $template{$ct}{$comp} = $txt; } # Define default find subroutine my $entries = sub { my(%files, %indexes); find( sub { my $d; my $curr_depth = $File::Find::dir =~ tr[/][]; return if $depth and $curr_depth > $depth; $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$! and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name) and $files{$File::Find::name} = stat($File::Find::name)->mtime and ( param('-all') or !-f "$static_dir/$1/index." . $static_flavours[0] or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime ) and $indexes{$1} = 1 and $d = join('/', (nice_date($files{$File::Find::name}))[5,2,3]) and $indexes{$d} = $d and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1; }, $datadir ); return (\%files, \%indexes); }; # Plugins: Entries # Allow for the first encountered plugin::entries subroutine to override the # default built-in entries subroutine my $tmp; foreach my $plugin ( @plugins ) { $plugin->can('entries') and defined($tmp = $plugin->entries()) and $entries = $tmp and last; } my($files, $indexes) = &$entries(); my %files = %$files; my %indexes = %$indexes; # Plugins: Filter foreach my $plugin ( @plugins ) { $plugin->can('filter') and $entries = $plugin->filter(\%files) } # Static if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) { param('-quiet') or print "Blosxom is generating static index pages...\n"; # Home Page and Directory Indexes my %done; foreach my $path ( sort keys %indexes) { my $p = ''; foreach ( ('', split /\//, $path) ) { $p .= "/$_"; $p =~ s!^/!!; $done{$p}++ and next; (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755; foreach $flavour ( @static_flavours ) { chomp(my $content_type = (load_template($p,'content_type',$flavour))); my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index"; param('-quiet') or print "$fn.$flavour\n"; my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!"; $output = ''; print $fh_w $indexes{$path} == 1 ? &generate('static', $p, '', $flavour, $content_type) : &generate('static', '', $p, $flavour, $content_type); $fh_w->close; } } } } # Dynamic else { chomp(my $content_type = (load_template($path_info,'content_type',$flavour))); $output = header($content_type); print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type); } # Plugins: End foreach my $plugin ( @plugins ) { $plugin->can('end') and $entries = $plugin->end() } # Generate sub generate { my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_; # Head my $head = (load_template($currentdir,'head',$flavour)); # Plugins: Head foreach my $plugin ( @plugins ) { $plugin->can('head') and $entries = $plugin->head($currentdir, \$head) } $head =~ s/(\$[\w:]+)/$1 . "||''"/gee; $output .= $head; # Stories my $curdate = ''; my $ne = $num_entries; if ( $currentdir =~ /(.+?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) { $currentdir = "$1$2.$file_extension"; my %f; $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} ); %files = %f; } else { $currentdir =~ s!/index\..+$!!; } # Define a default sort subroutine my $sort = sub { my($files_ref) = @_; return sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref; }; # Plugins: Sort # Allow for the first encountered plugin::sort subroutine to override the # default built-in sort subroutine my $tmp; foreach my $plugin ( @plugins ) { $plugin->can('sort') and defined($tmp = $plugin->sort()) and $sort = $tmp and last; } foreach my $path_file ( &$sort(\%files) ) { last if $ne <= 0 && $date !~ /\d/; my($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!; # Only stories in the right hierarchy $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next; # Prepend a slash for use in templates only if a path exists $path &&= "/$path"; # Date fiddling for by-{year,month,day} archive views my ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date($files{"$path_file"}); my ($hr,$min) = split /:/, $ti; # Only stories from the right date my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date; next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr; next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num]; next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da; # Date display if ( $curdate ne "$da/$mo/$yr" ) { my $date = (load_template($path,'date',$flavour)); # Plugins: Date foreach my $plugin ( @plugins ) { $plugin->can('date') and $entries = $plugin->date($currentdir, \$date, $files{$path_file}, $dw,$mo,$mo_num,$da,$ti,$yr) } $date =~ s/(\$[\w:]+)/$1 . "||''"/gee; $curdate = "$da/$mo/$yr"; $output .= $date; } my($title, $body); if (-f "$path_file" && $fh->open("< $path_file")) { chomp($title = <$fh>); chomp($body = join '', <$fh>); $fh->close; } my $story = (load_template($path,'story',$flavour)); # Plugins: Story foreach my $plugin ( @plugins ) { $plugin->can('story') and $entries = $plugin->story($path, $fn, \$story, \$title, \$body) } if ($content_type =~ m{\Wxml$}) { # Escape <, >, and &, and to produce valid RSS my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); my $escape_re = join '|' => keys %escape; $title =~ s/($escape_re)/$escape{$1}/g; $body =~ s/($escape_re)/$escape{$1}/g; } $story =~ s/(\$[\w:]+)/$1 . "||''"/gee; $output .= $story; $fh->close; $ne--; } # Foot my $foot = (load_template($currentdir,'foot',$flavour)); # Plugins: Foot foreach my $plugin ( @plugins ) { $plugin->can('foot') and $entries = $plugin->foot($currentdir, \$foot) } $foot =~ s/(\$[\w:]+)/$1 . "||''"/gee; $output .= $foot; $output; } sub nice_date { my($unixtime) = @_; my $c_time = ctime($unixtime); my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ ); $da = sprintf("%02d", $da); my $mo_num = $month2num{$mo}; return ($dw,$mo,$mo_num,$da,$ti,$yr); } sub load_template { my ($path, $chunk, $flavour) = @_; do { return join '', <$fh> if $fh->open("< $datadir/$path/$chunk.$flavour"); } while ($path =~ s/(\/*[^\/]*)$// and $1); return join '', ($template{$flavour}{$chunk} || $template{error}{$chunk} || ''); } # Default HTML and RSS template bits __DATA__ html content_type text/html html head $blog_title $path_info_da $path_info_mo $path_info_yr
$blog_title
$path_info_da $path_info_mo $path_info_yr

html story

$title
$body

posted at: $ti | path: $path | permanent link to this entry

\n html date

$dw, $da $mo $yr

\n html foot

rss content_type text/xml rss head \n\n\n\n\n \n $blog_title $path_info_da $path_info_mo $path_info_yr\n $url\n $blog_description\n $blog_language\n rss story \n $title\n $url/$yr/$mo_num/$da#$fn\n $body\n \n rss date \n rss foot \n error content_type text/plain error head Error: I'm afraid this is the first I've heard of a "$flavour" flavoured Blosxom. Try dropping the "/+$flavour" bit from the end of the URL.\n\n error date \n __END__