-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-examples
More file actions
executable file
·284 lines (258 loc) · 7.54 KB
/
make-examples
File metadata and controls
executable file
·284 lines (258 loc) · 7.54 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#! /usr/bin/perl
sub sys {
my $cmd = shift;
print("$cmd\n");
return system($cmd);
}
sub source_highlight {
my $infile = shift;
my $outfile = shift;
my $hitext = "";
open(SI, "source-highlight -i $infile |");
while (<SI>) { $hitext .= $_; }
close(SI);
if ($outfile) {
open(SO, ">$outfile");
print SO $hitext;
close(SO);
} else {
return $hitext;
}
}
##
## clean out the html/examples dir by remove and recreating it
##
system("/bin/rm -rf html/examples");
mkdir("html/examples");
##
## find all the directories containing an "example.txt" file in the html/tests directory tree
##
@testdirs = ();
open(FIND , "find html/tests -name example.txt|");
while (chomp($file = <FIND>)) {
$dir = $file;
$dir =~ s|html/tests/||;
$dir =~ s|/[^/]+$||;
push(@testdirs, $dir);
}
close(FIND);
@testdirs = sort(@testdirs);
@tests = ();
foreach $dir (@testdirs) {
$test = parse_example_txt("html/tests/$dir/example.txt");
$test->{dir} = $dir;
push(@tests, $test);
}
##
## create each example (each "test" becomes an "example"):
##
for ($i=0; $i<@tests; ++$i) {
$test = $tests[$i];
$dir = $test->{dir};
$prev_test = $i > 0 ? $tests[$i-1] : undef;
$next_test = $i < @tests ? $tests[$i+1] : undef;
print "creating example '$dir' ...";
if ($test->{htmlfile}) {
$htmlfile = $test->{htmlfile};
# if there's an html file specified in the example.txt file, parse it
$html = parse_mg_html("html/tests/$dir/$htmlfile");
} else {
# otherwise, example all html file in the dir, and use the first one found
# that seems to contain a multigraph graph
foreach $f (<html/tests/$dir/*>) {
if ($f =~ /\.html$/) {
$html = parse_mg_html($f);
if ($html && $html->{graphs} && @{$html->{graphs}}) {
$htmlfile = $f;
$htmlfile =~ s|^html/tests/$dir/||;
last;
}
$html = undef;
}
}
}
# if we didn't find an html file for this test, skip it
if (!$html) { next; }
# create the dir for this test
system("mkdir -p html/examples/$dir");
# keep track of which files we've copied
$file_was_copied = {};
# copy the html file
#system("cp 'html/tests/$dir/$htmlfile' 'html/examples/$dir/frameless-$htmlfile'");
decorate_html_file("html/tests/$dir/$htmlfile", "html/examples/$dir/frameless-$htmlfile",
$test, $prev_test, $next_test);
$file_was_copied->{$htmlfile} = 1;
# copy the mugl file(s), and create source-highlighted version(s):
foreach $g (@{$html->{graphs}}) {
$mugl = $g->{mugl};
system("cp 'html/tests/$dir/$mugl' 'html/examples/$dir'");
$file_was_copied->{$mugl} = 1;
source_highlight("html/tests/$dir/$mugl", "html/examples/$dir/$mugl.html");
}
# create index.html file
create_html_frames_file("html/examples/$dir/index.html",
$test,
$html->{graphs}->[0]->{height}+120,
"*",
"frameless-$htmlfile",
$html->{graphs}->[0]->{mugl}.".html");
# copy all other files
$excludes = "--exclude='*.svn*' --exclude='*~' "
. join(" ", map { "--exclude=$_" } keys(%$file_was_copied));
system("(cd html/tests/$dir ; tar -c -f - $excludes . ) | (cd html/examples/$dir ; tar xf -)");
print " done.\n";
}
##
## create the example index file
##
open(OUT, ">html/examples/index.html");
print OUT qq|<html>
<head>
<title>Multigraph Examples</title>
</head>
<body>
<h2>Multigraph Examples</h2>
<ul>
|;
foreach $test (@tests) {
printf(OUT qq|<li><a href="%s">%s</a>\n|,
$test->{dir},
$test->{title});
}
print OUT qq|
</ul>
</body>
</html>
|;
close(OUT);
exit;
sub count_slashes {
my $x = shift;
my @x = split("", $x);
my $count = 0;
foreach my $c (@x) {
if ($c eq "/") { ++$count; }
}
return $count;
}
sub decorate_html_file {
my $infile = shift;
my $outfile = shift;
my $this_test = shift;
my $prev_test = shift;
my $next_test = shift;
my $level = count_slashes($this_test->{dir}) + 1;
my $homedir = "../"x$level;
my $line;
open(DECORATE_HTML_FILE_IN, "<$infile");
open(DECORATE_HTML_FILE_OUT, ">$outfile");
while ($line=<DECORATE_HTML_FILE_IN>) {
print DECORATE_HTML_FILE_OUT $line;
if ($line =~ /<body/) { last; }
}
printf(DECORATE_HTML_FILE_OUT qq|<center>
<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>
<td valign="top" width="20%" align="left">%s</td>
<td valign="top" width="60%" align="center"><a target="_top" href="$homedir">Up: Example Index</a></td>
<td valign="top" width="20%" align="right">%s</td>
</tr></table>
<hr>
<b><font size="+2">%s</font></b><br>
<table><tr><td>\n|,
$prev_test ? sprintf(qq|<a target="_top" href="$homedir%s">Previous: %s</a>|, $prev_test->{dir}, $prev_test->{title}) : "",
$next_test ? sprintf(qq|<a target="_top" href="$homedir%s">Next: %s</a>|, $next_test->{dir}, $next_test->{title}) : "",
$this_test->{title}
);
while ($line=<DECORATE_HTML_FILE_IN>) {
if ($line =~ m|</body>|) {
printf(DECORATE_HTML_FILE_OUT "</td><td> </td><td>%s</td></tr></table></center>\n",
$this_test->{description});
}
print DECORATE_HTML_FILE_OUT $line;
}
close(DECORATE_HTML_FILE_OUT);
close(DECORATE_HTML_FILE_IN);
}
sub trim {
my $x = shift;
$x =~ s/^\s+//;
$x =~ s/\s+$//;
return $x;
}
sub parse_example_txt {
my $file = shift;
my $key = undef;
my $value = "";
my $line = undef;
my $x;
my $h = {};
open(IN, "<$file");
while ($line=<IN>) {
if ( ($x) = ($line =~ /^\[([^\]]+)\]/) ) {
$key = $x;
$h->{$key} = "";
} else {
if (defined($key)) {
$h->{$key} .= $line;
}
}
}
foreach $key (keys %{$h}) {
$h->{$key} = trim($h->{$key});
}
return $h;
}
sub create_html_frames_file {
my $outfile = shift;
my $test = shift;
my $rows = shift;
my $cols = shift;
my $graph_frame_src = shift;
my $mugl_frame_src = shift;
open(OUT, ">$outfile");
printf(OUT <<EOF
<HTML>
<HEAD>
<TITLE>Multigraph Example: %s</TITLE>
</HEAD>
<FRAMESET rows="%s,%s">
<FRAME src="%s">
<FRAME src="%s">
</FRAMESET>
</HTML>
EOF
,
$test->{title},
$rows,
$cols,
$graph_frame_src,
$mugl_frame_src);
close(OUT);
}
sub parse_mg_html {
my $file = shift;
my $mginfo = {
file => $file,
graphs => []
};
my $contents = "";
open(PARSE_MG_HTML_IN, "<$file");
my $line;
while ($line=<PARSE_MG_HTML_IN>) {
$contents .= $line;
}
close(PARSE_MG_HTML_IN);
$contents =~ s/(new Multigraph\(.*\))/$1\n/g;
my @lines = split(/\n/, $contents);
foreach $line (@lines) {
if (($div,$mugl,$x,$width,$height) = ($line =~ /new\s*Multigraph\(\s*'([^']+)'\s*,\s*'([^']+)'\s*(,\s*\[\s*(\d+)\s*,\s*(\d+)\s*\]\s*)?.*\)/)) {
push(@{$mginfo->{'graphs'}}, {
div => $div,
mugl => $mugl,
width => $width,
height => $height
});
}
}
return $mginfo;
}