website/lib/Config.rakumod

175 lines
4.1 KiB
Raku

use v6.e.PREVIEW;
use HTML::Functional;
use Render::Util;
use Render::Head;
use Render::Post;
use DB::BlogMeta;
use DB::Post;
unit class Config;
method generate-post(Int:D $id, Post:D $post, $db) {
my $meta = $db.meta;
my $content = $post.render-html;
my $head =
head [
generate-head($meta, $post.title, $post.description);
meta :property<og:title>, :content($post.title);
meta :property<og:url>, :content(post-link-abs $db.meta, $id, $post);
meta :property<og:site_name>, :content($db.meta.title);
optl $post.description ~~ Str:D,
-> {meta :property<og:description>, :content(^ $post.description)};
meta :property<og:type>, :content<article>;
meta :property<article:published_time>, :content($post.posted-at);
$post.tags.map(-> $tag {
meta :property<article:tag>, :content($tag)
});
];
my $body =
body [
site-header $meta;
article :class<post>, [
post-header $id, $post, $db;
div :class<post-body>, [
$content;
]
]
];
# TODO: Setup Comments
# TODO: Setup footer
# my $footer;
my $html = html :lang<en>, [
$head,
$body
];
show-html $html
}
method generate-index($db) {
my @most-recent =
$db.sorted-posts
.head(10)
.grep(!*.value.hidden)
.map(-> $pair {
generate-blurb $pair.key, $db
});
my $head = head [generate-head($db.meta);];
my $body = body [
site-header $db.meta;
div :class<post-blurbs>, [
h1 "Recent Posts"
], @most-recent;
];
my $html =
html :lang<en>, [
$head,
$body
];
show-html $html
}
method generate-archive($db) {
my @most-recent =
$db.sorted-posts
.grep(!*.value.hidden)
.map(-> $pair {
generate-blurb $pair.key, $db
});
my $head = head [generate-head($db.meta);];
my $body = body [
site-header $db.meta;
div :class<post-blurbs>, [
h1 "All Posts"
], @most-recent;
];
my $html =
html :lang<en>, [
$head,
$body
];
show-html $html
}
method generate-tag-blurb($db, $tag, $limit?) {
sub post-to-link($id, $post) {
my $desc = $post.description;
my $link = post-link $id, $post;
div :class<tag-blurb-post>, [
div :class<tag-blurb-post-title>, [
a :href($link), span [
h3 $post.title;
];
post-info $id, $post, $db;
if $desc ~~ Str:D {
div :class<tag-blurb-post-description>, [
p $post.description;
];
} else {
[]
}
];
]
}
my @posts = $db.sorted-posts.grep(-> $a { $tag (elem) $a.value.tags });
if $limit {
@posts.=head($limit);
}
if @posts {
div :class<tag-blurb>, [
span :class<tag-blurb-title>, [
a :href("/tags/$tag.html"), [
icon 'hash';
$tag;
];
];
div :class<tag-blurb-links>,
@posts.map(-> $a {post-to-link $a.key, $a.value});
]
} else {
[]
}
}
method generate-tags-page($db, @tags) {
my $head = head [generate-head($db.meta);];
my $body = body [
site-header $db.meta;
div :class<tags>, [
h1 "Tags";
], @tags.map(-> $tag {self.generate-tag-blurb($db, $tag, 4)});
];
my $html =
html :lang<en>, [
$head,
$body
];
show-html $html
}
method generate-tag-page($db, $tag) {
my $head = head [generate-head($db.meta);];
my $body = body [
site-header $db.meta;
self.generate-tag-blurb($db, $tag, 4);
];
my $html =
html :lang<en>, [
$head,
$body
];
show-html $html
}