-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen-vimwiki-diary-index.pl
executable file
·73 lines (53 loc) · 1.32 KB
/
gen-vimwiki-diary-index.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
#!/usr/bin/env perl
use strict;
use warnings;
use IO::File;
my $usage = "Usage: $0 <path> <.ext>\n";
my $path = shift || die $usage; $path =~ s{diary/?$}{};
my $ext = shift || die $usage; $ext =~ s/^\.+//;
$path = "$path/diary/";
chdir $path or die;
my @files = sort { $b cmp $a } grep {
/^(\d\d\d\d-\d\d-\d\d)\.$ext$/
} glob("*.$ext");
exit unless @files;
my $OUT = new IO::File("$path/diary.$ext", "w");
select $OUT;
print "# 日记\n";
my $curYear = '';
my $curMonth = '';
foreach my $file (@files) {
my ($date, $year, $month) = $file =~ /^((\d\d\d\d)-(\d\d)-\d\d)/;
if ( $year ne $curYear ){
print "\n## $year 年\n";
$curYear = $year;
$curMonth = '';
}
if ( $month ne $curMonth ){
print "\n### $month 月\n\n";
$curMonth = $month;
}
my $title = getTitle($file);
if ( not defined $title ){
print " * [$date]()\n";
}
else {
print " * [$date $title]($file)\n";
}
}
sub getTitle {
my $file = shift;
my $fh = new IO::File($file) or die "open: $!\n";
my $title;
my $entered = 0;
while(<$fh>) {
if (/^---/) {
last if $entered;
$entered = 1;
next;
}
next if not $entered;
last if ($title) = (m/^title:\s+(.*?)$/);
}
return $title;
}