diff --git a/lib/Pandoc.rakumod b/lib/Pandoc.rakumod index 1cbd52f..fb80e4e 100644 --- a/lib/Pandoc.rakumod +++ b/lib/Pandoc.rakumod @@ -3,16 +3,19 @@ unit module Pandoc; 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 { # Call into pandoc - my $pandoc = run 'pandoc', '-f', 'gfm', '-t', 'JSON', $file, :out, :err; + my $pandoc = run , $file, :out, :err; + + # Collect the output my $output = $pandoc.out.slurp: :close; my $stderr = $pandoc.err.slurp: :close; - - # Throw an error if pandoc failed - if !$pandoc { - die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr"; - } + die "Pandoc exited with {$pandoc.exitcode}\nout: $output\nerr: $stderr" + unless $pandoc; # 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 @@ -51,3 +54,16 @@ sub markdown-title(IO::Path:D $file --> Str:D) is export { 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 , $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 +}