あおやぎのさいと2.0という個人サイトを HTML 手打ちの静的サイトで運営しています。静的サイトであってもサイトマップとか RSS とかは出力したいのですが、さすがにそれらまで手打ちで作っていたら面倒でしょうがない。ということで、こんな簡単スクリプトを作って生成しています。
mksitemap.pl
#!/usr/bin/env perl
use strict;
use warnings;
use POSIX 'strftime';
print <<'XML';
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
XML
foreach my $file (`find . -name "*.html"`) {
chomp $file;
$file =~ s!^\./!!;
my @stat = stat($file);
my $lastmod = strftime( "%Y-%m-%d", localtime($stat[9]));
print <<"XML";
<url>
<loc>https://www.saoyagi2.net/${file}</loc>
<lastmod>${lastmod}</lastmod>
</url>
XML
}
print <<'XML';
</urlset>
XML
mkrss.pl
#!/usr/bin/env perl
use strict;
use warnings;
use Encode;
use utf8;
print encode('utf-8', <<"RSS"
<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0'>
<channel>
<title>あおやぎのさいと2.0</title>
<link>https://www.saoyagi2.net/</link>
<description>あおやぎ(saoyagi2)の個人サイトです。</description>
RSS
);
my @items;
foreach my $file (`find . -name "*.html"`) {
chomp $file;
$file =~ s!^\./!!;
my $buffer = decode('utf-8', `cat $file`);
next if(!($buffer =~ /<meta name="date" content="(.*)">/));
my $date = $1;
next if(!($buffer =~ /<title>(.*)<\/title>/));
my $title = $1;
next if(!($buffer =~ /<article>(.+?)<nav>.+?<\/nav>.+?<\/article>/s));
my $description = $1;
$description =~ s/<.+?>//sg;
$description = substr($description, 0, 100);
push(@items, [$file, $title, $description, $date]);
}
@items = (reverse sort {$a->[3] cmp $b->[3]} @items);
splice @items, 10;
foreach my $item (@items) {
print encode('utf-8', <<"RSS"
<item>
<title>${$item}[1]<\/title>
<link>https://www.saoyagi2.net/${$item}[0]<\/link>
<description>${$item}[2]<\/description>
<pubDate>${$item}[3]<\/pubDate>
<\/item>
RSS
);
}
print encode('utf-8', <<"RSS"
</channel>
</rss>
RSS
);
文字列連結で XML 組み立てたりしてるいい加減なものですがね。
RSS 生成時の記事作成年月日は各記事に <meta name="date" content="YYYY-MM-DD">
と入れておいて、それを拾うようにしています。
最終的には AWS S3 + AWS CloudFront 環境へ以下のスクリプトでデプロイ。
#!/bin/sh
./mksitemap.pl > sitemap.xml
./mkrss.pl > rss.xml
aws s3 sync . s3://www.saoyagi2.net/ --delete
aws cloudfront create-invalidation --distribution-id xxx --paths '/*'