Basci atom feed generation

This commit is contained in:
Nathan McCarty 2025-02-05 04:37:28 -05:00
parent 7d5cbfba3c
commit 53c0c6a9d6
6 changed files with 101 additions and 7 deletions

6
blog
View file

@ -44,8 +44,12 @@ multi MAIN(
my $title = get; my $title = get;
print "Tagline: "; print "Tagline: ";
my $tagline = get; my $tagline = get;
print "Base URL: ";
my $base-url = get;
my $meta = BlogMeta.new: title => $title, tagline => $tagline; my $meta =
BlogMeta.new:
title => $title, tagline => $tagline, base-url => $base-url;
my $db = DB::PostDB.init: $meta; my $db = DB::PostDB.init: $meta;

64
lib/Atom.rakumod Normal file
View file

@ -0,0 +1,64 @@
use v6.e.PREVIEW;
use DB::Post;
use DB::BlogMeta;
use XML;
unit module Atom;
# get the link for a post
sub post-link(BlogMeta:D $meta, Int:D $id, Post:D $post --> Str:D) {
my @slugs = $post.all-slugs;
my $base = $meta.get-base-url;
if @slugs.elems {
"$base/posts/by-slug/{@slugs[*-1]}.html"
} else {
"$base/posts/by-id/$id.html"
}
}
#| Convert a single post to an atom item
sub post-to-item(BlogMeta:D $meta, Int:D $id, Post:D $post --> XML::Element) {
my $link = post-link $meta, $id, $post;
my $desc = $post.description;
my $xml = XML::Element.new(:name<entry>);
$xml.append:
XML::Element.new(:name<id>, :nodes([$link]));
$xml.append:
XML::Element.new(:name<title>, :nodes([$post.title]));
$xml.append:
XML::Element.new(:name<updated>, :nodes([$post.updated]));
$xml.append:
XML::Element.new(:name<published>, :nodes([$post.posted-at]));
my $author = XML::Element.new(:name<author>);
$author.append:
XML::Element.new(:name<email>, :nodes(["thatonelutenist@stranger.systems"]));
$author.append:
XML::Element.new(:name<name>, :nodes(["Nathan McCarty"]));
$xml.append: $author;
$xml.append:
XML::Element.new(:name<link>, :attribs({:href($link), :rel<alternate>}));
$xml.append:
XML::Element.new(:name<summary>, :nodes([$desc])) if $desc;
$xml
}
#| Produce an atom feed from the database
sub posts-to-atom($db --> XML::Element) is export {
my $updated = $db.posts.values.map(*.updated).max;
my $xml =
XML::Element.new(
:name<feed>,
:attribs({:xmlns('http://www.w3.org/2005/Atom')}));
$xml.append: XML::Element.new(:name<id>, :nodes([$db.meta.get-base-url]));
$xml.append: XML::Element.new(:name<title>, :nodes([$db.meta.title]));
$xml.append: XML::Element.new(:name<updated>, :nodes([$updated]));
for $db.sorted-posts -> $pair {
unless $pair.value.hidden {
$xml.append: post-to-item $db.meta, $pair.key, $pair.value;
}
}
$xml
}

View file

@ -72,7 +72,7 @@ method site-header(BlogMeta:D $meta) {
'About'; 'About';
]; ];
]; ];
a :href</feed.xml>, [ a :href</atom.xml>, [
icon 'rss'; icon 'rss';
' '; ' ';
span [ span [
@ -193,8 +193,8 @@ method generate-blurb(Int:D $id, $db) {
my $desc = $post.description; my $desc = $post.description;
my @slugs = $post.all-slugs; my @slugs = $post.all-slugs;
# Use the primary slug if there is one, the id if there isn't # Use the primary slug if there is one, the id if there isn't
my $link = do if @slugs.elems > 0 { my $link = do if @slugs.elems {
"/posts/by-slug/{@slugs[0]}.html" "/posts/by-slug/{@slugs[*-1]}.html"
} else { } else {
"/posts/by-id/$id.html" "/posts/by-id/$id.html"
} }

View file

@ -5,12 +5,14 @@ unit module DB;
use Pandoc; use Pandoc;
use JSON::Class:auth<zef:vrurg>; use JSON::Class:auth<zef:vrurg>;
use XML;
use DB::Post; use DB::Post;
use DB::BlogMeta; use DB::BlogMeta;
use DB::MarkdownPost; use DB::MarkdownPost;
use DB::IdrisPost; use DB::IdrisPost;
use DB::PlaceholderPost; use DB::PlaceholderPost;
use Atom;
use Config; use Config;
subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D; subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D;
@ -92,12 +94,14 @@ class PostDB {
$out-dir.add('index.html').spurt: $config.generate-index(self); $out-dir.add('index.html').spurt: $config.generate-index(self);
# Render the archive # Render the archive
$out-dir.add('archive.html').spurt: $config.generate-archive(self); $out-dir.add('archive.html').spurt: $config.generate-archive(self);
# TODO: Symlink the about article # Symlink the about article
my $about-path = $out-dir.add('about.html'); my $about-path = $out-dir.add('about.html');
$about-path.unlink if $about-path.l; $about-path.unlink if $about-path.l;
$by-id.add("{$!meta.about-id}.html").symlink: $about-path; $by-id.add("{$!meta.about-id}.html").symlink: $about-path;
# TODO: Render the rss/atom feed # Render the rss/atom feed
die "Not Implemented" my $atom-path = $out-dir.add('atom.xml');
my $atom = posts-to-atom self;
$atom-path.spurt: ~$atom;
} }
#| Get a list of posts sorted by date #| Get a list of posts sorted by date

View file

@ -16,3 +16,16 @@ has Int:D $.placeholder-id is rw = 0;
#| The id of the about post #| The id of the about post
has Int:D $.about-id is rw = 0; has Int:D $.about-id is rw = 0;
#| The base url of this post
has Str:D $.base-url is required;
#| Return the base url, but substitute it out if the test environment variable
#| is set
method get-base-url(--> Str:D) {
if %*ENV<BLOG_TEST> {
"http://localhost:8080"
} else {
$!base-url
}
}

View file

@ -43,6 +43,15 @@ has Bool:D $.hidden is json is rw = False;
#| document produced it #| document produced it
method title(--> Str:D) {...} method title(--> Str:D) {...}
#| The time the post was last updated at
method updated(--> DateTime:D) {
if @!edited-at {
@!edited-at.max
} else {
$!posted-at
}
}
#| Get the list of slugs for this post, including ones auto generated from #| Get the list of slugs for this post, including ones auto generated from
#| the title, as well as any additional slugs #| the title, as well as any additional slugs
method all-slugs(--> Array[Str:D]) { method all-slugs(--> Array[Str:D]) {