-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinks.pl
66 lines (53 loc) · 1.23 KB
/
links.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
#
# Replace PREVLNK and NEXTLNK by valid links
# to the previous and next part of the article.
# Deal with NAVLIST and FILENAME too.
#
use strict;
sub get_title {
my $art = shift @_;
return "" if !$art;
open (my $fh, "<", $art . ".md")
or die "cannot open $art";
while (<$fh>) {
if (/^##\s/) {
s/^##\s*//;
close $fh;
chop;
return $_;
}
}
}
sub get_neighbors {
my ($cur, @l) = @_;
my $idx = 0;
$idx++ until ($cur eq $l[$idx]);
return ("", $l[$idx+1]) if ($idx == 0);
return ($l[$idx-1], $l[$idx+1]);
}
sub build_nav {
my ($cur, @l) = @_;
my $res;
for my $art (@l) {
my $title = get_title($art);
my $class = " class=\"active\"" if $art eq $cur;
$res .= "<li$class><a href=\"$art.html\">$title</a></li>\n";
}
return $res;
}
my ($cur, $l) = splice @ARGV, 0, 2;
$cur =~ s/\..*$//;
my (@l) = split / /, $l;
s/\..*$// for (@l);
my $nav = build_nav($cur, @l);
my ($prev, $next) = get_neighbors($cur, @l);
my ($prevtitle, $nexttitle) = (get_title($prev), get_title($next));
my $prevlnk = ($prev and "<a href=\"$prev.html\">< $prevtitle</a>");
my $nextlnk = ($next and "<a href=\"$next.html\">$nexttitle ></a>");
while (<>) {
s/PREVLNK/$prevlnk/;
s/NEXTLNK/$nextlnk/;
s/FILENAME/$cur/;
s/NAVLIST/$nav/;
print;
}