"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/highlight_pipe.pm" (12 May 2020, 1546 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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