2025-01-22 20:53:52 -05:00
|
|
|
use v6.e.PREVIEW;
|
|
|
|
|
|
|
|
use Pandoc;
|
|
|
|
use JSON::Class:auth<zef:vrurg>;
|
2025-02-04 01:20:57 -05:00
|
|
|
use File::Temp;
|
2025-01-22 20:53:52 -05:00
|
|
|
|
|
|
|
use DB::Post;
|
|
|
|
|
|
|
|
#| A literate, markdown, idris post
|
|
|
|
|
2025-02-02 17:57:27 -05:00
|
|
|
unit class IdrisPost does Post is json(:pretty);
|
2025-01-22 20:53:52 -05:00
|
|
|
|
|
|
|
#| Marker for disambiguation between post types in json representation, the
|
|
|
|
#| cheaty way
|
2025-02-04 01:20:57 -05:00
|
|
|
has Bool:D $.idris is json = True;
|
2025-01-22 20:53:52 -05:00
|
|
|
|
2025-02-03 23:43:19 -05:00
|
|
|
#| 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)
|
|
|
|
);
|
|
|
|
|
2025-01-22 20:53:52 -05:00
|
|
|
method title(--> Str:D) {
|
|
|
|
markdown-title($!source)
|
|
|
|
}
|
2025-02-03 20:41:31 -05:00
|
|
|
|
|
|
|
# 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) {
|
2025-02-04 01:20:57 -05:00
|
|
|
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;
|
|
|
|
}
|
2025-02-03 23:43:19 -05:00
|
|
|
|
2025-02-04 01:20:57 -05:00
|
|
|
# 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
|
2025-02-03 20:41:31 -05:00
|
|
|
}
|