From 2c38910ccd8b64a6c64d2d7b97c128eb441885d8 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Tue, 4 Feb 2025 01:20:57 -0500 Subject: [PATCH] basic idris rendering --- blog | 1 - lib/DB.rakumod | 5 ++++- lib/DB/IdrisPost.rakumod | 47 ++++++++++++++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/blog b/blog index 9b4dd8c..4198bdd 100755 --- a/blog +++ b/blog @@ -104,7 +104,6 @@ multi MAIN( #| Should the post be hidden from the archive? Bool :$hidden = False, ) { - say $default-ipkg; my $db = read-db $db-dir; my $id = $db.insert-post: diff --git a/lib/DB.rakumod b/lib/DB.rakumod index 55ab136..00b70fa 100644 --- a/lib/DB.rakumod +++ b/lib/DB.rakumod @@ -84,7 +84,10 @@ class PostDB { my $id-path = $by-id.add: "$id.html"; $id-path.spurt: $html; for $post.all-slugs -> $slug { - $by-slug.add($slug).symlink: $id-path; + # remove the symlink if it already exists + my $slug-path = $by-slug.add: $slug; + $slug-path.unlink if $slug-path.l; + $id-path.symlink: $slug-path; } } # Render the archive diff --git a/lib/DB/IdrisPost.rakumod b/lib/DB/IdrisPost.rakumod index e36da57..1c0bfbc 100644 --- a/lib/DB/IdrisPost.rakumod +++ b/lib/DB/IdrisPost.rakumod @@ -2,6 +2,7 @@ use v6.e.PREVIEW; use Pandoc; use JSON::Class:auth; +use File::Temp; use DB::Post; @@ -11,7 +12,7 @@ unit class IdrisPost does Post is json(:pretty); #| Marker for disambiguation between post types in json representation, the #| cheaty way -has Bool:D $.idris = True; +has Bool:D $.idris is json = True; #| Location of the ipkg for the package containing the post has IO::Path:D $.ipkg @@ -28,10 +29,42 @@ method title(--> Str:D) { # 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) { - # Do a pack build to make sure we have the needed files - # TODO: Figure out how to only do this once - - # Run through katla - # Send output - die "Not implemented"; + 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 ; + # 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 }