Original post is here: eklausmeier.goip.de
As written in Generate RSS from Markdown extracting RSS from Markdown with frontmatter is simple. Now I took a slightly different approach and generate RSS from the HTML files directly.
For this blog I still want an RSS feed. Simplified Saaze did not provide this functionality. Since 15-Aug-2022 Simplified Saaze can directly generate an RSS feed.
An RSS feed contains a header with some fixed XML. Each RSS entry consists of:
- link / URL
- publication date
- title
- the full blog post content
RSS closes with some XML tags. As these blog posts are generated by Saaze from Markdown with frontmatter they contain all information that is in the Markdown with frontmatter. Of course, provided the templates pass through all this information.
Below Perl script extracts this information out of the generated HTML. In this case it extracts the title of the <h1>
tag. The date is extracted when seeing some <p class>
. It stops if the <footer>
tag is seen.
1#!/bin/perl -W
2# Create RSS XML file ("feed") from Saaze generated HTML files
3#
4# Input: List of HTML files (order of files determines order of <item>))
5# Output: RSS
6#
7# Example:
8# bloghtmlrss `find blog/2021 -name index.html | sort -r`
9
10use strict;
11use POSIX qw(strftime);
12use POSIX qw(mktime);
13
14my $dt = strftime("%a, %d %b %Y %H:%M:%S GMT",gmtime()); # RFC-822 format: Wed, 02 Oct 2002 13:00:00 GMT
15print <<"EOT";
16<?xml version="1.0" encoding="utf-8"?>
17<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
18<channel>
19 <title>Elmar Klausmeier's Blog</title>
20 <description>Elmar Klausmeier's Blog</description>
21 <lastBuildDate>$dt</lastBuildDate>
22 <link>https://eklausmeier.goip.de</link>
23 <atom:link href="https://eklausmeier.goip.de/feed.xml" rel="self" type="application/rss+xml" />
24 <generator>bloghtmlrss</generator>
25
26EOT
27
28my %monthNr = ( # Convert full month name to month-number minus one
29 "January" => 0, "February" => 1, "March" => 2, "April" => 3,
30 "May" => 4, "June" => 5, "July" => 6, "August" => 7,
31 "September" => 8, "October" => 9, "November" => 10, "December" => 11
32);
33
34sub item(@) {
35 my $f = $_[0];
36 return if ($f =~ /\/\d{4}\/index\.html$/); # ignore .../2021/index.html etc.
37 open(F,"< $f") || die("Cannot open $f");
38
39 my $link = $f;
40 $link =~ s/index\.html$//;
41 print "\t<item>\n"
42 . "\t\t<link>https://eklausmeier.goip.de/$link</link>\n"
43 . "\t\t<guid>https://eklausmeier.goip.de/$link</guid>\n";
44
45 my ($dt,$year,$month,$day,$hour,$minute,$sec);
46 my ($title,$linecnt) = (0,0);
47 while (<F>) {
48 chomp;
49 if (/^<h1.*?>(.+?)<\/h1>/) {
50 printf("\t\t<title>%s</title>\n",$1);
51 $title = 1;
52 } elsif (/^\s*<p class=.+?>(\d+)..\s+(\w+)\s+(\d\d\d\d)<\/p>/) {
53 ($year,$month,$day,$hour,$minute,$sec) = ($3,$monthNr{$2},$1,12,0,0);
54 # RFC-822 format: Wed, 02 Oct 2002 13:00:00 GMT
55 $dt = strftime("%a, %d %b %Y %H:%M:%S GMT",$sec,$minute,$hour,$day,$month,$year-1900);
56 printf("\t\t<pubDate>%s</pubDate>\n",$dt);
57 } elsif ($title) {
58 if ($linecnt++ == 0) {
59 print "\t\t<description><![CDATA[\n";
60 }
61 last if (/^\t<footer>/);
62 s/<a href="\.\.\/\.\.\/\.\.\//<a href="https:\/\/eklausmeier\.goip\.de\//g;
63 s/<a href="\.\.\/\.\.\/2/<a href="https:\/\/eklausmeier\.goip\.de\/blog\/2/g;
64 s/<img src="\.\.\/\.\.\/\.\.\/img\//<img src="https:\/\/eklausmeier\.goip\.de\/img\//g;
65 print $_ . "\n";
66 }
67 }
68 print << "EOT";
69 ]]></description>
70 </item>
71EOT
72
73 close(F) || die("Cannot close $f");
74}
75
76
77while (<@ARGV>) {
78 item($_);
79}
80
81
82print "</channel>\n</rss>\n";
Source code for bloghtmlrss
is in GitHub.
I checked the validity of the generated RSS with W3C Feed Validation Service.
Added 15-Aug-2022: Since today Simplified Saaze can generate RSS directly with -x
flag, see RSS XML Feed.