Factor out post generation components
This commit is contained in:
parent
87e18dbf60
commit
731d7aa19c
3 changed files with 122 additions and 113 deletions
|
@ -2,122 +2,14 @@ use v6.e.PREVIEW;
|
|||
|
||||
use HTML::Functional;
|
||||
|
||||
use Render::Head;
|
||||
use Render::Util;
|
||||
use Render::Head;
|
||||
use Render::Post;
|
||||
use DB::BlogMeta;
|
||||
use DB::Post;
|
||||
|
||||
unit class Config;
|
||||
|
||||
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<post-time>, :title("Posted At $timestamp"), [
|
||||
icon 'time';
|
||||
' ';
|
||||
$timestamp
|
||||
]
|
||||
}
|
||||
|
||||
method post-edit(Post:D $post) {
|
||||
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("Laste Edited At $timestamp"), [
|
||||
icon 'edit';
|
||||
' ';
|
||||
$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<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;
|
||||
]
|
||||
}
|
||||
|
||||
method post-tag(Str:D $tag) {
|
||||
span :class<post-tag>, [
|
||||
a :href("/tags/$tag.html"), [
|
||||
icon 'hash';
|
||||
$tag;
|
||||
]
|
||||
]
|
||||
}
|
||||
|
||||
method post-tags(Post:D $post){
|
||||
sub intersperse (\element, +list) {
|
||||
gather for list {
|
||||
FIRST .take, next;
|
||||
take slip element, $_;
|
||||
}
|
||||
}
|
||||
my @tags = $post.tags;
|
||||
if @tags {
|
||||
@tags.=map(-> $tag {self.post-tag($tag)});
|
||||
div :class<post-tags>, [
|
||||
icon 'purchase-tag-alt';
|
||||
' ';
|
||||
intersperse(', ', @tags);
|
||||
]
|
||||
} else {
|
||||
[]
|
||||
}
|
||||
}
|
||||
|
||||
method post-info(Post:D $post) {
|
||||
div :class<post-info>, [
|
||||
self.post-date: $post;
|
||||
self.post-edit: $post;
|
||||
self.post-read-time: $post;
|
||||
self.post-tags: $post;
|
||||
];
|
||||
}
|
||||
|
||||
method post-header(Post:D $post) {
|
||||
header :class<post-header>, [
|
||||
div :class<post-title>, [
|
||||
h1 $post.title;
|
||||
];
|
||||
self.post-info: $post;
|
||||
]
|
||||
}
|
||||
|
||||
# TODO: Support GFM admonitions
|
||||
method generate-post(Post:D $post, BlogMeta:D $meta) {
|
||||
my $content = $post.render-html;
|
||||
|
@ -126,7 +18,7 @@ method generate-post(Post:D $post, BlogMeta:D $meta) {
|
|||
body [
|
||||
site-header $meta;
|
||||
article :class<post>, [
|
||||
self.post-header: $post;
|
||||
post-header $post;
|
||||
div :class<post-body>, [
|
||||
$content;
|
||||
]
|
||||
|
@ -153,7 +45,7 @@ method generate-blurb(Int:D $id, $db) {
|
|||
h2 $post.title;
|
||||
];
|
||||
];
|
||||
self.post-info: $post;
|
||||
post-info $post;
|
||||
if $desc ~~ Str:D {
|
||||
div :class<post-blurb-description>, [
|
||||
p $post.description;
|
||||
|
@ -224,7 +116,7 @@ method generate-tag-blurb($db, $tag, $limit?) {
|
|||
a :href($link), span [
|
||||
h3 $post.title;
|
||||
];
|
||||
self.post-info: $post;
|
||||
post-info $post;
|
||||
if $desc ~~ Str:D {
|
||||
div :class<tag-blurb-post-description>, [
|
||||
p $post.description;
|
||||
|
|
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
Reference in a new issue