Factor out post generation components
This commit is contained in:
parent
87e18dbf60
commit
731d7aa19c
3 changed files with 122 additions and 113 deletions
100
lib/Render/Post.rakumod
Normal file
100
lib/Render/Post.rakumod
Normal file
|
@ -0,0 +1,100 @@
|
|||
use v6.e.PREVIEW;
|
||||
unit module Render::Post;
|
||||
|
||||
use Render::Util;
|
||||
use DB::Post;
|
||||
|
||||
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<post-time>, :title("Posted At $timestamp"), [
|
||||
icon 'time';
|
||||
' ';
|
||||
$timestamp
|
||||
]
|
||||
}
|
||||
|
||||
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<post-edit>, :title("Last Edited At $timestamp"), [
|
||||
icon 'edit';
|
||||
' ';
|
||||
$timestamp
|
||||
]
|
||||
}
|
||||
|
||||
sub post-read-time(Post:D $post) is export {
|
||||
my ($slow, $average, $fast) = $post.readtimes;
|
||||
div :class<post-read-time>, :title<Estimated read time at 140/180/220 WPM>, [
|
||||
icon 'timer';
|
||||
' ';
|
||||
mins-to-string $slow;
|
||||
' ';
|
||||
'/';
|
||||
' ';
|
||||
mins-to-string $average;
|
||||
' ';
|
||||
'/';
|
||||
' ';
|
||||
mins-to-string $fast;
|
||||
]
|
||||
}
|
||||
|
||||
sub post-tag(Str:D $tag) is export {
|
||||
span :class<post-tag>, [
|
||||
a :href("/tags/$tag.html"), [
|
||||
icon 'hash';
|
||||
$tag;
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
sub post-tags(Post:D $post) is export {
|
||||
my @tags = $post.tags;
|
||||
if @tags {
|
||||
@tags.=map(*.&post-tag);
|
||||
div :class<post-tags>, [
|
||||
icon 'purchase-tag-alt';
|
||||
' ';
|
||||
intersperse(', ', @tags);
|
||||
]
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
sub post-info(Post:D $post) is export {
|
||||
div :class<post-info>, [
|
||||
post-date $post;
|
||||
post-edit $post;
|
||||
post-read-time $post;
|
||||
post-tags $post;
|
||||
];
|
||||
}
|
||||
|
||||
sub post-header(Post:D $post) is export {
|
||||
header :class<post-header>, [
|
||||
div :class<post-title>, [
|
||||
h1 $post.title;
|
||||
];
|
||||
post-info $post;
|
||||
]
|
||||
}
|
|
@ -35,3 +35,20 @@ sub post-link(Int:D $id, Post:D $post --> Str:D) is export {
|
|||
sub icon($icon) is export {
|
||||
i(:class("bx bx-$icon"))
|
||||
}
|
||||
|
||||
sub mins-to-string($mins) is export {
|
||||
if $mins < 60 {
|
||||
$mins.Str ~ "m"
|
||||
} else {
|
||||
my $h = $mins div 60;
|
||||
my $m = $mins mod 60;
|
||||
$h.Str ~ "h" ~ $m.Str ~ "m"
|
||||
}
|
||||
}
|
||||
|
||||
sub intersperse (\element, +list) is export {
|
||||
gather for list {
|
||||
FIRST .take, next;
|
||||
take slip element, $_;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue