website/lib/DB/IdrisPost.rakumod

70 lines
2.1 KiB
Raku

use v6.e.PREVIEW;
use Pandoc;
use JSON::Class:auth<zef:vrurg>;
use File::Temp;
use DB::Post;
#| A literate, markdown, idris post
unit class IdrisPost does Post is json(:pretty);
#| Marker for disambiguation between post types in json representation, the
#| cheaty way
has Bool:D $.idris is json = True;
#| Location of the ipkg for the package containing the post
has IO::Path:D $.ipkg
is required
is json(
:to-json(*.Str),
:from-json(*.IO)
);
method title(--> Str:D) {
markdown-title($!source)
}
# Use katla to highlight our file, mangle the resulting output, and then pass it
# through to pandoc for html generation
method render-html(--> Str:D) {
my $project-dir = $!ipkg.parent;
indir $project-dir, {
# Do a pack build to make sure we have the needed files
# TODO: Figure out how to only do this once
pack <build>;
# First we have to find the location of the ttm file
my $relative-path = $!source.extension('ttm').relative:
$project-dir.add('src');
my $ttc-dir = dir($project-dir.add('build/ttc/'))[0];
my $ttm-path = $ttc-dir.add: $relative-path;
# Run through katla
my $output = katla 'markdown', $!source, $ttm-path;
# Send output to pandoc through a temporary file
my ($filename, $filehandle) = tempfile;
$filehandle.spurt: $output, :close;
markdown-to-html $filename.IO
};
}
# Run a pack command, erroring on failure
sub pack(*@args) {
my $pack = run 'pack', @args, :out, :err;
my $pack-out = $pack.out.slurp: :close;
my $pack-err = $pack.err.slurp: :close;
die "Pack exited with {$pack.exitcode}\nout: $pack-out\nerr: $pack-err"
unless $pack;
}
# Run a katla command, erroring on failure, returning the standard out, and
# mangling the output accordingly
sub katla(*@args --> Str:D) {
my $katla = run 'katla', @args, :out, :err;
my $katla-out = $katla.out.slurp: :close;
my $katla-err = $katla.err.slurp: :close;
die "Katla exited with {$katla.exitcode}\nout: $katla-out\nerr: $katla-err"
unless $katla;
# TODO modify output
$katla-out
}