aboutsummaryrefslogtreecommitdiff
path: root/gen.pl
blob: 6a0ec7c8656ea9a43bb8b144fa8d0ab6a884ef88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl

# gen.pl -- generate static files for perlisdead.org

use 5.010;

use strict;
use warnings;
use experimental 'switch';

use File::Copy;
use FindBin '$Bin';
use Getopt::Std;

# Parse command-line options

my %opt;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('d', \%opt);

sub main::HELP_MESSAGE {
	print <<HELP;
usage: $0 [-d]
  -d	enable debug messages
HELP
}
sub main::VERSION_MESSAGE {}

# Change directory to script path

chdir $Bin;

# Generate index.html

mkdir 'out' if ! -d 'out';
open my $src, '<', 'src/index.html' or die "Could not open < src/index.html: $!";
open my $out, '>', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!";
for (<$src>) {
	when (/^\.inc (.*)/) {
		open my $h, '<', "inc/$1" or do {
			warn "Could not open inc/$1: $!";
			next;
		};
		print STDERR "INCLUDE: $_\n" if $opt{d};
		print $out $_ while <$h>;
		close $h;
	}
	when (/^\.eval (.*)/) {
		print STDERR "EVAL: $1\n" if $opt{d};
		{
			no strict; no warnings; no experimental;
			select $out;
			eval $1;
			select STDOUT;
			warn $@ if $@;
		}
	}
	when (/^\.run (.*)/) {
		open my $h, '<', "run/$1" or do {
			warn "Could not open run/$1: $!";
			next;
		};
		print STDERR "RUN: $1\n" if $opt{d};
		my $c = do { local $/; <$h> };
		close $h;
		{
			no strict; no warnings; no experimental;
			local $0 = "run/$1";
			select $out;
			eval $c;
			select STDOUT;
			warn $@ if $@;
		}
		# I guess you *could* just run the script in a separate process...
	}
	default {
		print $out $_;
	}
}
close $out;
close $src;

# Rename temporary file

move 'out/index.html.tmp' => 'out/index.html';

# Copy modified static files

opendir $src, 'src' or die "Could not open directory src: $!";
opendir $out, 'out' or die "Could not open directory src: $!";
my %src = map { ($_, (stat "src/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $src;
my %out = map { ($_, (stat "out/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $out;
for (keys %src) {
	no warnings 'uninitialized';
	if ($src{$_} > $out{$_}) {
		print STDERR "COPY: src/$_ => out/$_\n" if $opt{d};
		copy "src/$_" => "out/$_";
	}
}
closedir $out;
closedir $src;

print STDERR "DONE\n" if $opt{d};