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 # What's this blog's text encoding ?
23 $blog_encoding = "UTF-8";
25 # Where are this blog's entries kept?
26 $datadir = "/Library/WebServer/Documents/blosxom";
28 # What's my preferred base URL for this blog (leave blank for automatic)?
31 # Should I stick only to the datadir for items or travel down the
32 # directory hierarchy looking for items? If so, to what depth?
33 # 0 = infinite depth (aka grab everything), 1 = datadir only, n = n levels down
36 # How many entries should I show on the home page?
39 # What file extension signifies a blosxom entry?
40 $file_extension = "txt";
42 # What is the default flavour?
43 $default_flavour = "html";
45 # Should I show entries from the future (i.e. dated after now)?
46 $show_future_entries = 0;
48 # --- Plugins (Optional) -----
50 # File listing plugins blosxom should load
51 # (if empty blosxom will load all plugins in $plugin_path directories)
54 # Where are my plugins kept?
55 # List of directories, separated by ';' on windows, ':' everywhere else
58 # Where should my plugins keep their state information?
59 $plugin_state_dir = "";
61 #$plugin_state_dir = "/var/lib/blosxom/state";
63 # --- Static Rendering -----
65 # Where are this blog's static files to be created?
66 $static_dir = "/Library/WebServer/Documents/blog";
68 # What's my administrative password (you must set this for static rendering)?
69 $static_password = "";
71 # What flavours should I generate statically?
72 @static_flavours = qw/html rss/;
74 # Should I statically generate individual entries?
78 # --------------------------------
81 qw! $version $blog_title $blog_description $blog_language $blog_encoding $datadir $url %template $template $depth $num_entries $file_extension $default_flavour $static_or_dynamic $config_dir $plugin_list $plugin_path $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 !;
89 use CGI qw/:standard :netscape/;
93 # Load configuration from $ENV{BLOSXOM_CONFIG_DIR}/blosxom.conf, if it exists
95 if ( $ENV{BLOSXOM_CONFIG_FILE} && -r $ENV{BLOSXOM_CONFIG_FILE} ) {
96 $blosxom_config = $ENV{BLOSXOM_CONFIG_FILE};
97 ( $config_dir = $blosxom_config ) =~ s! / [^/]* $ !!x;
100 for my $blosxom_config_dir ( $ENV{BLOSXOM_CONFIG_DIR}, '/etc/blosxom',
103 if ( -r "$blosxom_config_dir/blosxom.conf" ) {
104 $config_dir = $blosxom_config_dir;
105 $blosxom_config = "$blosxom_config_dir/blosxom.conf";
111 # Load $blosxom_config
112 if ($blosxom_config) {
113 if ( -r $blosxom_config ) {
114 eval { require $blosxom_config }
115 or warn "Error reading blosxom config file '$blosxom_config'"
116 . ( $@ ? ": $@" : '' );
119 warn "Cannot find or read blosxom config file '$blosxom_config'";
123 my $fh = new FileHandle;
140 @num2month = sort { $month2num{$a} <=> $month2num{$b} } keys %month2num;
142 # Use the stated preferred URL or figure it out automatically
143 $url ||= url( -path_info => 1 );
144 $url =~ s/^included:/http:/ if $ENV{SERVER_PROTOCOL} eq 'INCLUDED';
146 # NOTE: Since v3.12, it looks as if CGI.pm misbehaves for SSIs and
147 # always appends path_info to the url. To fix this, we always
148 # request an url with path_info, and always remove it from the end of the
150 my $pi_len = length $ENV{PATH_INFO};
151 my $might_be_pi = substr( $url, -$pi_len );
152 substr( $url, -length $ENV{PATH_INFO} ) = ''
153 if $might_be_pi eq $ENV{PATH_INFO};
157 # Drop ending any / from dir settings
159 $plugin_dir =~ s!/$!!;
160 $static_dir =~ s!/$!!;
162 # Fix depth to take into account datadir's path
163 $depth += ( $datadir =~ tr[/][] ) - 1 if $depth;
165 # Global variable to be used in head/foot.{flavour} templates
168 if ( !$ENV{GATEWAY_INTERFACE}
169 and param('-password')
171 and param('-password') eq $static_password )
173 $static_or_dynamic = 'static';
176 $static_or_dynamic = 'dynamic';
177 param( -name => '-quiet', -value => 1 );
181 # Take a gander at HTTP's PATH_INFO for optional blog name, archive yr/mo/day
182 my @path_info = split m{/}, path_info() || param('path');
185 while ( $path_info[0]
186 and $path_info[0] =~ /^[a-zA-Z].*$/
187 and $path_info[0] !~ /(.*)\.(.*)/ )
189 $path_info .= '/' . shift @path_info;
192 # Flavour specified by ?flav={flav} or index.{flav}
195 if ( $path_info[$#path_info] =~ /(.+)\.(.+)$/ ) {
197 $path_info .= "/$1.$2" if $1 ne 'index';
201 $flavour = param('flav') || $default_flavour;
204 # Strip spurious slashes
205 $path_info =~ s!(^/*)|(/*$)!!g;
208 ( $path_info_yr, $path_info_mo, $path_info_da ) = @path_info;
211 ? ( $path_info_mo =~ /\d{2}/
213 : ( $month2num{ ucfirst( lc $path_info_mo ) } || undef ) )
216 # Define standard template subroutine, plugin-overridable at Plugins: Template
218 my ( $path, $chunk, $flavour ) = @_;
221 return join '', <$fh>
222 if $fh->open("< $datadir/$path/$chunk.$flavour");
223 } while ( $path =~ s/(\/*[^\/]*)$// and $1 );
225 # Check for definedness, since flavour can be the empty string
226 if ( defined $template{$flavour}{$chunk} ) {
227 return $template{$flavour}{$chunk};
229 elsif ( defined $template{error}{$chunk} ) {
230 return $template{error}{$chunk};
237 # Bring in the templates
240 last if /^(__END__)$/;
241 my ( $ct, $comp, $txt ) = /^(\S+)\s(\S+)(?:\s(.*))?$/ or next;
243 $template{$ct}{$comp} .= $txt . "\n";
247 my $path_sep = $^O eq 'MSWin32' ? ';' : ':';
248 my @plugin_dirs = split /$path_sep/, ( $plugin_path || $plugin_dir );
249 my @plugin_list = ();
250 my %plugin_hash = ();
252 # If $plugin_list is set, read plugins to use from that file
253 $plugin_list = "$config_dir/$plugin_list"
254 if $plugin_list && $plugin_list !~ m!^\s*/!;
255 if ( $plugin_list and -r $plugin_list and $fh->open("< $plugin_list") ) {
256 @plugin_list = map { chomp $_; $_ } grep { /\S/ && !/^#/ } <$fh>;
260 # Otherwise walk @plugin_dirs to get list of plugins to use
261 elsif (@plugin_dirs) {
262 for my $plugin_dir (@plugin_dirs) {
263 next unless -d $plugin_dir;
264 if ( opendir PLUGINS, $plugin_dir ) {
266 grep { /^[\w:]+$/ && !/~$/ && -f "$plugin_dir/$_" }
271 next if $plugin_hash{$plugin};
273 # Add to @plugin_list and %plugin_hash
274 $plugin_hash{$plugin} = "$plugin_dir/$plugin";
275 push @plugin_list, $plugin;
280 @plugin_list = sort @plugin_list;
283 # Load all plugins in @plugin_list
284 unshift @INC, @plugin_dirs;
285 foreach my $plugin (@plugin_list) {
286 my ( $plugin_name, $off ) = $plugin =~ /^\d*([\w:]+?)(_?)$/;
287 my $on_off = $off eq '_' ? -1 : 1;
289 # Allow perl module plugins
290 if ( $plugin =~ m/::/ && -z $plugin_hash{$plugin} ) {
292 # For Blosxom::Plugin::Foo style plugins, we need to use a string require
293 eval "require $plugin_name";
296 eval { require $plugin };
300 warn "error finding or loading blosxom plugin '$plugin_name': $@";
303 if ( $plugin_name->start() and ( $plugins{$plugin_name} = $on_off ) ) {
304 push @plugins, $plugin_name;
308 shift @INC foreach @plugin_dirs;
311 # Allow for the first encountered plugin::template subroutine to override the
312 # default built-in template subroutine
313 foreach my $plugin (@plugins) {
314 if ( $plugins{$plugin} > 0 and $plugin->can('template') ) {
315 if ( my $tmp = $plugin->template() ) {
322 # Provide backward compatibility for Blosxom < 2.0rc1 plug-ins
324 return &$template(@_);
327 # Define default entries subroutine
329 my ( %files, %indexes, %others );
333 my $curr_depth = $File::Find::dir =~ tr[/][];
334 return if $depth and $curr_depth > $depth;
340 =~ m!^$datadir/(?:(.*)/)?(.+)\.$file_extension$!
342 # not an index, .file, and is readable
343 and $2 ne 'index' and $2 !~ /^\./ and ( -r $File::Find::name )
347 # read modification time
348 my $mtime = stat($File::Find::name)->mtime or return;
350 # to show or not to show future entries
351 return unless ( $show_future_entries or $mtime < time );
353 # add the file and its associated mtime to the list of files
354 $files{$File::Find::name} = $mtime;
356 # static rendering bits
358 = "$static_dir/$1/index." . $static_flavours[0];
361 or stat($static_file)->mtime < $mtime )
364 $d = join( '/', ( nice_date($mtime) )[ 5, 2, 3 ] );
366 $indexes{ ( $1 ? "$1/" : '' ) . "$2.$file_extension" } = 1
371 # not an entries match
372 elsif ( !-d $File::Find::name and -r $File::Find::name ) {
373 $others{$File::Find::name} = stat($File::Find::name)->mtime;
379 return ( \%files, \%indexes, \%others );
383 # Allow for the first encountered plugin::entries subroutine to override the
384 # default built-in entries subroutine
385 foreach my $plugin (@plugins) {
386 if ( $plugins{$plugin} > 0 and $plugin->can('entries') ) {
387 if ( my $tmp = $plugin->entries() ) {
394 my ( $files, $indexes, $others ) = &$entries();
395 %indexes = %$indexes;
398 if ( !$ENV{GATEWAY_INTERFACE}
399 and param('-password')
401 and param('-password') eq $static_password )
404 param('-quiet') or print "Blosxom is generating static index pages...\n";
406 # Home Page and Directory Indexes
408 foreach my $path ( sort keys %indexes ) {
410 foreach ( ( '', split /\//, $path ) ) {
414 mkdir "$static_dir/$p", 0755
415 unless ( -d "$static_dir/$p" or $p =~ /\.$file_extension$/ );
416 foreach $flavour (@static_flavours) {
418 = ( &$template( $p, 'content_type', $flavour ) );
419 $content_type =~ s!\n.*!!s;
420 my $fn = $p =~ m!^(.+)\.$file_extension$! ? $1 : "$p/index";
421 param('-quiet') or print "$fn.$flavour\n";
422 my $fh_w = new FileHandle "> $static_dir/$fn.$flavour"
423 or die "Couldn't open $static_dir/$p for writing: $!";
425 if ( $indexes{$path} == 1 ) {
431 $path_info =~ s!\.$file_extension$!\.$flavour!;
432 print $fh_w &generate( 'static', $path_info, '', $flavour,
439 $path_info_yr, $path_info_mo,
440 $path_info_da, $path_info
441 ) = split /\//, $p, 4;
442 unless ( defined $path_info ) { $path_info = "" }
443 print $fh_w &generate( 'static', '', $p, $flavour,
454 my $content_type = ( &$template( $path_info, 'content_type', $flavour ) );
455 $content_type =~ s!\n.*!!s;
457 $content_type =~ s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
458 $header = { -type => $content_type };
460 print generate( 'dynamic', $path_info,
461 "$path_info_yr/$path_info_mo_num/$path_info_da",
462 $flavour, $content_type );
466 foreach my $plugin (@plugins) {
467 if ( $plugins{$plugin} > 0 and $plugin->can('end') ) {
468 $entries = $plugin->end();
474 my($static_or_dynamic, $currentdir, $date, $flavour, $content_type) = @_;
476 %files = %$files; %others = ref $others ? %$others : ();
479 foreach my $plugin ( @plugins ) {
480 if ($plugins{$plugin} > 0 and $plugin->can('filter')){ $entries = $plugin->filter(\%files, \%others); }
486 # Allow plugins to decide if we can cut short story generation
488 foreach my $plugin (@plugins) {
489 if ( $plugins{$plugin} > 0 and $plugin->can('skip') ) {
490 if ( my $tmp = $plugin->skip() ) {
498 # Define default interpolation subroutine
502 my $template = shift;
504 s/(\$\w+(?:::)?\w*)/"defined $1 ? $1 : ''"/gee;
508 unless (defined($skip) and $skip) {
510 # Plugins: Interpolate
511 # Allow for the first encountered plugin::interpolate subroutine to
512 # override the default built-in interpolate subroutine
513 foreach my $plugin (@plugins) {
514 if ( $plugins{$plugin} > 0 and $plugin->can('interpolate') ) {
515 if ( my $tmp = $plugin->interpolate() ) {
523 my $head = (&$template($currentdir,'head',$flavour));
526 foreach my $plugin (@plugins) {
527 if ( $plugins{$plugin} > 0 and $plugin->can('head') ) {
528 $entries = $plugin->head( $currentdir, \$head );
532 $head = &$interpolate($head);
538 my $ne = $num_entries;
540 if ( $currentdir =~ /(.*?)([^\/]+)\.(.+)$/ and $2 ne 'index' ) {
541 $currentdir = "$1$2.$file_extension";
542 %f = ( "$datadir/$currentdir" => $files{"$datadir/$currentdir"} ) if $files{"$datadir/$currentdir"};
545 $currentdir =~ s!/index\..+$!!;
548 # Define a default sort subroutine
551 return sort { $files_ref->{$b} <=> $files_ref->{$a} } keys %$files_ref;
555 # Allow for the first encountered plugin::sort subroutine to override the
556 # default built-in sort subroutine
557 foreach my $plugin (@plugins) {
558 if ( $plugins{$plugin} > 0 and $plugin->can('sort') ) {
559 if ( my $tmp = $plugin->sort() ) {
566 foreach my $path_file ( &$sort(\%f, \%others) ) {
567 last if $ne <= 0 && $date !~ /\d/;
568 use vars qw/ $path $fn /;
569 ($path,$fn) = $path_file =~ m!^$datadir/(?:(.*)/)?(.*)\.$file_extension!;
571 # Only stories in the right hierarchy
572 $path =~ /^$currentdir/ or $path_file eq "$datadir/$currentdir" or next;
574 # Prepend a slash for use in templates only if a path exists
577 # Date fiddling for by-{year,month,day} archive views
578 use vars qw/ $dw $mo $mo_num $da $ti $yr $hr $min $hr12 $ampm $utc_offset/;
579 ($dw,$mo,$mo_num,$da,$ti,$yr,$utc_offset) = nice_date($files{"$path_file"});
580 ($hr,$min) = split /:/, $ti;
581 ($hr12, $ampm) = $hr >= 12 ? ($hr - 12,'pm') : ($hr, 'am');
582 $hr12 =~ s/^0//; if ($hr12 == 0) {$hr12 = 12};
584 # Only stories from the right date
585 my($path_info_yr,$path_info_mo_num, $path_info_da) = split /\//, $date;
586 next if $path_info_yr && $yr != $path_info_yr; last if $path_info_yr && $yr < $path_info_yr;
587 next if $path_info_mo_num && $mo ne $num2month[$path_info_mo_num];
588 next if $path_info_da && $da != $path_info_da; last if $path_info_da && $da < $path_info_da;
591 my $date = (&$template($path,'date',$flavour));
594 foreach my $plugin (@plugins) {
595 if ( $plugins{$plugin} > 0 and $plugin->can('date') ) {
597 = $plugin->date( $currentdir, \$date, $files{$path_file}, $dw,
598 $mo, $mo_num, $da, $ti, $yr );
602 $date = &$interpolate($date);
604 if ( $date && $curdate ne $date ) {
609 use vars qw/ $title $body $raw /;
610 if (-f "$path_file" && $fh->open("< $path_file")) {
611 chomp($title = <$fh>);
612 chomp($body = join '', <$fh>);
614 $raw = "$title\n$body";
616 my $story = (&$template($path,'story',$flavour));
619 foreach my $plugin (@plugins) {
620 if ( $plugins{$plugin} > 0 and $plugin->can('story') ) {
621 $entries = $plugin->story( $path, $fn, \$story, \$title, \$body );
625 if ($content_type =~ m{\bxml\b}) {
626 # Escape <, >, and &, and to produce valid RSS
627 my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
628 my $escape_re = join '|' => keys %escape;
629 $title =~ s/($escape_re)/$escape{$1}/g;
630 $body =~ s/($escape_re)/$escape{$1}/g;
633 $story = &$interpolate($story);
642 my $foot = (&$template($currentdir,'foot',$flavour));
645 foreach my $plugin (@plugins) {
646 if ( $plugins{$plugin} > 0 and $plugin->can('foot') ) {
647 $entries = $plugin->foot( $currentdir, \$foot );
651 $foot = &$interpolate($foot);
655 foreach my $plugin (@plugins) {
656 if ( $plugins{$plugin} > 0 and $plugin->can('last') ) {
657 $entries = $plugin->last();
663 # Finally, add the header, if any and running dynamically
664 $output = header($header) . $output if ($static_or_dynamic eq 'dynamic' and $header);
673 my $c_time = ctime($unixtime);
674 my($dw,$mo,$da,$hr,$min,$yr) = ( $c_time =~ /(\w{3}) +(\w{3}) +(\d{1,2}) +(\d{2}):(\d{2}):\d{2} +(\d{4})$/ );
676 $da = sprintf("%02d", $da);
677 my $mo_num = $month2num{$mo};
679 my $offset = timegm(00, $min, $hr, $da, $mo_num-1, $yr-1900)-$unixtime;
680 my $utc_offset = sprintf("%+03d", int($offset / 3600)).sprintf("%02d", ($offset % 3600)/60) ;
682 return ($dw,$mo,$mo_num,$da,$ti,$yr,$utc_offset);
686 # Default HTML and RSS template bits
688 html content_type text/html; charset=$blog_encoding
692 html head <meta http-equiv="content-type" content="text/html;charset=$blog_encoding" />
693 html head <link rel="alternate" type="type="application/rss+xml" title="RSS" href="$url/index.rss" />
694 html head <title>$blog_title $path_info_da $path_info_mo $path_info_yr
699 html head <font size="+3">$blog_title</font><br />
700 html head $path_info_da $path_info_mo $path_info_yr
705 html story <a name="$fn"><b>$title</b></a><br />
706 html story $body<br />
708 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>
711 html date <h3>$dw, $da $mo $yr</h3>
716 html foot <a href="http://blosxom.sourceforge.net/"><img src="http://blosxom.sourceforge.net/images/pb_blosxom.gif" border="0" /></a>
721 rss content_type text/xml; charset=$blog_encoding
723 rss head <?xml version="1.0" encoding="$blog_encoding"?>
724 rss head <rss version="2.0">
726 rss head <title>$blog_title</title>
727 rss head <link>$url/$path_info</link>
728 rss head <description>$blog_description</description>
729 rss head <language>$blog_language</language>
730 rss head <docs>http://blogs.law.harvard.edu/tech/rss</docs>
731 rss head <generator>blosxom/$version</generator>
734 rss story <title>$title</title>
735 rss story <pubDate>$dw, $da $mo $yr $ti:00 $utc_offset</pubDate>
736 rss story <link>$url/$yr/$mo_num/$da#$fn</link>
737 rss story <category>$path</category>
738 rss story <guid isPermaLink="false">$path/$fn</guid>
739 rss story <description>$body</description>
747 error content_type text/html
751 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></p>
754 error story <p><b>$title</b><br />
755 error story $body <a href="$url/$yr/$mo_num/$da#fn.$default_flavour">#</a></p>
757 error date <h3>$dw, $da $mo $yr</h3>