website/lib/Render/Util.rakumod

82 lines
2 KiB
Raku

use v6.e.PREVIEW;
unit module Render::Util;
use DB::Post;
use DB::BlogMeta;
use HTML::Functional;
sub show-html($html) is export {
my $out = "<!doctype html>$html";
# Work around HTML::Functional automatically putting newlines between tags
$out ~~ s:g/'</i>' \v+ '<span>'/<\/i><span>/;
$out ~~ s:g/\v+ '</a>'/<\/a>/;
$out ~~ s:g/\s+ ',' \s+ '<span'/, <span/;
# Fixup unit test divs
$out ~~ s:g/'<div class="unit-test">' \s* '<pre><code class="idris-code">&nbsp;&nbsp;'/<div class="unit-test"><span><i class='bx bx-check-circle'><\/i>Unit Test<\/span><pre><code class="idris-code">/;
$out ~~ s:g/'<div class="warn-unit-test">' \s* '<pre><code class="idris-code">&nbsp;&nbsp;'/<div class="unit-test"><span><i class='bx bx-info-circle'><\/i>Unit Test<\/span><pre><code class="idris-code">/;
$out
}
sub opt($test, $item) is export {
if $test {
$item
} else {
[]
}
}
sub optl($test, &item) is export {
if $test {
item
} else {
[]
}
}
#| Link to the post by the primary slug, if there is one, linking to it by id
#| otherwise
sub post-link(Int:D $id, Post:D $post --> Str:D) is export {
my @slugs = $post.all-slugs;
if @slugs {
"/posts/by-slug/{@slugs[*-1]}.html"
} else {
"/posts/by-id/$id.html"
}
}
sub icon($icon) is export {
i(:class("bx bx-$icon"))
}
sub logo($logo) is export {
i(:class("bx bxl-$logo"))
}
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, $_;
}
}
# get the link for a post
sub post-link-abs(BlogMeta:D $meta, Int:D $id, Post:D $post --> Str:D) is export {
my @slugs = $post.all-slugs;
my $base = $meta.get-base-url;
if @slugs.elems {
"$base/posts/by-slug/{@slugs[*-1]}.html"
} else {
"$base/posts/by-id/$id.html"
}
}