Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
998 B

  1. #!/usr/bin/env perl
  2. # Execute shell commands and include their output in the current buffer,
  3. # formatted as an indented code block with the original command, in Markdown.
  4. #
  5. # See also: filter-exec-raw for unformatted output without the original
  6. # command.
  7. use strict;
  8. use warnings;
  9. use 5.10.0;
  10. my $startmarker = '<!-- exec -->';
  11. my $endmarker = '<!-- end -->';
  12. my $text;
  13. {
  14. # line separator:
  15. local $/ = undef;
  16. $text = <>;
  17. }
  18. $text =~ s{$startmarker(.*?)$endmarker}{handle_block($1);}egs;
  19. print $text;
  20. sub handle_block {
  21. my ($block) = @_;
  22. my $cmd;
  23. if ($block =~ m/\$ (.*?)$/m) {
  24. $cmd = $1;
  25. } else {
  26. die "bogus cmd";
  27. }
  28. # Note that we're redirecting stderr here, so it shows up inside the
  29. # output block rather than at the top of the filtered file:
  30. my $result = qx{$cmd 2>&1};
  31. # Indent output by four lines, mostly for the benefit of markdown documents:
  32. $result =~ s/^/ /gm;
  33. return "$startmarker\n\n \$ " . $cmd . "\n" . $result . "\n$endmarker";
  34. }