forked from andre-simon/highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight_pipe.pm
65 lines (48 loc) · 1.51 KB
/
highlight_pipe.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package highlight_pipe;
# This Perl package serves as interface to the highlight utility.
# Input and output streams are handled with pipes.
# Command line parameter length is validated before use.
use IPC::Open3;
my $hl_bin='highlight';
sub new {
my $object = shift;
my $ref = {};
bless($ref,$object);
return($ref);
}
sub getResult {
my $object = shift;
my $src = shift;
my @hl_args = ();
my $option;
while ( my ($key, $value) = each(%$object) ) {
$option =" --$key";
if ($value ne "1") {$option .= "=$value"};
if (length($option)<50) { push (@hl_args, $option); }
}
local(*HIS_IN, *HIS_OUT, *HIS_ERR);
my $childpid = IPC::Open3::open3(\*HIS_IN, \*HIS_OUT, \*HIS_ERR, $hl_bin. join ' ', @hl_args)
or die ("error invoking highlight");
print HIS_IN $src;
close(HIS_IN); # Give end of file to kid.
my @outlines = <HIS_OUT>; # Read till EOF.
my @errlines = <HIS_ERR>; # Read till EOF.
close HIS_OUT;
close HIS_ERR;
waitpid($childpid, 0);
if (@errlines) { die (join '\n', @errlines); }
return join '', @outlines;
}
###############################################################################
# Sample code:
# insert use statement in other perl scripts:
#use highlight_pipe;
my $html = highlight_pipe -> new();
$html->{'syntax'} ='c';
$html->{'fragment'} = 1;
$html->{'inline-css'} = 1;
$html->{'enclose-pre'} = 1;
$html->{'style'} = 'edit-vim';
my $input='int main () { return 0; }';
my $output=$html->getResult($input);
print "$output\n";