website/lib/DB/MarkdownPost.rakumod

48 lines
1.1 KiB
Raku

use v6.e.PREVIEW;
use Pandoc;
use Pygments;
use DB::Post;
use JSON::Class:auth<zef:vrurg>;
use File::Temp;
#| A plain markdown post
unit class MarkdownPost does Post is json(:pretty);
#| Marker for disambiguation between post types in json representation, the
#| cheaty way
has Bool:D $.markdown = True;
#| Override the generated description for this post
has Str $.summary;
method title(--> Str:D) {
markdown-title $!source
}
# Simply provide our source file to pandoc
method render-html(--> Str:D) {
# Test to see if this posts contains any fenced code blocks, if so,
# pygmentize it through a temporary file
my $contents = $!source.slurp;
if $contents ~~ /'```'/ {
my $output = highlight-code $contents;
my ($filename, $filehandle) = tempfile;
$filehandle.spurt: $output, :close;
markdown-to-html $filename.IO
} else {
markdown-to-html $!source
}
}
# Return our summary, if we have one, otherwise extract the first paragraph of
# the markdown document
method description(--> Str) {
if $!summary {
$!summary
} else {
markdown-first-paragraph $!source
}
}