Dogfood time!
Shell::Piping
has now all the features I had on my list. So it is time to use it. I still need to work on documentation in the form of README.md. It would be nice to render it to HTML without pushing to github. As it turns out there is a ruby gem that takes a fancy Markdown file and outputs a HTML-fragment. I already had a script that embeds HTML into an HTML page with some effort to make it printable.
#! /usr/bin/env raku
use v6;
put q:to/EOH/;
<html>
<head>
<style>
body {
margin: auto; margin-top: 4em; max-width: 80em;
}
@media print {
@page { margin: 4em; }
p { break-inside: avoid; break-before: avoid; }
h3 { break-after: avoid; break-before: avoid; }
h2 { break-after: avoid-page; break-before: auto; }
}
</style>
</head>
<body>
EOH
put $*ARGFILES.slurp;
put ‚ </body>‘;
put ‚</html>‘;
Since this script is taking input from STDIN by the virtue of $*ARGFILES
it lends itself to be part of a unix pipe. Starting such a pipe by hand is way to much work. Writing to README.md creates all the data needed to decide that and how to create a README.html.
#! /usr/bin/env raku
use v6;
use Shell::Piping;
react whenever Supply.merge(Promise.in(0).Supply, ‚.‘.IO.watch) {
say "something changed";
for dir(‚.‘).grep(*.ends-with('.md')) {
my $src = .IO;
my $dst = $src.extension(‚html‘);
if $src.modified > (try $dst.modified // 0) {
my @html;
#`[MARK] px«commonmarker $src» |» px<html-container> |» $dst;
say ‚spurted!‘;
}
}
}
The MARK
ed line saves me about a dozen lines of code not counting error handling. Most errors will produce error messages by having exceptions thrown by Shell::Pipe
. Since we can’t easily have dependencies across language borders, it would be nice to remind my future self what is needed.
CATCH {
when X::Shell::CommandNotFound {
when .cmd ~~ ‚commonmarker‘ {
put ‚Please install commonmarker with `gem install commonmarker`.‘;
exit 2;
}
default {
.rethrow;
}
}
}
I’m specialising an exception so to speak. There is the X::Shell::CommandNotFound
type object and a conditional. If that conditional is not met we use the already provided exception. Otherwise we replace the message with a better one. I believe that is a patten worth thinking about. There may be more boilerplate to remove.
-
August 3, 2020 at 15:172020.31 TwentyTwenty – Rakudo Weekly News