use v6.e.PREVIEW; use HTML::Functional; use DB::BlogMeta; use DB::Post; unit class Config; method generate-head(Str:D $title, BlogMeta:D $meta, $description?) { head [ meta :charset; meta :name, :content; meta :author :content; title "{$meta.title} — $title"; # Add description, if one exists do if $description ~~ Str:D { meta :description :content($description) } else { [] } # Preconnect to all our resource sources link :rel :href; link :rel :href; link :rel :href :crossorigin; link :rel :href; # Load fonts, Iosevka for code, Open Sans for content, and boxicons for # icons link :rel, :href; link :rel, :href; link :rel, :href; # Inline our style sheets style %?RESOURCES.slurp; style %?RESOURCES.slurp; ]; } method site-header(BlogMeta:D $meta) { header :class, [ div :class, [ # TODO: Use a real image here $meta.title ]; div :class, [ $meta.tagline ]; div :class, [ ] ] } method post-date(Post:D $post) { my $datetime = $post.posted-at; my $timestamp = sprintf( "%s %02d:%02d %s", $datetime.yyyy-mm-dd, ($datetime.hour % 12) || 12, $datetime.minute, $datetime.hour < 12 ?? 'am' !! 'pm' ); div :class, :title("Posted At $timestamp"), [ icon 'time'; ' '; $timestamp ] } sub mins-to-string($mins) { if $mins < 60 { $mins.Str ~ "m" } else { my $h = $mins div 60; my $m = $mins mod 60; $h.Str ~ "h" ~ $m.Str ~ "m" } } method post-read-time(Post:D $post) { my ($slow, $average, $fast) = $post.readtimes; div :class, :title, [ icon 'timer'; ' '; mins-to-string $slow; ' '; '/'; ' '; mins-to-string $average; ' '; '/'; ' '; mins-to-string $fast; ] } method post-header(Post:D $post) { header :class, [ div :class, [ h1 $post.title; ]; div :class, [ self.post-date: $post; self.post-read-time: $post; ]; # TODO: Add tags once we have support for that ] } # TODO: Support GFM admonitions method generate-post(Post:D $post, BlogMeta:D $meta) { my $content = $post.render-html; my $head = self.generate-head($post.title, $meta, $post.description); my $body = body [ self.site-header: $meta; article :class, [ self.post-header: $post; div :class, [ $content; ] ] ]; # TODO: Setup footer # my $footer; my $html = html :lang, [ $head, $body ]; "$html" } sub icon($icon) { i(:class("bx bx-$icon")) }