Add series data structure

This commit is contained in:
Nathan McCarty 2025-02-09 01:50:37 -05:00
parent 49502df416
commit 0571a16dab
3 changed files with 38 additions and 1 deletions

View file

@ -24,6 +24,7 @@ method generate-post(Post:D $post, BlogMeta:D $meta) {
] ]
] ]
]; ];
# TODO: Setup Comments
# TODO: Setup footer # TODO: Setup footer
# my $footer; # my $footer;

View file

@ -9,6 +9,7 @@ use XML;
use XQ; use XQ;
use DB::Post; use DB::Post;
use DB::Series;
use DB::BlogMeta; use DB::BlogMeta;
use DB::MarkdownPost; use DB::MarkdownPost;
use DB::IdrisPost; use DB::IdrisPost;
@ -25,6 +26,8 @@ class PostDB {
#| A mapping from post ids to posts #| A mapping from post ids to posts
# has %.posts is Posts; # has %.posts is Posts;
has %.posts{Int} of PostTypes = %(); has %.posts{Int} of PostTypes = %();
#| A mapping from series ids to series
has %.series{Int} of Series = %();
#| The post id to use for placeholder posts #| The post id to use for placeholder posts
has Int $.placeholder-id = 0; has Int $.placeholder-id = 0;
@ -57,9 +60,11 @@ class PostDB {
#| Write a database to a directory #| Write a database to a directory
method write(IO::Path:D $dir) { method write(IO::Path:D $dir) {
my $posts-dir = $dir.add('posts/'); my $posts-dir = $dir.add('posts/');
my $series-dir = $dir.add('series/');
# Make sure directory structrue exists # Make sure directory structrue exists
mkdir $dir unless $dir.e; mkdir $dir unless $dir.e;
mkdir $posts-dir unless $posts-dir.e; mkdir $posts-dir unless $posts-dir.e;
mkdir $series-dir unless $series-dir.e;
# Write out metadata # Write out metadata
# TODO: Track changes and only write changed files # TODO: Track changes and only write changed files
$dir.add('meta.json').spurt: $!meta.to-json(:sorted-keys); $dir.add('meta.json').spurt: $!meta.to-json(:sorted-keys);
@ -67,6 +72,10 @@ class PostDB {
for %!posts.kv -> $key, $value { for %!posts.kv -> $key, $value {
$posts-dir.add("$key.json").spurt: $value.to-json(:sorted-keys); $posts-dir.add("$key.json").spurt: $value.to-json(:sorted-keys);
} }
# Write out the series
for %!series.kv -> $key, $value {
$series-dir.add("$key.json").spurt: $value.to-json(:sorted-keys);
}
} }
#| Render the site to the provided output directory #| Render the site to the provided output directory
@ -114,6 +123,7 @@ class PostDB {
$tags-dir.add("$tag.html").spurt: $tags-dir.add("$tag.html").spurt:
$config.generate-tag-page(self, $tag); $config.generate-tag-page(self, $tag);
} }
# TODO: Generate the series pages
# Render the rss/atom feed # Render the rss/atom feed
my $atom-path = $out-dir.add('atom.xml'); my $atom-path = $out-dir.add('atom.xml');
my $atom = posts-to-atom self; my $atom = posts-to-atom self;
@ -135,6 +145,7 @@ class PostDB {
#| Read the database out of a directory #| Read the database out of a directory
sub read-db(IO::Path:D $dir --> PostDB:D) is export { sub read-db(IO::Path:D $dir --> PostDB:D) is export {
my $posts-dir = $dir.add('posts/'); my $posts-dir = $dir.add('posts/');
my $series-dir = $dir.add('series/');
die "DB directory does not exist" unless $dir.e; die "DB directory does not exist" unless $dir.e;
die "posts directory does not exist" unless $posts-dir.e; die "posts directory does not exist" unless $posts-dir.e;
# Read metadata # Read metadata
@ -160,6 +171,16 @@ sub read-db(IO::Path:D $dir --> PostDB:D) is export {
} }
} }
} }
# Build db structure # Read series
PostDB.new: meta => $meta, posts => %posts my %series{Int} of PostTypes = %();
# For backwards compatability, the series directory is optional. It will be
# created on the next db operation, but we don't need it to read the site
if $series-dir.e {
for dir $series-dir -> $series {
my $id = $series.extension("").basename.Int;
%series{$id} = Series.from-json: $series.slurp;
}
}
# Build db structure
PostDB.new: meta => $meta, posts => %posts, series => %series
} }

15
lib/DB/Series.rakumod Normal file
View file

@ -0,0 +1,15 @@
use v6.e.PREVIEW;
use JSON::Class:auth<zef:vrurg>;
#| A plain markdown post
unit class Series is json(:pretty);
#| The title of a series
has Str:D $.title is required;
#| The description of a series
has Str:D $.desc is required;
#| The ids of the posts in the series, in series order
has Int:D @.post-ids = [];