website/blog

132 lines
3.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;
use DB::BlogMeta;
2025-01-22 20:49:27 -05:00
use DB::MarkdownPost;
2025-02-03 20:22:32 -05:00
use DB::IdrisPost;
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
#| The directory this script is located in
my IO::Path:D $default-blog-dir = $*PROGRAM.parent;
2025-01-21 03:24:01 -05:00
#| Default database directory
my IO::Path:D $default-db-dir =
do if %*ENV<BLOG_TEST> {
$default-blog-dir.add('test-db/')
2025-01-21 03:24:01 -05:00
} else {
$default-blog-dir.add('db/')
};
2025-01-22 04:29:44 -05:00
2025-02-03 21:36:07 -05:00
#| The default output directory
my IO::Path:D $default-site-dir = $default-blog-dir.add('site/');
2025-02-03 23:43:19 -05:00
#| The default idris ipkg
my IO::Path:D $default-ipkg = $default-blog-dir.add('projects/Idris/Idris.ipkg');
2025-01-21 03:24:01 -05:00
#| Initalize the database
multi MAIN(
"db",
"init",
#| The path of the database file
IO::Path(Str) :$db-dir = $default-db-dir,
2025-01-21 03:24:01 -05:00
#| Overwrite an already existing database file
Bool :$force
) {
die "Database folder already exists, use --force to overwrite: {$db-dir.Str}"
if $db-dir.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 = BlogMeta.new: title => $title, tagline => $tagline;
2025-01-21 22:08:10 -05:00
my $db = DB::PostDB.init: $meta;
2025-01-21 03:24:01 -05:00
$db.write: $db-dir;
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
IO::Path(Str) :$db-dir = $default-db-dir,
2025-01-21 03:24:01 -05:00
) {
read-db $db-dir;
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-dir = $default-db-dir,
2025-01-22 04:29:44 -05:00
#| 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 = read-db $db-dir;
2025-01-22 04:29:44 -05:00
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,
);
$db.write: $db-dir;
2025-01-22 04:29:44 -05:00
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
}
2025-02-03 20:22:32 -05:00
#| Create a new idris post
multi MAIN(
"new",
"idris",
#| The path to the idris file
IO::Path(Str) $source,
2025-02-03 23:43:19 -05:00
#| The path to the ipkg file
IO::Path(Str) :$ipkg = $default-ipkg,
2025-02-03 20:22:32 -05:00
#| The path of the database file
IO::Path(Str) :$db-dir = $default-db-dir,
#| 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 = read-db $db-dir;
my $id =
$db.insert-post:
IdrisPost.new(
source => $source.absolute.IO,
posted-at => $posted-at,
hidden => $hidden,
2025-02-03 23:43:19 -05:00
ipkg => $ipkg.absolute.IO,
2025-02-03 20:22:32 -05:00
);
$db.write: $db-dir;
say 'Post inserted with id ', $id;
say 'Post has slugs: ', $db.posts{$id}.all-slugs;
}
2025-02-03 21:36:07 -05:00
#| Render the blog to html
multi MAIN(
"render",
#| The path of the database directory
IO::Path(Str) :$db-dir = $default-db-dir,
#| The path of the output directory
IO::Path(Str) :$site-dir = $default-site-dir,
) {
my $db = read-db $db-dir;
$db.render: $site-dir;
}