/
make_it_local.pl
85 lines (60 loc) · 1.75 KB
/
make_it_local.pl
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
#!/usr/bin/perl
# Make the oeis-draft links as local HTML pages.
use 5.036;
use File::Spec::Functions qw(catfile);
my $cache_dir = 'cache';
my $local_dir = 'local';
if (not -d $local_dir) {
mkdir($local_dir);
}
my %cache;
foreach my $file (glob("$cache_dir/*")) {
-f $file or next;
open my $fh, '<:utf8', $file or next;
chomp(my $url = <$fh>);
if (exists $cache{$url}) {
next if ((-M $file) > (-M $cache{$url})); # skip older cache file
}
$cache{$url} = $file;
}
sub read_cache_file ($file) {
open my $fh, '<:utf8', $file or die "error: $!";
while (defined(my $line = <$fh>)) {
chomp($line);
$line eq '' and last;
}
my $html = do {
local $/;
<$fh>;
};
$html =~ s{<script[ >].*?</script>}{}gs;
return $html;
}
sub make_local_cache ($id, $url) {
my $local_file = catfile($local_dir, $id . '.html');
if (-e $local_file) {
return $local_file;
}
my $cache_file = $cache{$url} // die "can't find $url in cache";
my $html = read_cache_file($cache_file);
open my $fh, '>:utf8', $local_file or die "can't create $local_file: $!";
print $fh $html;
close $fh;
return $local_file;
}
open my $fh, '<:utf8', 'links.html' or die "error: $!";
open my $out_fh, '>:utf8', 'local.html' or die "error: $!";
while (defined(my $line = <$fh>)) {
if ($line =~ m{^\s*<li>\s*\[\d+\]\s*<a\s*href=(https://oeis.org/draft/\w+)>https://oeis.org/draft/(\w+)</a>\s*</li>}) {
my $url = $1;
my $id = $2;
my $local_file = make_local_cache($id, $url);
$line =~ s{https://oeis.org/draft/\w+}{$local_file};
print $out_fh $line;
}
else {
print $out_fh $line;
}
}
close $fh;
close $out_fh;