From b16468fbeaddea235a0ef105997b701e24eccefd Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Mon, 10 Apr 2017 17:28:41 +0100 Subject: [PATCH] mason/pastebin/%show: Factor out piping the content through a filter. --- mason/pastebin/%show | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/mason/pastebin/%show b/mason/pastebin/%show index 9e6afab..cfa0f26 100644 --- a/mason/pastebin/%show +++ b/mason/pastebin/%show @@ -25,21 +25,8 @@ % } elsif ($lang =~ /^hl:(.*)$/) {
-<%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: $!, $?";
-	}
-
+% filter "highlight", $m, $content,
+%   "highlight", "-Ohtml", "-f", "-t8", "-S$1";
 
% } else {
@@ -68,4 +55,19 @@ %# <%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: $!, $?"; + } + } -- 2.11.0