-
Notifications
You must be signed in to change notification settings - Fork 2
/
linkthem.pl
executable file
·112 lines (96 loc) · 2.02 KB
/
linkthem.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl
sub fentries {
my ($d)= @_;
opendir(my $dh, $d) || die "Can't opendir $some_dir: $!";
my @f = grep { /^\d\d\d\d-.*.md\z/ && -f "$d/$_" } readdir($dh);
closedir $dh;
return @f;
}
opendir(my $dh, ".") || die "Can't opendir $some_dir: $!";
my @years = grep { /^\d\d\d\d/ && -d "$_" } readdir($dh);
closedir $dh;
for my $y (sort @years) {
my @f = fentries($y);
for my $f (sort @f) {
push @a, "$y/$f";
}
}
# make a rel link to the new file from the old
sub rel {
my ($n, $old) = @_;
my $ny;
my $oy;
if($n =~ /^(\d\d\d\d)/) {
$ny = $1;
}
if($old =~ /^(\d\d\d\d)/) {
$oy = $1;
}
if($ny == $oy) {
# same year, return the file part
$n =~ s/[0-9]+\///g;
return $n;
}
# not the same year, return ../ new
return "../$n";
}
sub fixup {
my ($i) = @_;
my $f = $a[$i];
my @n;
my $blank;
my $title;
open(F, "<$f");
while(<F>) {
chomp;
if(/^# (.*)/ && !$title) {
$name{$f} = $1;
$title = 1;
}
if(/^## Links/) {
last;
}
push @n, "$_\n";
$blank = length($_);
}
close(F);
if($blank) {
push @n, "\n";
}
push @n, "## Links\n\n";
push @n, sprintf "[<< prev](%s) | [up](%s) | [next >> ](%s)\n",
rel($a[$i - 1], $a[$i]),
"../",
rel($a[$i + 1], $a[$i]);
open(F, ">$f");
print F @n;
close(F);
}
for $i (0 .. $#a) {
fixup($i);
}
sub dateit {
my ($path) = @_;
# remove the leading year dir
$path =~ s/\d\d\d\d\///;
# remove the extension
$path =~ s/\.md\z//;
# remove letters (like 'b')
$path =~ s/[a-z]//g;
return $path;
}
print <<HEAD
# All emails listed
This is the full index of the emails in the collection.
|num|name|date|
|---|----|----|
HEAD
;
for $i (0 .. $#a) {
my $f = $a[$i];
printf "|%d|[%s](%s)|%s|\n", $i + 1, $name{$f}, $f, dateit($f);
}
print <<HEAD
[back to main page](../)
HEAD
;