Initial release of Maemo 5 port of gnuplot
[gnuplot] / demo / html / webify.pl
1 #!/usr/bin/perl -w
2 #
3 # Convert a single gnuplot demo script to a web page
4 # Usage:
5 #       webify xxx
6 #
7 # Reads xxx.dem and creates xxx.html along with associated 
8 # png images output to xxx.<n>.png 
9 #
10 # If gpsavediff is present also create a set of scripts
11 # xxx.<n>.gnu corresponding to the minimal set of commands 
12 # needed to generate that png image.
13 #
14 # If gnuplot_demo.css is present, link to it as a stylesheet.
15 #
16 # Ethan A Merritt <merritt@u.washington.edu>
17 # December 2003
18 #
19 # EAM Jan 2004
20 #   use gpsavediff if available
21 #   link to gnuplot_demo.css if available
22 #
23 # EAM Aug 2005
24 # If DEMOTERM is present as an environmental variable, then use
25 #    set term DEMOTERM
26 # rather than the default terminal settings
27 # E.g. (for image demo)
28 #    setenv DEMOTERM "png truecolor enhanced font arial 8 transparent size 420,320"
29 #    ./webify.pl image
30 #
31 use Env qw(DEMOTERM GNUPLOT_LIB);
32
33 use HTML::Entities;
34
35 require "ctime.pl";
36
37 # Use the in-tree copy of gnuplot if there is one
38         my $gnuplot = ( -x "../../src/gnuplot" ) ? "../../src/gnuplot" : "gnuplot" ;
39
40         my $date = &ctime(time);
41         my $plot = 1;
42
43 # input and output files
44         open(IN,  "<$GNUPLOT_LIB/$ARGV[0].dem") or die "can't open $GNUPLOT_LIB/$ARGV[0].dem";
45         open(OUT, ">$ARGV[0].html") or die "can't open $ARGV[0].html";
46
47 # open pipe to gnuplot and set terminal type
48         open(GNUPLOT, "|$gnuplot") or die "can't find gnuplot";
49         if ((defined $ENV{DEMOTERM}) && $DEMOTERM ne "") {
50             print GNUPLOT "set term $DEMOTERM\n";
51         } else {
52             print GNUPLOT "set term png enhanced font arial 8 transparent size 420,320\n";
53         }
54         print GNUPLOT "set output \"$ARGV[0].$plot.png\"\n";
55
56 # find out if gpsavediff is available in current path
57         my $savescripts = T;
58         {local $^W=0; $savescripts = open(FOO, "|gpsavediff") }
59         close FOO if ($savescripts);
60
61 # Boiler plate header
62         print OUT "<html>\n<head>\n<title>gnuplot demo script: $ARGV[0].dem </title>\n";
63         print OUT "<link rel=\"stylesheet\" href=\"gnuplot_demo.css\" type=\"text/css\">\n"
64                   if (-e "gnuplot_demo.css");
65         print OUT "</head>\n";
66         print OUT "<body>\n<h2>gnuplot demo script: <font color=blue>$ARGV[0].dem</font> </h2>\n";
67         print OUT "<i>autogenerated by webify.pl on $date</i>";
68
69 # try to find gnuplot version
70         $version = `$gnuplot --version`;
71         print OUT "\n<br><i>gnuplot version $version</i>";
72         print OUT "<hr>\n";
73
74 # Start processing
75         print OUT "<img src=\"$ARGV[0].$plot.png\" alt=\"\" align=right>\n";
76         print OUT "<pre>\n";
77
78         while (<IN>) {
79                 if (/^pause -1/) {
80                         if ($savescripts) {
81                             print OUT "<br><p>Click <a href=$ARGV[0].$plot.gnu>here</a> ",
82                                   "for minimal script to generate this plot</p>\n";
83                             print GNUPLOT "save \"| gpsavediff > $ARGV[0].$plot.gnu\"\n";
84                         }
85                         print OUT "</pre>\n<br clear=all>\n<hr>\n";
86                         $plot++;
87                         print OUT "<img src=\"$ARGV[0].$plot.png\" alt=\"\" align=right>\n";
88                         print OUT "<pre>\n";
89                         print GNUPLOT "set output \"$ARGV[0].$plot.png\"\n";
90                 } elsif (/^pause/) {
91                         print GNUPLOT "set output \"$ARGV[0].$plot.png\"\n";
92                 } else {
93                         print OUT HTML::Entities::encode($_);
94                         print GNUPLOT;
95                 }
96         }
97
98 # Amazingly enough, that's it.
99 # Unlink leftover empty plot before leaving.
100         close GNUPLOT;
101         unlink("$ARGV[0].$plot.png");
102         print OUT "</pre>\n";
103         print OUT "</body>\n</html>\n";
104