website/blog

96 lines
2.2 KiB
Text
Raw Normal View History

2025-01-20 17:59:22 -05:00
#!/usr/bin/env raku
2025-01-21 03:24:01 -05:00
use v6.e.PREVIEW;
2025-01-21 01:31:33 -05:00
use DB;
2025-01-22 20:49:27 -05:00
use DB::MarkdownPost;
2025-01-21 01:31:33 -05:00
2025-01-21 03:24:01 -05:00
my %*SUB-MAIN-OPTS =
:named-anywhere,
:bundling,
;
2025-01-21 01:31:33 -05:00
2025-01-21 21:41:28 -05:00
my IO::Path:D $blog-dir = $*PROGRAM.parent;
2025-01-21 03:24:01 -05:00
#= The directory this script is located in
#| Load the database from the provided file
sub load-db(IO::Path $file --> DB::PostDB:D) {
my $result = DB::PostDB.from-json: $file.slurp;
if $result ~~ DB::PostDB:D {
return $result;
} else {
die "Error parsing $file as databse: $result";
}
}
2025-01-22 04:29:44 -05:00
#| Write the databse to the provided file
sub write-db(IO::Path $file, DB::PostDB $db) {
my $output = $db.to-json;
$file.spurt: $output;
}
2025-01-21 03:24:01 -05:00
#| Initalize the database
multi MAIN(
"db",
"init",
#| The path of the database file
2025-01-22 04:29:44 -05:00
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
2025-01-21 03:24:01 -05:00
#| Overwrite an already existing database file
Bool :$force
) {
2025-01-22 04:29:44 -05:00
die "Database file already exists, use --force to overwrite: {$db-file.Str}"
if $db-file.e && !$force;
2025-01-21 03:24:01 -05:00
2025-01-21 22:08:10 -05:00
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;
2025-01-21 03:24:01 -05:00
if $force {
2025-01-22 04:29:44 -05:00
$db-file.spurt: $db.to-json, :create-only;
2025-01-21 03:24:01 -05:00
} else {
2025-01-22 04:29:44 -05:00
$db-file.spurt: $db.to-json;
2025-01-21 03:24:01 -05:00
}
}
#| Ensure that the database loads, erroring otherwise
multi MAIN(
"db",
"verify",
#| The path of the database file
2025-01-22 04:29:44 -05:00
IO::Path(Str) :$db = $blog-dir.add("db.json"),
2025-01-21 03:24:01 -05:00
) {
2025-01-22 04:29:44 -05:00
load-db $db;
2025-01-21 03:24:01 -05:00
say "Database OK";
}
2025-01-22 04:29:44 -05:00
#| Create a new markdown post
multi MAIN(
"new",
"markdown",
#| The path to the markdown file
IO::Path(Str) $source,
#| The path of the database file
IO::Path(Str) :$db-file = $blog-dir.add("db.json"),
#| The date/time the post should be recorded as posted at
DateTime(Str) :$posted-at = DateTime.now,
#| Should the post be hidden from the archive?
Bool :$hidden = False,
) {
my $db = load-db $db-file;
my $id =
$db.insert-post:
2025-01-22 20:49:27 -05:00
MarkdownPost.new(
2025-01-22 04:29:44 -05:00
source => $source.absolute.IO,
posted-at => $posted-at,
hidden => $hidden,
);
write-db $db-file, $db;
say 'Post inserted with id ', $id;
2025-01-22 21:17:38 -05:00
say 'Post has slugs: ', $db.posts{$id}.all-slugs;
2025-01-22 04:29:44 -05:00
}