summaryrefslogtreecommitdiff
path: root/context/perltk/mptopdf.pl
blob: e55fb74b264278b7718f785f1df8190b40336340 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
        if 0;

# MikTeX users can set environment variable TEXSYSTEM to "miktex". 

#D \module
#D   [       file=mptopdf.pl,
#D        version=2000.05.29,
#D          title=converting MP to PDF,
#D       subtitle=\MPTOPDF,
#D         author=Hans Hagen,
#D           date=\currentdate,
#D            url=www.pragma-ade.nl,
#D      copyright={PRAGMA ADE / Hans Hagen \& Ton Otten}]
#C
#C This module is part of the \CONTEXT\ macro||package and is
#C therefore copyrighted by \PRAGMA. See licen-en.pdf for
#C details.

# use File::Copy ; # not in every perl 

use Config ;
use Getopt::Long ;
use strict ; 

$Getopt::Long::passthrough = 1 ; # no error message
$Getopt::Long::autoabbrev  = 1 ; # partial switch accepted

my $Help = my $Latex = 0 ; 

&GetOptions
  ( "help"  => \$Help  ,
    "latex" => \$Latex ) ;
 
my $program = "MPtoPDF 1.2" ;
my $pattern = $ARGV[0] ;
my $done    = 0 ;
my $report  = '' ;
my $latexswitch = " --tex=latex --format=latex " ;

## $dosish = ($Config{'osname'} =~ /dos|mswin/i) ;
my $dosish = ($Config{'osname'} =~ /^(ms)?dos|^os\/2|^(ms|cyg)win/i) ;

my $miktex = ($ENV{"TEXSYSTEM"} =~ /miktex/io); 

my @files ; 
my $command = "" ; 

sub CopyFile # agressive copy, works for open files like in gs 
  { my ($From,$To) = @_ ; 
    return unless open(INP,"<$From") ; binmode INP ; 
    return unless open(OUT,">$To") ; binmode OUT ; 
    while (<INP>) { print OUT $_ } 
    close (INP) ; 
    close (OUT) }

if (($pattern eq '')||($Help))
  { print "\n$program : provide MP output file (or pattern)\n" ;
    exit }
elsif ($pattern =~ /\.mp$/io) 
  { shift @ARGV ; my $rest = join(" ", @ARGV) ;  
    if (open(INP,$pattern))
      { while (<INP>) 
          { if (/(documentstyle|documentclass|begin\{document\})/io) 
              { $Latex = 1 ; last } } 
        close (INP) } 
    if ($Latex) 
      { $rest .= " $latexswitch" } 
    my $error = system ("texexec --mptex $rest $pattern") ;
    if ($error) 
      { print "\n$program : error while processing mp file\n" ; exit } 
    else 
      { $pattern =~ s/\.mp$//io ; 
        @files = glob "$pattern.*" } } 
elsif (-e $pattern)
  { @files = ($pattern) }
elsif ($pattern =~ /.\../o)
  { @files = glob "$pattern" }
else
  { $pattern .= '.*' ;
    @files = glob "$pattern" }

foreach my $file (@files)
  { $_ = $file ;
    if (s/\.(\d+|mps)$// && -e $file)
      { if ($miktex) 
          { if ($dosish) 
              { $command = "pdfetex   &mptopdf" } 
            else
              { $command = "pdfetex \\&mptopdf" } }
        else 
#         { $command = "pdfetex -progname=pdfetex -efmt=mptopdf" } 
          { $command = "pdfetex -progname=context -efmt=mptopdf" } 
        if ($dosish)  
          { system ("$command   \\relax $file") }
        else
          { system ("$command \\\\relax $file") }
        rename ("$_.pdf", "$_-$1.pdf") ;
        if (-e "$_.pdf") { CopyFile ("$_.pdf", "$_-$1.pdf") }
        if ($done) { $report .= " +" }
        $report .= " $_-$1.pdf" ;
        ++$done } }

if ($done)
  { print "\n$program : $pattern is converted to$report\n" }
else
  { print "\n$program : no filename matches $pattern\n" }