#!/usr/bin/env perl
|
|
|
|
# Execute shell commands and include their output in the current buffer,
|
|
# formatted as an indented code block with the original command, in Markdown.
|
|
#
|
|
# See also: filter-exec-raw for unformatted output without the original
|
|
# command.
|
|
|
|
use strict;
|
|
use warnings;
|
|
use 5.10.0;
|
|
|
|
my $startmarker = '<!-- exec -->';
|
|
my $endmarker = '<!-- end -->';
|
|
|
|
my $text;
|
|
{
|
|
# line separator:
|
|
local $/ = undef;
|
|
$text = <>;
|
|
}
|
|
|
|
$text =~ s{$startmarker(.*?)$endmarker}{handle_block($1);}egs;
|
|
|
|
print $text;
|
|
|
|
sub handle_block {
|
|
my ($block) = @_;
|
|
|
|
my $cmd;
|
|
|
|
if ($block =~ m/\$ (.*?)$/m) {
|
|
$cmd = $1;
|
|
} else {
|
|
die "bogus cmd";
|
|
}
|
|
|
|
my $result = `$cmd`;
|
|
$result =~ s/^/ /gm;
|
|
|
|
return "$startmarker\n\n \$ " . $cmd . "\n" . $result . "\n$endmarker";
|
|
}
|