summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/petri-nets/pn2pdf
blob: 38f2deda1ebfb0f380f46f68d40d85c067f1e761 (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
#!/usr/bin/perl

## This file is part of the Petri-nets packages. See file README for
## copyright notice.

use strict;
use Digest::MD5 qw(md5_hex);

my $keep = 0;

foreach my $job (@ARGV) {
    ##
    ## check argument
    ##
    if ($job =~ /^(-k|--keep)$/) {
	$keep = 1;
	next;
    } elsif ($job =~ /^(-c|--clean)$/) {
	$keep = 0;
	next;
    } elsif ($job =~ /^(-f|--force)$/) {
	unlink "$job.sum" if -f "$job.sum";
	next;
    } elsif ($job =~ /^(-h|--help)$/) {
	print "Usage: pn2pdf [OPTION]... DOC...\n";
	print "Create Petri net figures from DOC.\n\n";
	print "  -k, --keep      keep temporary files\n";
	print "  -c, --clean     delete temporary files\n";
	print "  -f, --force     force the rendering of all pictures\n";
	print "  -h, --help      display this help and exit\n\n";
	print "Report bugs to <pommereau\@univ-paris12.fr>\n";
	exit 0;
    }
    ##
    ## load MD5 sums if available
    ##
    my %sum = ();
    if (-f "$job.sum") {
	open SUM, "$job.sum"
	    or die "pn2pdf: unable to open $job.sum -- $!\n";
	while (<SUM>) {
	    if (/(\d+)\s+(.*)/) {
		$sum{$1} = $2;
	    } else {
		warn "pn2pdf: invalid checksum in $job.sum\n";
	    }
	}
	close SUM;
    }
    ##
    ## process file
    ##
    my $preamble = "";
    open TEX, "$job.tex"
	or die "pn2pdf: unable to open $job.tex -- $!\n";
    open SUM, ">$job.sum"
	or die "pn2pdf: unable to open $job.sum -- $!\n";
    while (<TEX>) {
	$preamble .= $_;
	last if /\\begin\{document\}/;
	$preamble =~ s/(\\begin\{document\}).*$/$1/;
    }
    $preamble .= "\\thispagestyle\{empty\}\n";
    my $count    = 0;
    my $petrinet;
    while (<TEX>) {
	last if /^\\end\{document\}$/;
	if (/(\\begin\{petrinet\}.*)/) {
	    $count++;
	    $petrinet = "$1\n";
	    while (<TEX>) {
		$petrinet .= $_;
		last if /\\end\{petrinet\}/;
	    }
	    $petrinet =~ s/(\\end\{petrinet\}).*$/$1\n/;
	    ##
	    ## create fig-file
	    ##
	    my $md5 = md5_hex ("$preamble$petrinet");
	    print SUM "$count $md5\n";
	    next if ($md5 eq $sum{$count}) && (-f "$job-fig$count.pdf");
	    open FIG, ">$job-fig$count.tex"
		or die "pn2pdf: unable to open $job-fig$count.tex -- $!\n";
	    print FIG "$preamble$petrinet\\end\{document\}\n";
	    close FIG;
	    ##
	    ## process fig-file
	    ##
	    system "latex", "$job-fig$count";
	    system "dvips", ("-E", "-Pcmz", "-Pamz",
			     "-o", "$job-fig$count.eps",
			     "$job-fig$count");
	    system "epstopdf", "$job-fig$count.eps";
	    unlink ("$job-fig$count.tex", "$job-fig$count.log",
		    "$job-fig$count.aux", "$job-fig$count.dvi",
		    "$job-fig$count.eps") unless $keep;
	}
    }
    close TEX;
    close SUM;
}