Rendering template

This commit is contained in:
Nathan McCarty 2025-02-03 21:36:07 -05:00
parent abe676f512
commit 815ff4bf4e
3 changed files with 71 additions and 0 deletions

15
blog
View file

@ -22,6 +22,9 @@ my IO::Path:D $default-db-dir =
$default-blog-dir.add('db/') $default-blog-dir.add('db/')
}; };
#| The default output directory
my IO::Path:D $default-site-dir = $default-blog-dir.add('site/');
#| Initalize the database #| Initalize the database
multi MAIN( multi MAIN(
"db", "db",
@ -108,3 +111,15 @@ multi MAIN(
say 'Post inserted with id ', $id; say 'Post inserted with id ', $id;
say 'Post has slugs: ', $db.posts{$id}.all-slugs; say 'Post has slugs: ', $db.posts{$id}.all-slugs;
} }
#| 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;
}

27
lib/Config.rakumod Normal file
View file

@ -0,0 +1,27 @@
use v6.e.PREVIEW;
use HTML::Functional;
use DB::BlogMeta;
unit class Config;
method generate-post(Str:D $title, Str:D $content, BlogMeta:D $meta) {
my $head =
head [
meta :charset<utf-8>;
meta :name<viewport>, :content<width=device-width, initial-scale=1>;
# meta :name<description>, :content<raku does htmx>;
title "";
];
my $body;
# my $footer;
die "Not done yet";
my $html = html :lang<en>, [
$head,
$body
];
"<!doctype html>$html"
}

View file

@ -11,6 +11,7 @@ use DB::BlogMeta;
use DB::MarkdownPost; use DB::MarkdownPost;
use DB::IdrisPost; use DB::IdrisPost;
use DB::PlaceholderPost; use DB::PlaceholderPost;
use Config;
subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D; subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D;
@ -64,8 +65,36 @@ class PostDB {
$posts-dir.add("$key.json").spurt: $value.to-json; $posts-dir.add("$key.json").spurt: $value.to-json;
} }
} }
#| Render the site to the provided output directory
method render(IO::Path:D $out-dir, Config:D :$config = Config.new) {
my $posts = $out-dir.add('posts/');
my $by-id = $posts.add('by-id/');
my $by-slug = $posts.add('by-slug/');
# Make sure the directory structure exists
mkdir $out-dir unless $out-dir.e;
mkdir $posts unless $posts.e;
mkdir $by-id unless $by-id.e;
mkdir $by-slug unless $by-slug.e;
# Render all the posts and make symlinks
for %!posts.kv -> $id, $post {
my $html =
$config.generate-post:
$post.title, $post.render-html, $!meta;
my $id-path = $by-id.add: "$id.html";
$id-path.spurt: $html;
for $post.all-slugs -> $slug {
$by-slug.add($slug).symlink: $id-path;
}
}
# Render the archive
# Render the rss/atom feed
# Render the index
die "Not Implemented"
}
} }
#| 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/');
die "DB directory does not exist" unless $dir.e; die "DB directory does not exist" unless $dir.e;