Created
April 9, 2017 07:44
-
-
Save jgreely/cbda1af3ecc399e85420c63ba58b6896 to your computer and use it in GitHub Desktop.
Create monthly archives for a Hugo site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# work around Hugo's lack of monthly archives by filling the archive | |
# section with stub articles, one for each month referenced in the | |
# front matter of articles in the "post" category: | |
# +++ | |
# date = "2017-04-01T00:00:00" | |
# title = "April 2017" | |
# month = "2017-04" | |
# +++ | |
# | |
# To list all the articles from that month, | |
# layouts/archive/single.html contains: | |
# {{ $month := .Params.month }} | |
# {{ range $.Site.Sections.post.Pages.GroupByDate "2006-01" }} | |
# {{ if eq .Key $month }} | |
# {{ range .Pages.ByDate }} | |
# {{ partial "article-summary.html" . }} | |
# {{end}} | |
# {{end}} | |
# {{end}} | |
use strict; | |
my @monthname = qw( | |
monthzero | |
January February March April May June July | |
August September October November December | |
); | |
my %month; | |
foreach my $entry (<content/post/*.md>) { | |
open(In,$entry); | |
my $is_frontmatter = 2; | |
while (<In>) { | |
chomp; | |
$is_frontmatter-- if /^\+\+\+/; | |
last unless $is_frontmatter; | |
if (/^date\s*=\s*"((?:\d+)-(?:\d+))-/) { | |
$month{$1}++; | |
last; | |
} | |
} | |
close(In); | |
} | |
mkdir("content/archive") unless -d "content/archive"; | |
chdir("content/archive") or die "$0: content/archive: $!\n"; | |
unlink(<20*.md>); | |
foreach my $month (keys %month) { | |
my ($y,$m) = split(/-/,$month); | |
open(Out,">$month.md") or die "$0: $month: $!\n"; | |
print Out <<EOF; | |
+++ | |
date = "$month-01T00:00:00" | |
title = "$monthname[$m] $y" | |
month = "$month" | |
+++ | |
EOF | |
close(Out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment