From 1c1f94ecda0fd05dadf03996287142358d842a47 Mon Sep 17 00:00:00 2001 From: lymslive <403708621@qq.com> Date: Sat, 29 Dec 2018 23:17:47 +0800 Subject: [PATCH] script --- vm.pl | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/vm.pl b/vm.pl index 4d1d520..fdfbfb2 100755 --- a/vm.pl +++ b/vm.pl @@ -118,10 +118,55 @@ sub postbody } } +# fix links from/to home content and prev/next pages sub fixlink { my $base = $ENV{SCRIPT_NAME}; - if ($mdpath =~ /content\.md$/) { + my $subname = $query{p}; + if ($subname =~ /^content$/) { $body =~ s/href="(.+)\.md"/href="$base?p=$1"/g; } + else { + my ($prev, $next) = findprenext($subname); + my @foot = (); + + push(@foot, "
"); + $prev ? push(@foot, qq{Prev}) + : push(@foot, qq{First}); + push(@foot, qq{| Home |}); + $next ? push(@foot, qq{Next}) + : push(@foot, qq{Last}); + + $body .= sprintf(qq{\n\n}, join("\n", @foot)); + } +} + +# read content.md, find the prev/next page +sub findprenext +{ + my $curname = shift; + my ($prev, $next) = ('', ''); + + my $tocname = "$Bin/content.md"; + return ($prev, $next) unless -f $tocname; + + open(my $fh, '<', $tocname) or die "cannot open $tocname $!"; + my ($prev_may, $cur_may); + while (<$fh>) { + chomp; + next unless /\((\S+)\.md\)/; + $prev_may = $cur_may; + $cur_may = $1; + if ($cur_may eq $curname) { + $prev = $prev_may; + next; + } + if ($prev_may eq $curname) { + $next = $cur_may; + last; + } + } + close($fh); + + return ($prev, $next); }