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
|
#!/usr/bin/perl -w
#
# Convert a single gnuplot demo script to a web page
# Usage:
# webify xxx
#
# Reads xxx.dem and creates xxx.html along with associated
# png images output to xxx.<n>.png
#
# If gpsavediff is present also create a set of scripts
# xxx.<n>.gnu corresponding to the minimal set of commands
# needed to generate that png image.
#
# If gnuplot_demo.css is present, link to it as a stylesheet.
#
# Ethan A Merritt <merritt@u.washington.edu>
# December 2003
#
# EAM Jan 2004
# use gpsavediff if available
# link to gnuplot_demo.css if available
#
# EAM Aug 2005
# If DEMOTERM is present as an environmental variable, then use
# set term DEMOTERM
# rather than the default terminal settings
# E.g. (for image demo)
# setenv DEMOTERM "png truecolor enhanced font arial 8 transparent size 420,320"
# ./webify.pl image
#
# EAM Apr 2011
# Mouseable demos
#
use Env qw(DEMOTERM GNUPLOT_LIB);
use Time::localtime;
use HTML::Entities;
# Use the in-tree copy of gnuplot if there is one
my $gnuplot = ( -x "../../src/gnuplot" ) ? "../../src/gnuplot" : "gnuplot" ;
if ((!defined $ENV{GNUPLOT_LIB}) || $GNUPLOT_LIB eq "") {
$GNUPLOT_LIB = "..";
}
my $date = ctime();
my $plot = 1;
my $mousing = 0;
my $name = "foo";
# options
my $iar = 0;
if ($ARGV[$iar] eq "--mouse") {
$mousing = 1;
$iar++;
}
$name = $ARGV[$iar];
print STDERR $name, "\n";
# input and output files
open(IN, "<$GNUPLOT_LIB/$name.dem") or die "can't open $GNUPLOT_LIB/$name.dem";
open(OUT, ">$name.html") or die "can't open $name.html";
binmode IN, ":encoding(UTF-8)";
binmode OUT,":encoding(UTF-8)";
# open pipe to gnuplot and set terminal type
open(GNUPLOT, "|$gnuplot") or die "can't find gnuplot";
binmode GNUPLOT,":encoding(UTF-8)";
if ((defined $ENV{DEMOTERM}) && $DEMOTERM ne "") {
print GNUPLOT "set term $DEMOTERM\n";
} else {
if ($mousing) {
print GNUPLOT "set term svg enhanced font 'arial,10' mouse name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
} else {
print GNUPLOT "set term svg enhanced font 'arial,10' name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
}
}
print GNUPLOT "set output \"$name.$plot.svg\"\n";
# suppress animations
print GNUPLOT "NO_ANIMATION = 1\n";
# find out if gpsavediff is available in current path
my $savescripts = T;
{local $^W=0; $savescripts = open(FOO, "|gpsavediff") }
close FOO if ($savescripts);
# Boiler plate header
print OUT "<!DOCTYPE HTML>\n";
print OUT "<html>\n<head>\n<title>gnuplot demo script: $name.dem </title>\n";
print OUT "<meta charset=\"UTF-8\" />\n";
print OUT "<link rel=\"stylesheet\" href=\"gnuplot_demo.css\" type=\"text/css\">\n"
if (-e "gnuplot_demo.css");
print OUT "</head>\n";
print OUT "<body>\n";
print OUT "<a href=index.html><image src=return.png alt=\"Back to demo index\" class=\"float-right\"></a>\n";
print OUT "<h2>gnuplot demo script: <font color=blue>$name.dem</font> </h2>\n";
print OUT "<i>autogenerated by webify.pl on $date</i>";
# try to find gnuplot version
$version = `$gnuplot --version`;
print OUT "\n<br><i>gnuplot version $version</i>";
print OUT "<hr>\n";
# Start processing
print OUT "<table><tr><td valign=top>\n";
print OUT "<object data=\"$name.$plot.svg\" type=\"image/svg+xml\" width=600 height=400 class=\"float-right\"/>\n";
print OUT "</td><td>\n";
print OUT "<pre>\n";
while (<IN>) {
if (/^ *pause -1/) {
if ($savescripts) {
print OUT "<br>Click <a href=$name.$plot.gnu>here</a> ",
"for minimal script to generate this plot\n";
print GNUPLOT "save \"| gpsavediff > $name.$plot.gnu\"\n";
}
print OUT "</pre></td></tr></table>\n<br clear=all>\n<hr>\n";
$plot++;
print OUT "<table class=\"noborder\"><tr><td>\n";
print OUT "<object data=\"$name.$plot.svg\" type=\"image/svg+xml\" width=600 height=400 class=\"float-right\"/>\n";
print OUT "</td><td valign=top>\n";
print OUT "<pre>\n";
if ((defined $ENV{DEMOTERM}) && $DEMOTERM ne "") {
print GNUPLOT "set term $DEMOTERM\n";
} else {
if ($mousing) {
print GNUPLOT "set term svg enhanced font 'arial,10' mouse name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
} else {
print GNUPLOT "set term svg enhanced font 'arial,10' name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
}
}
print GNUPLOT "set output \"$name.$plot.svg\"\n";
} elsif (/^pause/) {
if ((defined $ENV{DEMOTERM}) && $DEMOTERM ne "") {
print GNUPLOT "set term $DEMOTERM\n";
} else {
if ($mousing) {
print GNUPLOT "set term svg enhanced font 'arial,10' mouse name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
} else {
print GNUPLOT "set term svg enhanced font 'arial,10' name \"$name"."_$plot\" jsdir '.' size 600,400 dynamic\n";
}
}
print GNUPLOT "set output \"$name.$plot.svg\"\n";
} else {
print OUT HTML::Entities::encode($_);
print GNUPLOT;
}
}
# Amazingly enough, that's it.
# Replace leftover empty file with a legal svg empty plot before leaving.
close GNUPLOT;
unlink("$name.$plot.svg");
open(EMPTY, ">$name.$plot.svg") or die "can't open empty plot";
print EMPTY <<MT
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="1" height="1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
</svg>
MT
;
print OUT "</pre></td></tr><table>\n";
print OUT "</body>\n</html>\n";
|