2025-01-27 08:10:50 +00:00
|
|
|
#!/usr/bin/env raku
|
|
|
|
|
|
|
|
use File::Temp;
|
|
|
|
use Shell::Command;
|
|
|
|
use paths;
|
|
|
|
|
2025-01-27 08:29:53 +00:00
|
|
|
unit sub MAIN(Bool :$upload);
|
2025-01-27 08:10:50 +00:00
|
|
|
|
|
|
|
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
|
2025-01-28 02:02:07 +00:00
|
|
|
sub copy-to-dest($src) {
|
2025-01-27 08:10:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-01-28 02:02:07 +00:00
|
|
|
# Special handling for our readme file, we need to butcher up it's links
|
|
|
|
my $readme-contents = 'README.md'.IO.slurp;
|
|
|
|
$readme-contents ~~ s:g/'src/'//;
|
|
|
|
my $readme-dest = $tempdir.add('src/README.md');
|
|
|
|
$readme-dest.parent.mkdir;
|
|
|
|
$readme-dest.spurt: $readme-contents;
|
2025-01-27 08:10:50 +00:00
|
|
|
|
|
|
|
# Copy our metadata files
|
2025-01-28 02:02:07 +00:00
|
|
|
copy-to-dest "book.toml";
|
|
|
|
copy-to-dest "src/SUMMARY.md";
|
2025-01-27 08:10:50 +00:00
|
|
|
|
|
|
|
# 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 <mdbook build>;
|
|
|
|
die "Ooops" unless $mdbook;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copy it over
|
|
|
|
rm_rf "book";
|
|
|
|
cp $tempdir.add("book"), "book", :r;
|
2025-01-27 09:45:38 +00:00
|
|
|
|
|
|
|
if $upload {
|
2025-01-29 08:35:21 +00:00
|
|
|
my $rsync = run 'rsync', '-avzh', $tempdir.add("book/").Str,
|
|
|
|
'ubuntu@static.stranger.systems:/var/www/static.stranger.systems/idris-by-contrived-example';
|
|
|
|
die "rsync went bad" unless $rsync;
|
2025-01-27 09:45:38 +00:00
|
|
|
}
|
2025-01-28 02:02:07 +00:00
|
|
|
|
|
|
|
# This function goes at the end because it breaks emacs fontification after it
|
|
|
|
# for some bizzare reason.
|
|
|
|
#
|
|
|
|
# Invoke katla on a source file, streaming its output to the temporary directory
|
|
|
|
sub katla($src, $ttc-src) {
|
|
|
|
# Run katla and collect the output
|
|
|
|
my $katla = run 'katla', 'markdown', $src, $ttc-src, :out;
|
|
|
|
my $output = $katla.out.slurp(:close);
|
|
|
|
# Post process them to set themeing correctly
|
|
|
|
# TODO: We need to remove the extra new line after the start of code blocks
|
|
|
|
# still
|
|
|
|
$output ~~ s:g/'<style>' .* '</style>'//;
|
|
|
|
$output ~~ s:g/'<br />'//;
|
|
|
|
$output ~~ s:g/'\\*'/*/;
|
|
|
|
$output ~~ s:g/'\\_'/_/;
|
|
|
|
$output ~~ s:g/'\\\\'/\\/;
|
|
|
|
$output ~~ s:g/'<code'/<pre><code/;
|
|
|
|
$output ~~ s:g/'</code>'/<\/code><\/pre>/;
|
|
|
|
$output ~~ s:g/'="IdrisKeyword"'/="hljs-keyword"/;
|
|
|
|
$output ~~ s:g/'="IdrisModule"'/="hljs-symbol hljs-emphasis"/;
|
|
|
|
$output ~~ s:g/'="IdrisComment"'/="hljs-comment"/;
|
|
|
|
$output ~~ s:g/'="IdrisFunction"'/="hljs-symbol"/;
|
|
|
|
$output ~~ s:g/'="IdrisBound"'/="hljs-name"/;
|
|
|
|
$output ~~ s:g/'="IdrisData"'/="hljs-title"/;
|
|
|
|
$output ~~ s:g/'="IdrisType"'/="hljs-type"/;
|
|
|
|
$output ~~ s:g/'="IdrisNamespace"'/="hljs-symbol hljs-emphasis"/;
|
|
|
|
|
|
|
|
# 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);
|
|
|
|
}
|