diff --git a/blog b/blog index 5fcc409..e260329 100755 --- a/blog +++ b/blog @@ -22,6 +22,9 @@ my IO::Path:D $default-db-dir = $default-blog-dir.add('db/') }; +#| The default output directory +my IO::Path:D $default-site-dir = $default-blog-dir.add('site/'); + #| Initalize the database multi MAIN( "db", @@ -108,3 +111,15 @@ multi MAIN( say 'Post inserted with id ', $id; 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; +} diff --git a/lib/Config.rakumod b/lib/Config.rakumod new file mode 100644 index 0000000..fc1f057 --- /dev/null +++ b/lib/Config.rakumod @@ -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; + meta :name, :content; + # meta :name, :content; + title ""; + ]; + my $body; + # my $footer; + + die "Not done yet"; + + my $html = html :lang, [ + $head, + $body + ]; + + "$html" +} diff --git a/lib/DB.rakumod b/lib/DB.rakumod index e1d4365..55ab136 100644 --- a/lib/DB.rakumod +++ b/lib/DB.rakumod @@ -11,6 +11,7 @@ use DB::BlogMeta; use DB::MarkdownPost; use DB::IdrisPost; use DB::PlaceholderPost; +use Config; subset PostTypes where MarkdownPost:D | IdrisPost:D | PlaceholderPost:D; @@ -64,8 +65,36 @@ class PostDB { $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 { my $posts-dir = $dir.add('posts/'); die "DB directory does not exist" unless $dir.e;