summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/metapost-examples/mp2html.pl
blob: 143e2314e0a691e9fc5a1b78aaac9d9def09712c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! perl -w

#
# Builds an HTML file and a bunch of GIF files from a Metapost file
# Also creates a PS file
#
# (c) Vincent Zoonekynd <zoonek@math.jussieu.fr>
# August 1999
# modified in august 2001
# distributed under the GPL
#

$main = $ARGV[0] || "examples";
$main =~ s/\.mp$//i;

# On crée les images GIF
# BUG : à chaque fois qu'il y a des fontes, ça plante.
# C'est normal pour deux raisons :
#  - si j'utilisais prologuse:=2, il ne trouverait pas les fontes.
#  - Comme j'utilise prologues:=0, c'est encore pire...
# L'idéal serait de créer un fichier LaTeX pour chaque fichier postscript,
# de lancer later puis dvips dessus et de récupérer le fichier postscript.

# 1. Compile the examples

system "TEX=latex mpost --interaction=nonstopmode $main";

# 2. Create a LaTeX file encompassing the examples and compile it

open(LATEX,'>', "$main.tex") || die "Cannot open examples.tex for writing: $!";
print LATEX '\documentclass[a4paper]{article}' ."\n";
print LATEX '\usepackage{graphicx}' ."\n";
print LATEX '\begin{document}' ."\n";
print LATEX '\begin{verbatim}' ."\n";
open(MP, "<", "$main.mp") || die "cannot open $main.mp for reading: $!";
while(<MP>){
  if (m/^\s*beginfig\s*\((.*)\)/) {
    print LATEX '\end{verbatim}' . "\n";
    print LATEX "\\includegraphics{${main}_$1.mps}\n";
    print LATEX '\begin{verbatim}' . "\n";
    print LATEX $_;
  } elsif (m/^\s*endfig/) {
    print LATEX $_;
    print LATEX '\end{verbatim}' ."\n";
    print LATEX '\hrulefill' . "\n";
    print LATEX '\begin{verbatim}' . "\n";
  } else {
    print LATEX $_;
  }
}
print LATEX '\end{verbatim}' . "\n";
print LATEX '\end{document}' ."\n";
close MP;
close LATEX;

opendir(DIR,"./") || die "Cannot open ./ directory for reading : $!";
foreach $file (readdir DIR) {
  if ($file =~ m/^$main.[0-9]+$/) {
    my $new = $file;
    $new =~ s/\./_/g;
    $new .= ".mps";
    symlink $file, $new;
  }
}
closedir(DIR);

system "latex    --interaction=nonstopmode $main.tex";
system "dvips -o $main.ps $main.dvi";
#system "pdflatex --interaction=nonstopmode $main.tex";

# 3. Create the GIF pictures

opendir(DIR,"./") || die "Cannot open ./ directory for reading : $!";
foreach $file (readdir DIR) {
  if ($file =~ m/^$main.[0-9]+$/) {
    
    ## Création du fichier PS
    symlink "$file", "$file.eps";
    open(TEX,">$file.tex") || die "cannot open $file.tex for writing : $!";
    print TEX '\nonstopmode'                                          ."\n" ;
    print TEX '\documentclass[a4paper,10pt]{article}'                 ."\n" ;
    print TEX '\usepackage[T1]{fontenc}\usepackage[latin1]{inputenc}' ."\n" ;
    print TEX '\usepackage{graphicx,amsmath,amssymb}'                 ."\n" ;
    print TEX '\pagestyle{empty}'                                     ."\n" ;    
    print TEX '\begin{document}'                                      ."\n" ;
    print TEX '\includegraphics{'. "$file.eps" .'}'                   ."\n" ;
    print TEX '\end{document}'                                        ."\n" ;
    close TEX;
    system "latex $file.tex";
    system "dvips -E -f $file.dvi -o $file.ps";
    
    ## Transformation en GIF
    my ($bbx,$bby,$bbw,$bbh);
    open(PS,"$file.ps");
    while (<PS>) {
      if (/^%%BoundingBox:\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/) {
        $bbx = 0-$1;    $bby = 0-$2;
        $bbw = $3+$bbx;    $bbh = $4+$bby;
#	print "*** Seen BBOX\n";
      }
      last if /^%%EndComments/;
    }
    close(PS);

    my $scale = 3;
    my $density = 72*$scale;
    $bbw = $scale * $bbw;
    $bbh = $scale * $bbh;
#    print "*** gs -q -dNOPAUSE -dNO_PAUSE -sDEVICE=ppmraw -g${bbw}x${bbh} -r$density -sOutputFile=$file.ppm\n";
    open(GS, "|gs -q -dNOPAUSE -dNO_PAUSE -sDEVICE=ppmraw -g${bbw}x${bbh} -r$density -sOutputFile=$file.ppm");
    print GS "$bbx $bby translate\n";
    print GS "($file.ps) run\n";
    print GS "showpage\n";
    print GS "quit\n";
    close(GS);

    system("pnmcrop $file.ppm | ppmquant 256 | ppmtogif > $file.gif");

    unlink "$file.ppm";
  }
}


# 4. Create the HTML file
open(MP,"$main.mp") || die "cannot open $main.mp for reading: $!";
open(HTML,">$main.html") || die "cannot open $main.html for writing: $!";
select HTML;
print "<HTML><HEAD><TITLE>Metapost : exemples</TITLE></HEAD><BODY>\n";
print "<H1> Métapost : exemples </H1>\n";
print "<HR><PRE>\n";
while(<MP>){
  if (m/^\s*beginfig\s*\((.*)\)/) {
    print "</PRE><IMG SRC=\"$main.$1.gif\"><PRE>\n";
    print ;
  } elsif (m/^\s*endfig/) {
    print;
    print "</PRE><HR><PRE>\n";
  } else {
    print;
  }
}
print "</PRE>\n";
print "</BODY></HTML>\n";
close HTML;  
close MP;  

# 5. Remove unnecessary files

opendir(DIR,"./") || die "Cannot open ./ directory for reading : $!";
foreach $file (readdir DIR) {
  if ($file =~ m/^$main.[0-9]+$/) {
    unlink $file;
    unlink "$file.tex";
    unlink "$file.dvi";
    unlink "$file.aux";
    unlink "$file.log";
    unlink "$file.eps";
    unlink "$file.ps";
    my $new = $file;
    $new =~ s/\./_/g;
    $new .= ".mps";
    unlink $new;
  } elsif( $file =~ m/\.mpx$/ ) {
    unlink $file;
  }
}
closedir DIR;
unlink "$main.aux";
unlink "$main.log";