pandoc generate html method

This commit is contained in:
Nathan McCarty 2025-02-03 20:53:32 -05:00
parent 72607c7f25
commit abe676f512

View file

@ -3,16 +3,19 @@ unit module Pandoc;
use JSON::Fast; use JSON::Fast;
#| Extract the title from a markdown document
#|
#| The title is the only top level header, will throw an error if there are
#| multiple top level headers or are none
sub markdown-title(IO::Path:D $file --> Str:D) is export { sub markdown-title(IO::Path:D $file --> Str:D) is export {
# Call into pandoc # Call into pandoc
my $pandoc = run 'pandoc', '-f', 'gfm', '-t', 'JSON', $file, :out, :err; my $pandoc = run <pandoc -f gfm -t JSON>, $file, :out, :err;
# Collect the output
my $output = $pandoc.out.slurp: :close; my $output = $pandoc.out.slurp: :close;
my $stderr = $pandoc.err.slurp: :close; my $stderr = $pandoc.err.slurp: :close;
die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr"
# Throw an error if pandoc failed unless $pandoc;
if !$pandoc {
die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr";
}
# Parse out output from pandoc, we are making an executive decision to trust # Parse out output from pandoc, we are making an executive decision to trust
# pandoc here, so we won't do any error handling for pandoc's output # pandoc here, so we won't do any error handling for pandoc's output
@ -51,3 +54,16 @@ sub markdown-title(IO::Path:D $file --> Str:D) is export {
return $title; return $title;
} }
#| Use pandoc to render a markdown document to html
sub markdown-to-html(IO::Path:D $file --> Str:D) is export {
# Call into pandoc
my $pandoc = run <pandoc -f gfm>, $file, :out, :err;
# Collect the output
my $output = $pandoc.out.slurp: :close;
my $stderr = $pandoc.err.slurp: :close;
die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr"
unless $pandoc;
$output
}