forked from perladvent/Perl-Advent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
year2yaml
executable file
·59 lines (54 loc) · 1.59 KB
/
year2yaml
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
#!/usr/bin/env perl
#===============================================================================
#
# FILE: year2yaml
#
# USAGE: year2yaml YYYY [ outfile ]
#
# DESCRIPTION: Append the given year's topics to the archives.yaml file
#
# OPTIONS: If the outfile is specified, use that instead of
# 'archives.yaml'
# REQUIREMENTS: ---
# BUGS: Assumes every topic is a module
# NOTES: ---
# AUTHOR: Pete Houston (pete), [email protected]
# ORGANIZATION: Openstrike
# VERSION: 1.0
# CREATED: 05/07/23
#===============================================================================
use strict;
use warnings;
use 5.010;
my ($year, $archfile) = @ARGV;
die "You must provide the year as the first argument.\n"
unless defined $year;
$year =~ /^\d{4}$/ or die "Bad year: $year\n";
my $ydir = "$year/articles";
$archfile //= 'archives.yaml';
die "No articles found for $year\n" unless -d $ydir;
my $ytext = "\n$year:\n";
my $yh;
for my $day (1 .. 25) {
my $file = sprintf "$ydir/%4.4i-12-%2.2i.pod", $year, $day;
print "Processing file $file\n";
my @modlist;
next unless -e $file;
open my $ifh, '<', $file or die "Cannot open $file for reading: $!";
while (my $line = <$ifh>) {
if ($line =~ /^Topic: (\S+)/) {
print "Found topic $1\n";
push @modlist, $1;
}
last unless $line =~ /\S/;
}
close $ifh;
next unless @modlist;
$ytext .= sprintf " %2.2i:\n", $day;
for my $m (@modlist) {
$ytext .= qq# - { "module" : "$m" }\n#;
}
}
open my $ofh, '>>', $archfile or die "Cannot open $archfile for writing: $!";
print $ofh $ytext;
close $ofh;