From a294515c1d2490a430ad6b947525b4df992f9438 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Mon, 27 Jan 2025 03:10:50 -0500 Subject: [PATCH] Basic book building --- scripts/build-book | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 scripts/build-book diff --git a/scripts/build-book b/scripts/build-book new file mode 100755 index 0000000..8dd718f --- /dev/null +++ b/scripts/build-book @@ -0,0 +1,81 @@ +#!/usr/bin/env raku + +use File::Temp; +use Shell::Command; +use paths; + + +my $tempdir = tempdir.IO; +my $ttc-number = dir('build/ttc').first.basename; +my $ttc = "build/ttc/$ttc-number".IO; + +# Filenames to ignore while processing source files +my Str:D @ignored = ["README.md", "SUMMARY.md"]; +# Check to see if a filename is ignored +sub not-ignored($path) { + for @ignored -> $ignored { + return False if $path.ends-with: $ignored; + } + return True; +} + +# Copy a file from the current directory to the temporary directory, preserving +# realtive path. Resolves symlinks in source, but does not reflect symlink +# resoultion in the output path +sub cp-temp($src) { + my $src-path = do given $src { + when Str { + $src.IO + } + when IO::Path { + $src + } + default { + die "Invalid source $src, {$src.WHAT}" + } + } + my $output-path = $tempdir.add($src-path.relative).IO; + # Create the parent directory if needed + if !$output-path.parent.d { + $output-path.parent.mkdir; + + } + # Copy the file + $src-path.resolve.copy: $output-path; +} + +# Invoke katla on a source file, streaming its output to the temporary directory +sub katla($src, $ttc-src) { + # TODO: Post process them to set themeing correctly + # Run katla and collect the output + my $katla = run 'katla', 'markdown', $src, $ttc-src, :out; + my $output = $katla.out.slurp(:close); + # Spurt the output to the temporary directory + my $output-path = $tempdir.add: $src; + if !$output-path.parent.d { + $output-path.parent.mkdir; + } + $output-path.spurt($output); +} + +# Copy our metadata files +cp-temp "book.toml"; +cp-temp "src/README.md"; +cp-temp "src/SUMMARY.md"; + +# Katla over the source files +for paths("src", :file(*.¬-ignored)) -> $path { + my $ttc-path = $ttc.add($path.IO.relative: "src").extension: "ttm"; + katla $path.IO.relative, $ttc-path.relative; +} + +# Build the book + +indir $tempdir, { + my $mdbook = run ; + die "Ooops" unless $mdbook; +} + +# Copy it over +rm_rf "book"; +cp $tempdir.add("book"), "book", :r;