website/lib/Render/Util.rakumod

70 lines
1.5 KiB
Raku
Raw Normal View History

2025-02-07 02:26:17 -05:00
use v6.e.PREVIEW;
unit module Render::Util;
2025-02-07 02:33:40 -05:00
use DB::Post;
2025-02-07 02:44:35 -05:00
use HTML::Functional;
2025-02-09 05:28:59 -05:00
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>/;
2025-02-09 17:10:46 -05:00
$out ~~ s:g/\s+ ',' \s+ '<span'/, <span/;
2025-02-09 22:27:17 -05:00
# 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">/;
2025-02-09 05:28:59 -05:00
$out
}
2025-02-07 02:26:17 -05:00
sub opt($test, $item) is export {
if $test {
$item
} else {
[]
}
}
sub optl($test, &item) is export {
if $test {
item
} else {
[]
}
}
2025-02-07 02:33:40 -05:00
#| 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"
}
}
2025-02-07 02:44:35 -05:00
sub icon($icon) is export {
i(:class("bx bx-$icon"))
}
2025-02-07 02:54:20 -05:00
2025-02-10 12:27:05 -05:00
sub logo($logo) is export {
i(:class("bx bxl-$logo"))
}
2025-02-07 02:54:20 -05:00
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, $_;
}
}