mason/pastebin/%show: Factor out piping the content through a filter.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 10 Apr 2017 16:28:41 +0000 (17:28 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 7 Jul 2017 20:35:13 +0000 (21:35 +0100)
mason/pastebin/%show

index 9e6afab..cfa0f26 100644 (file)
 </pre>
 % } elsif ($lang =~ /^hl:(.*)$/) {
 <pre class="paste">
-<%perl>
-       my $hl_lang = $1;
-       my $kid = open my $fh, "-|" // die "fork: $!";
-       if ($kid == 0) {
-         open my $hl, "|-", "highlight", "-Ohtml", "-f", "-t8", "-S$hl_lang"
-           or die "open highlight: $!";
-         syswrite $hl, $content // die "highlight write: $!";
-         close $hl or die "highlight kid: $!, $?";
-         exit 0;
-       } else {
-         while (sysread $fh, my $buf, 8192) { $m->print($buf); }
-         close $fh and waitpid $kid, 0
-           or die "highlight parent: $!, $?";
-       }
-</%perl>
+% filter "highlight", $m, $content,
+%   "highlight", "-Ohtml", "-f", "-t8", "-S$1";
 </pre>
 % } else {
 <div class="note">
 %#
 <%once>
        use utf8;
+
+       sub filter ($$$@) {
+         my ($what, $m, $content, @cmd) = @_;
+         my $kid = open my $fh, "-|" // die "fork: $!";
+         if ($kid == 0) {
+           open my $hl, "|-", @cmd or die "open $what: $!";
+           syswrite $hl, $content // die "$what write: $!";
+           close $hl or die "$what kid: $!, $?";
+           exit 0;
+         } else {
+           while (sysread $fh, my $buf, 8192) { $m->print($buf); }
+           close $fh and waitpid $kid, 0
+             or die "$what parent: $!, $?";
+         }
+       }
 </%once>