use v6.e.PREVIEW; unit module Render::Post; use Render::Util; use DB::Post; use DB::Series; use HTML::Functional; sub post-date(Post:D $post) is export { 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 'calendar'; ' '; $datetime.Date.Str ] } sub post-edit(Post:D $post) is export { return [] unless $post.edited-at.elems; my $datetime = $post.edited-at.max; 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("Last Edited At $timestamp"), [ icon 'edit'; ' '; $datetime.Date.Str ] } sub post-read-time(Post:D $post) is export { my ($slow, $average, $fast) = $post.readtimes; div :class, :title, [ icon 'timer'; ' '; mins-to-string $slow; ' '; '/'; ' '; mins-to-string $average; ' '; '/'; ' '; mins-to-string $fast; ] } sub post-tag(Str:D $tag) is export { span :class, [ a :href("/tags/$tag.html"), [ icon 'hash'; span $tag; ] ] } sub post-tags(Post:D $post) is export { my @tags = $post.tags.sort; if @tags { @tags.=map(*.&post-tag); div :class, [ icon 'purchase-tag-alt'; ' '; intersperse(', ', @tags); ] } else { [] } } sub series-tag(Int:D $post-id, Int:D $series-id, Series:D $series) is export { span :class, [ a :href("/series/$series-id.html"), [ icon 'book'; ' '; span :class, [ $series.title; ' '; '('; ($series.post-ids.first($post-id, :k) + 1).Str; '/'; $series.post-ids.elems.Str; ')'; ] ] ] } sub post-series-tags(Int:D $post-id, Post:D $post, $db) is export { # Find all the series this post is in my @series = $db.series.grep(*.value.contains-post: $post-id); if @series { div :class, @series.map(-> $pair { series-tag $post-id, $pair.key, $pair.value }); } else { [] } } sub post-info(Int:D $id, Post:D $post, $db) is export { div :class, [ post-date $post; post-edit $post; post-read-time $post; post-tags $post; post-series-tags $id, $post, $db; ]; } sub post-header(Int:D $id, Post:D $post, $db) is export { header :class, [ div :class, [ h1 $post.title; ]; post-info $id, $post, $db; ] }