#!/usr/bin/perl -w # Copyright © 2002, 2003, 2004, 2005 Jamie Zawinski # # Permission to use, copy, modify, distribute, and sell this software and its # documentation for any purpose is hereby granted without fee, provided that # the above copyright notice appear in all copies and that both that # copyright notice and this permission notice appear in supporting # documentation. No representations are made about the suitability of this # software for any purpose. It is provided "as is" without express or # implied warranty. # # Splice /calendar/summary.html and /gallery/snapshots.html into /index.html. # Created: 13-Feb-2002. require 5; use diagnostics; use strict; # Utter foulness! Without this, [:upper:] doesn't work on Latin1 characters. use locale; use POSIX qw(locale_h mktime); setlocale(LC_ALL, "en_US"); my $progname = $0; $progname =~ s@.*/@@g; my $version = q{ $Revision: 1.19 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/; my $verbose = 0; my $overview_file = "calendar/overview.html"; my $upcoming_file = "calendar/upcoming.html"; my $snapshots_file = "gallery/snapshots.html"; sub read_component($$) { my ($file, $name) = @_; my $body = ""; local *IN; open (IN, "<$file") || error ("$file: $!"); while () { $body .= $_; } close IN; print STDERR "$progname: read $file\n" if ($verbose > 2); if (! ($body =~ s@^.* \s*(.*?)\s* .*$ @$1@xs)) { error ("$file: no %%${name}_START/END%% markers?"); } $body =~ s/^\n+//s; $body =~ s/\s*$/\n/s; # Correct relative HREFs by inserting "$dir/" at the front. # my $dir = $file; $dir =~ s@/[^/]*$@/@; $dir =~ s@^\./@@; $body =~ s@\b((SRC|HREF)\s*=\s*\")([^\"<>:]+\")@$1$dir$3@g; # Clean up URLs by short-circuiting "foo/../" to "" # $body =~ s@\b((SRC|HREF)\s*=\s*\")([^\"<>/:]+/\.\./)@$1@g; return $body; } sub splice_component($$$$) { my ($component, $into, $into_file, $name) = @_; if (! ($into =~ s/\s* () [ \t]*\n (.*) () [ \t]*\n /\n$1\n$component$3\n/xs)) { error ("$into_file: no %%${name}_START/END%% namewords\n"); } elsif ($verbose > 2) { print STDERR "$progname: $into_file: patched %%$name%%\n"; } return $into; } sub splice_all($) { my ($into_file) = @_; my $ovr = read_component ($overview_file, "OVERVIEW"); my $tic = read_component ($upcoming_file, "UPCOMING"); my $gal = read_component ($snapshots_file, "SNAPSHOTS"); my $into = ""; local *IN; open (IN, "<$into_file") || error ("$into_file: $!"); while () { $into .= $_; } close IN; print STDERR "$progname: read $into_file\n" if ($verbose > 2); my $ointo = $into; $_ = $into; $into = splice_component ($ovr, $into, $into_file, "OVERVIEW"); $into = splice_component ($tic, $into, $into_file, "UPCOMING"); $into = splice_component ($gal, $into, $into_file, "SNAPSHOTS"); if ($into eq $ointo) { print STDERR "$progname: $into_file unchanged\n" if ($verbose > 1); } else { local *OUT; my $file = $into_file; my $file_tmp = "$file.tmp"; open(OUT, ">$file_tmp") || error ("$file_tmp: $!"); print OUT $into || error ("$file_tmp: $!"); close OUT; if (!rename ("$file_tmp", "$file")) { unlink "$file_tmp"; error ("mv $file_tmp $file: $!"); } print STDERR "$progname: wrote $file\n"; } } sub error($) { my ($err) = @_; print STDERR "$progname: $err\n"; exit 1; } sub usage() { print STDERR "usage: $progname [--verbose] into-file\n"; exit 1; } sub main() { my $into; error ("LANG is $ENV{LANG} -- UTF is no good, man!") if ($ENV{LANG} && $ENV{LANG} =~ m/utf/i); while ($_ = $ARGV[0]) { shift @ARGV; if ($_ eq "--verbose") { $verbose++; } elsif (m/^-v+$/) { $verbose += length($_)-1; } elsif (m/^-./) { usage; } elsif (!defined ($into)) { $into = $_; } else { usage; } } usage() unless ($into); splice_all ($into); } main(); exit 0;