PERL   50

html2ps

Guest on 22nd April 2022 01:13:31 AM

  1. #!/usr/bin/perl
  2. #
  3. # Usage:
  4. #  htmltops -ps [files]    to turn HTML files into postscript
  5. #  htmltops -txt [files]    to turn HTML files into text
  6. # If [files] are omitted, it will do every .html file in the current directory
  7. # If -ps and -txt are omitted, it will default to -ps.
  8. # If an output file already exists and is newer than the corresponding
  9. # input file, it will not be regenerated
  10.  
  11. use strict 'vars';
  12.  
  13. use Cwd ();
  14. my $cwd = Cwd::cwd();
  15. # my $NETSCAPE = '/usr/local/bin/netscape';
  16. # my $NETSCAPE = '/usr/bin/netscape';
  17. my $NETSCAPE = '/usr/bin/netscape-navigator';
  18.  
  19. my %type = ('txt' => 'text', 'ps' => 'PostScript');
  20.  
  21. my $OUTPUT = 'ps';
  22.  
  23. if ($ARGV[0] eq '--') {
  24.   shift;
  25. } elsif ($ARGV =~ /^-(\w+)/) {
  26.   $OUTPUT = $1;
  27.   shift;
  28. }
  29.  
  30. my $type = $type{$OUTPUT};
  31.  
  32. unless (@ARGV) {
  33.   @ARGV = glob('*.html');
  34. }
  35.  
  36. for (@ARGV) {
  37.   my $html = $_;
  38.   $html = "$cwd/$_" unless $html =~ m{^/};
  39.  
  40.   my $out = $html;
  41.   $out =~ s/\.html$//;
  42.   $out =~ s/$/.$OUTPUT/;
  43.  
  44.   if (-f $html && (-M _ < -M $out || ! -e $out)) {
  45.     unlink $out;  # Ignore errors --- this is to prevent a Netscape confirmation dialog
  46.     netscape("openFile($html)");
  47.     netscape("saveAS($out, $type)");
  48.     print STDERR "$_ -> $out\n";
  49.   }
  50. }
  51.  
  52.  
  53.  
  54. sub netscape {
  55.   unless (system($NETSCAPE, '-noraise', '-remote', @_) == 0) {
  56.     die "Couldn't run command @_; aborting ($?).\n";
  57.   }
  58. }

Raw Paste


Login or Register to edit or fork this paste. It's free.