Metadata support

This commit is contained in:
Nathan McCarty 2025-01-21 22:08:10 -05:00
parent 1d01a8986f
commit 707b55d761
2 changed files with 22 additions and 3 deletions

9
blog
View file

@ -33,7 +33,14 @@ multi MAIN(
die "Database file already exists, use --force to overwrite: {$file.Str}"
if $file.e && !$force;
my $db = DB::PostDB.new;
print "Blog Title: ";
my $title = get;
print "Tagline: ";
my $tagline = get;
my $meta = DB::BlogMeta.new: title => $title, tagline => $tagline;
my $db = DB::PostDB.new: meta => $meta;
if $force {
$file.spurt: $db.to-json, :create-only;

View file

@ -6,6 +6,14 @@ unit module DB;
use Pandoc;
use JSON::Class:auth<zef:vrurg>;
#| Top level metadata for the blog
class BlogMeta is json(:pretty) {
#| The title of the blog
has Str:D $.title is required;
#| The tagline of the blog
has Str:D $.tagline is required;
}
#| Shared post meta-data
role Post is json {
#| The location of the source file for the post
@ -16,6 +24,8 @@ role Post is json {
has DateTime:D @.edited-at = [];
#| An optional list of extra slugs to use for this post
has Str:D @.slugs = [];
#| Should the post be hidden from the main list
has Bool:D $.hidden is rw = False;
#| Get the title for this post, intended to be extracted from whatever
#| document produced it
@ -23,9 +33,9 @@ role Post is json {
#| Get the list of slugs for this post, including ones auto generated from
#| the title, as well as any additional slugs
method all-slugs(--> List[Str:D]) {
method all-slugs(--> Array[Str:D]) {
my $long-title-slug = self.title.lc.subst: /\h*/, '-';
return [$long-title-slug, @!slugs].flat.list;
return [$long-title-slug, @!slugs].flat.Array;
}
}
@ -50,6 +60,8 @@ class MarkdownPost does Post is json {
#| The top level posts database
class PostDB is json(:pretty) {
#| The metadat for the blog
has BlogMeta:D $.meta is required;
#| A mapping from post ids to posts
has Hash[Int:D, Post:D] %.posts = %();
}