diff --git a/blog b/blog index e3ebd30..45a2f74 100755 --- a/blog +++ b/blog @@ -2,6 +2,7 @@ use v6.e.PREVIEW; use DB; +use DB::BlogMeta; use DB::MarkdownPost; my %*SUB-MAIN-OPTS = @@ -45,9 +46,9 @@ multi MAIN( print "Tagline: "; my $tagline = get; - my $meta = DB::BlogMeta.new: title => $title, tagline => $tagline; + my $meta = BlogMeta.new: title => $title, tagline => $tagline; - my $db = DB::PostDB.new: meta => $meta; + my $db = DB::PostDB.init: $meta; if $force { $db-file.spurt: $db.to-json, :create-only; diff --git a/lib/DB.rakumod b/lib/DB.rakumod index b7bbb05..ef3ca23 100644 --- a/lib/DB.rakumod +++ b/lib/DB.rakumod @@ -10,10 +10,17 @@ use DB::Post; use DB::BlogMeta; use DB::MarkdownPost; use DB::IdrisPost; +use DB::PlaceholderPost; -class Posts is json(:dictionary(:keyof(Int:D), MarkdownPost:D, IdrisPost:D)) {} +class Posts is json( + :dictionary( + :keyof(Int:D), + MarkdownPost:D, + IdrisPost:D, + PlaceholderPost:D, + )) {} -subset PostTypes where MarkdownPost:D | IdrisPost:D; +subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D; #| The top level posts database class PostDB is json(:pretty) { @@ -21,6 +28,8 @@ class PostDB is json(:pretty) { has BlogMeta:D $.meta is required; #| A mapping from post ids to posts has %.posts is Posts; + #| The post id to use for placeholder posts + has Int $.placeholder-id = 0; method TWEAK() { %!posts := Posts.new unless %!posts; @@ -41,4 +50,14 @@ class PostDB is json(:pretty) { %!posts{$id} = $post; $id } + + #| Initialize a new database + method init(BlogMeta:D $meta --> PostDB:D) { + my %posts is Posts = Posts.new; + %posts{0} = PlaceholderPost.empty; + PostDB.new( + meta => $meta, + posts => %posts, + ) + } } diff --git a/lib/DB/PlaceholderPost.rakumod b/lib/DB/PlaceholderPost.rakumod new file mode 100644 index 0000000..74d1ac8 --- /dev/null +++ b/lib/DB/PlaceholderPost.rakumod @@ -0,0 +1,32 @@ +use v6.e.PREVIEW + +use Pandoc; +use JSON::Class:auth; + +use DB::Post; + +#| An empty placeholder post +unit class PlaceholderPost does Post is json; + +#| Marker for disambiguation between post types in json representation, the +#| cheaty way +has Bool:D $.placeholder = True; + +method title(--> Str:D) { + "Placeholder Article" +} + +#| Shortcut for creating an empty placeholder post +method empty(--> PlaceholderPost:D) { + PlaceholderPost.new( + source => "/dev/null".IO, + posted-at => DateTime.now, + hidden => True, + ) +} + +# Override the all-slugs method to return an empty array, we don't want the +# placeholder article to be refered to as anything but its id +method all-slugs(--> Array[Str:D]) { + Array[Str:D].new +}