aboutsummaryrefslogtreecommitdiff
path: root/gen.pl
blob: 0b682c291c74b64778053fa1dd991684c3b80a90 (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
#!/usr/bin/perl

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

use strict;
use warnings;
use experimental 'switch';
use File::Copy;
use FindBin '$Bin';

# Change directory to script path
chdir $Bin;

# Generate index.html
mkdir 'out' if ! -d 'out';
open my $f, '<', 'src/index.html' or die "Could not open < src/index.html: $!";
open my $g, '>', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!";
for (<$f>) {
	when (/^\.inc (.*)/) {
		open my $h, '<', "inc/$1" or do {
			warn "Could not open inc/$1: $!";
			next;
		};
		print $g $_ while <$h>;
		close $h;
	}
	when (/^\.eval (.*)/) {
		no strict; no warnings; no experimental;
		local *STDOUT = $g;
		eval $1;
		warn $@ if $@;
	}
	when (/^\.run (.*)/) {
		open my $h, '<', "run/$1" or do {
			warn "Could not open run/$1: $!";
			next;
		};
		my $c = do { local $/; <$h> };
		close $h;
		local *STDOUT = $g;
		local $0 = "run/$1";
		no strict; no warnings; no experimental;
		eval $c;
		warn $@ if $@;
		# I guess you could just shell out instead...
	}
	default {
		print $g $_;
	}
}
close $g;
close $f;

# Copy static files
opendir my $dh, 'src' or die "Could not open directory src: $!";
while (readdir $dh) {
	copy "src/$_" => "out/$_" unless /^\./ or /^index\.html$/;
}
closedir $dh;

# Rename temporary file
move 'out/index.html.tmp' => 'out/index.html';