4 # Author: Rael Dornfest <rael@oreilly.com>
6 # Home/Docs/Licensing: http://blosxom.sourceforge.net/
7 # Development/Downloads: http://sourceforge.net/projects/blosxom
11 # --- Configurable variables -----
13 # What's this blog's title?
14 $blog_title = "My Weblog";
16 # What's this blog's description (for outgoing RSS feed)?
17 $blog_description = "Yet another Blosxom weblog.";
19 # What's this blog's primary language (for outgoing RSS feed)?
20 $blog_language = "en";
22 # Where are this blog's entries kept?
23 $datadir = "/Library/WebServer/Documents/blosxom";
25 # What's my preferred base URL for this blog (leave blank for automatic)?
28 # Should I stick only to the datadir for items or travel down the
29 # directory hierarchy looking for items? If so, to what depth?
30 # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down
33 # How many entries should I show on the home page?
36 # What file extension signifies a blosxom entry?
37 $file_extension = "txt";
39 # What is the default flavour?
40 $default_flavour = "html";
42 # Should I show entries from the future (i.e. dated after now)?
43 $show_future_entries = 0;
45 # --- Plugins (Optional) -----
47 # Where are my plugins kept?
50 # Where should my modules keep their state information?
51 $plugin_state_dir = "$plugin_dir/state";
53 # --- Static Rendering -----
55 # Where are this blog's static files to be created?
56 $static_dir = "/Library/WebServer/Documents/blog";
58 # What's my administrative password (you must set this for static rendering)?
59 $static_password = "";
61 # What flavours should I generate statically?
62 @static_flavours = qw/html rss/;
64 # Should I statically generate individual entries?
68 # --------------------------------
70 use vars qw! $version $blog_title $blog_description $blog_language $datadir $url %template $template $depth $num_entries $file_extension $default_flavour $static_or_dynamic $plugin_dir $plugin_state_dir @plugins %plugins $static_dir $static_password @static_flavours $static_entries $path_info $path_info_yr $path_info_mo $path_info_da $path_info_mo_num $flavour $static_or_dynamic %month2num @num2month $interpolate $entries $output $header $show_future_entries %files %indexes %others !;
77 use CGI qw/:standard :netscape/;
81 my $fh = new FileHandle;
83 %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');
84 @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
86 # Use the stated preferred URL or figure it out automatically
87 $url ||= url(-path_info => 1);
88 $url =~ s/^included:/http:/ if $ENV{SERVER_PROTOCOL} eq 'INCLUDED';
90 # NOTE: Since v3.12, it looks as if CGI.pm misbehaves for SSIs and
91 # always appends path_info to the url. To fix this, we always
92 # request an url with path_info, and always remove it from the end of the
94 my $pi_len = length $ENV{PATH_INFO};
95 my $might_be_pi = substr($url, -$pi_len);
96 substr($url, -length $ENV{PATH_INFO}) = '' if $might_be_pi eq $ENV{PATH_INFO};
100 # Drop ending any / from dir settings
101 $datadir =~ s!/$!!; $plugin_dir =~ s!/$!!; $static_dir =~ s!/$!!;
103 # Fix depth to take into account datadir's path
104 $depth and $depth += ($datadir =~ tr[/][]) - 1;
106 # Global variable to be used in head/foot.{flavour} templates
109 $static_or_dynamic = (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) ? 'static' : 'dynamic';
110 $static_or_dynamic eq 'dynamic' and param(-name=>'-quiet', -value=>1);
113 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
114 my @path_info = split m{/}, path_info() || param('path');
117 while ($path_info[0] and $path_info[0] =~ /^[a-zA-Z].*$/ and $path_info[0] !~ /(.*)\.(.*)/) { $path_info .= '/' . shift @path_info; }
119 # Flavour specified by ?flav={flav} or index.{flav}
122 if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
124 $1 ne 'index' and $path_info .= "/$1.$2";
127 $flavour = param('flav') || $default_flavour;
130 # Strip spurious slashes
131 $path_info =~ s!(^/*)|(/*$)!!g;
134 ($path_info_yr,$path_info_mo,$path_info_da) = @path_info;
135 $path_info_mo_num = $path_info_mo ? ( $path_info_mo =~ /\d{2}/ ? $path_info_mo : ($month2num{ucfirst(lc $path_info_mo)} || undef) ) : undef;
137 # Define standard template subroutine, plugin-overridable at Plugins: Template
140 my ($path, $chunk, $flavour) = @_;
143 return join '', <$fh> if $fh->open("< $datadir/$path/$chunk.$flavour");
144 } while ($path =~ s/(\/*[^\/]*)$// and $1);
146 # Check for definedness, since flavour can be the empty string
147 if (defined $template{$flavour}{$chunk}) {
148 return $template{$flavour}{$chunk};
149 } elsif (defined $template{error}{$chunk}) {
150 return $template{error}{$chunk}
155 # Bring in the templates
158 last if /^(__END__)$/;
159 my($ct, $comp, $txt) = /^(\S+)\s(\S+)(?:\s(.*))?$/ or next;
161 $template{$ct}{$comp} .= $txt . "\n";
165 if ( $plugin_dir and opendir PLUGINS, $plugin_dir ) {
166 unshift @INC, $plugin_dir;
167 foreach my $plugin ( grep { /^[\w:]+$/ && -f "$plugin_dir/$_" } sort readdir(PLUGINS) ) {
168 next if ($plugin =~ /~$/); # Ignore emacs backups
169 my($plugin_name, $off) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
170 my $on_off = $off eq '_' ? -1 : 1;
171 # Allow perl module plugins
172 if ($plugin =~ m/::/ && -z "$plugin_dir/$plugin") {
173 # For Blosxom::Plugin::Foo style plugins, we need to use a string require
174 eval "require $plugin_name";
177 eval { require $plugin };
179 $@ and warn "error finding or loading blosxom plugin $plugin_name - skipping\n" and next;
180 $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) and push @plugins, $plugin_name;
187 # Allow for the first encountered plugin::template subroutine to override the
188 # default built-in template subroutine
189 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('template') and defined($tmp = $plugin->template()) and $template = $tmp and last; }
191 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
193 return &$template(@_);
196 # Define default entries subroutine
199 my(%files, %indexes, %others);
203 my $curr_depth = $File::Find::dir =~ tr[/][];
204 return if $depth and $curr_depth > $depth;
208 $File::Find::name =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
209 # not an index, .file, and is readable
210 and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name)
213 # to show or not to show future entries
216 or stat($File::Find::name)->mtime < time
219 # add the file and its associated mtime to the list of files
220 and $files{$File::Find::name} = stat($File::Find::name)->mtime
222 # static rendering bits
225 or !-f "$static_dir/$1/index." . $static_flavours[0]
226 or stat("$static_dir/$1/index." . $static_flavours[0])->mtime < stat($File::Find::name)->mtime
229 and $d = join('/', (nice_date($files{$File::Find::name}))[5,2,3])
231 and $indexes{$d} = $d
232 and $static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$file_extension" } = 1
236 !-d $File::Find::name and -r $File::Find::name and $others{$File::Find::name} = stat($File::Find::name)->mtime
241 return (\%files, \%indexes, \%others);
245 # Allow for the first encountered plugin::entries subroutine to override the
246 # default built-in entries subroutine
247 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('entries') and defined($tmp = $plugin->entries()) and $entries = $tmp and last; }
249 my ($files, $indexes, $others) = &$entries();
250 %indexes = %$indexes;
253 if (!$ENV{GATEWAY_INTERFACE} and param('-password') and $static_password and param('-password') eq $static_password) {
255 param('-quiet') or print "Blosxom is generating static index pages...\n";
257 # Home Page and Directory Indexes
259 foreach my $path ( sort keys %indexes) {
261 foreach ( ('', split /\//, $path) ) {
264 $done{$p}++ and next;
265 (-d "$static_dir/$p" or $p =~ /\.$file_extension$/) or mkdir "$static_dir/$p", 0755;
266 foreach $flavour ( @static_flavours ) {
267 my $content_type = (&$template($p,'content_type',$flavour));
268 $content_type =~ s!\n.*!!s;
269 my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
270 param('-quiet') or print "$fn.$flavour\n";
271 my $fh_w = new FileHandle "> $static_dir/$fn.$flavour" or die "Couldn't open $static_dir/$p for writing: $!";
273 if ($indexes{$path} == 1) {
277 $path_info =~ s!\.$file_extension$!\.$flavour!;
278 print $fh_w &generate('static', $path_info, '', $flavour, $content_type);
281 local ($path_info_yr,$path_info_mo,$path_info_da, $path_info) =
283 unless (defined $path_info) {$path_info = ""};
284 print $fh_w &generate('static', '', $p, $flavour, $content_type);
294 my $content_type = (&$template($path_info,'content_type',$flavour));
295 $content_type =~ s!\n.*!!s;
297 $header = {-type=>$content_type};
299 print generate('dynamic', $path_info, "$path_info_yr/$path_info_mo_num/$path_info_da", $flavour, $content_type);
303 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('end') and $entries = $plugin->end() }
307 my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_;
309 %files = %$files; %others = ref $others ? %$others : ();
312 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('filter') and $entries = $plugin->filter(\%files, \%others) }
317 # Allow plugins to decide if we can cut short story generation
318 my $skip; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('skip') and defined($tmp = $plugin->skip()) and $skip = $tmp and last; }
320 # Define default interpolation subroutine
324 my $template = shift;
326 s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
330 unless (defined($skip) and $skip) {
332 # Plugins: Interpolate
333 # Allow for the first encountered plugin::interpolate subroutine to
334 # override the default built-in interpolate subroutine
335 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('interpolate') and defined($tmp = $plugin->interpolate()) and $interpolate = $tmp and last; }
338 my $head = (&$template($currentdir,'head',$flavour));
341 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('head') and $entries = $plugin->head($currentdir, \$head) }
343 $head = &$interpolate($head);
349 my $ne = $num_entries;
351 if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
352 $currentdir = "$1$2.$file_extension";
353 $files{"$datadir/$1$2.$file_extension"} and %f = ( "$datadir/$1$2.$file_extension" => $files{"$datadir/$1$2.$file_extension"} );
356 $currentdir =~ s!/index\..+$!!;
359 # Define a default sort subroutine
362 return sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref;
366 # Allow for the first encountered plugin::sort subroutine to override the
367 # default built-in sort subroutine
368 my $tmp; foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('sort') and defined($tmp = $plugin->sort()) and $sort = $tmp and last; }
370 foreach my $path_file ( &$sort(\%f, \%others) ) {
371 last if $ne <= 0 && $date !~ /\d/;
372 use vars qw/ $path $fn /;
373 ($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
375 # Only stories in the right hierarchy
376 $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next;
378 # Prepend a slash for use in templates only if a path exists
381 # Date fiddling for by-{year,month,day} archive views
382 use vars qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm /;
383 ($dw,$mo,$mo_num,$da,$ti,$yr) = nice_date($files{"$path_file"});
384 ($hr,$min) = split /:/, $ti;
385 ($hr12, $ampm) = $hr >= 12 ? ($hr - 12,'pm') : ($hr, 'am');
386 $hr12 =~ s/^0//; $hr12 == 0 and $hr12 = 12;
388 # Only stories from the right date
389 my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date;
390 next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr;
391 next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
392 next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da;
395 my $date = (&$template($path,'date',$flavour));
398 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('date') and $entries = $plugin->date($currentdir, \$date, $files{$path_file}, $dw,$mo,$mo_num,$da,$ti,$yr) }
400 $date = &$interpolate($date);
402 $curdate ne $date and $curdate = $date and $output .= $date;
404 use vars qw/ $title $body $raw /;
405 if (-f "$path_file" && $fh->open("< $path_file")) {
406 chomp($title = <$fh>);
407 chomp($body = join '', <$fh>);
409 $raw = "$title\n$body";
411 my $story = (&$template($path,'story',$flavour));
414 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('story') and $entries = $plugin->story($path, $fn, \$story, \$title, \$body) }
416 if ($content_type =~ m{\bxml\b}) {
417 # Escape <, >, and &, and to produce valid RSS
418 my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
419 my $escape_re = join '|' => keys %escape;
420 $title =~ s/($escape_re)/$escape{$1}/g;
421 $body =~ s/($escape_re)/$escape{$1}/g;
424 $story = &$interpolate($story);
433 my $foot = (&$template($currentdir,'foot',$flavour));
436 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('foot') and $entries = $plugin->foot($currentdir, \$foot) }
438 $foot = &$interpolate($foot);
442 foreach my $plugin ( @plugins ) { $plugins{$plugin} > 0 and $plugin->can('last') and $entries = $plugin->last() }
446 # Finally, add the header, if any and running dynamically
447 $static_or_dynamic eq 'dynamic' and $header and $output = header($header) . $output;
456 my $c_time = ctime($unixtime);
457 my($dw,$mo,$da,$ti,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}:\d{2}):\d{2} +(\d{4})$/ );
458 $da = sprintf("%02d", $da);
459 my $mo_num = $month2num{$mo};
461 return ($dw,$mo,$mo_num,$da,$ti,$yr);
465 # Default HTML and RSS template bits
467 html content_type text/html
471 html head <link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/index.rss" />
472 html head <title>$blog_title $path_info_da $path_info_mo $path_info_yr
477 html head <font size="+3">$blog_title</font><br />
478 html head $path_info_da $path_info_mo $path_info_yr
483 html story <a name="$fn"><b>$title</b></a><br />
484 html story $body<br />
486 html story posted at: $ti | path: <a href="$url$path">$path </a> | <a href="$url/$yr/$mo_num/$da#$fn">permanent link to this entry</a>
489 html date <h3>$dw, $da $mo $yr</h3>
494 html foot <a href="http://blosxom.sourceforge.net/"><img src="http://blosxom.sourceforge.net/images/pb_blosxom.gif" border="0" /></a>
499 rss content_type text/xml
501 rss head <?xml version="1.0"?>
502 rss head <!-- name="generator" content="blosxom/$version" -->
503 rss head <!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
505 rss head <rss version="0.91">
507 rss head <title>$blog_title $path_info_da $path_info_mo $path_info_yr</title>
508 rss head <link>$url</link>
509 rss head <description>$blog_description</description>
510 rss head <language>$blog_language</language>
513 rss story <title>$title</title>
514 rss story <link>$url/$yr/$mo_num/$da#$fn</link>
515 rss story <description>$body</description>
523 error content_type text/html
527 error head <p><font color="red">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.</font>
530 error story <p><b>$title</b><br />
531 error story $body <a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
533 error date <h3>$dw, $da $mo $yr</h3>