From a06df0558010ae49746bdd032459b56b17bde180 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Fri, 20 Dec 2024 10:33:05 +0000 Subject: [PATCH] Basic index generation --- src/upload.raku | 61 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/upload.raku b/src/upload.raku index bd3767c..42baff4 100755 --- a/src/upload.raku +++ b/src/upload.raku @@ -3,6 +3,7 @@ use paths; use File::Temp; use DOM::Tiny; +use HTML::Lazy (:ALL); my $build-path; my $dir; @@ -52,6 +53,49 @@ sub render-idris-file($idr) { # Make sure the directory exists and write the contents to it $out-file.dirname.IO.mkdir; $out-file.spurt: $contents; + # Return the out-file + $out-file +} + +# Add a path to our index hash +my method add-path(%index: $path, $value) { + # Split the path up + my @parts = $path.split("/"); + my $head = @parts[0]; + @parts = @parts[1..*-1]; + if @parts.elems == 0 { + %index{$head} = $value; + } else { + if !%index{$head} { + %index{$head} = %(); + } + %index{$head}.&add-path(join('/', @parts), $value); + } +} + +# Render the index to html +my method render-index(%index:) { + my @elements = []; + for %index.keys -> $key { + say ""; + say $key; + say %index{$key} ~~ Str; + say %index{$key}; + say ""; + given %index{$key} { + when Str { + my $x = $_; + @elements.push( + li(Map, + a({ :href($x) }, text($key)))); + } + default { + my $rest = $_.&render-index(); + @elements.push(li(Map, text($key), $rest)); + } + } + } + ul(Map, @elements) } sub MAIN( @@ -84,11 +128,24 @@ sub MAIN( # For each of our idris files, find the ttm, and generate the output in the # temporary directory + my %index = %(); for @idris-files.hyper :batch(1) -> $idr { - render-idris-file $idr; - # TODO : Build index + my $out = render-idris-file $idr; + %index.&add-path($idr, $out.relative($dir)) when $out ~~ IO; } + my $index = %index.&render-index(); + my $title = $*CWD.basename; + $index = html-en + head(Map, + title(Map, text($title)) + ), + body(Map, + $index + ); + + "$dir/index.html".IO.spurt(render($index)); + # Figure out our destination my $dest = do if $dest_dir ~~ Str { $dest_dir