#!/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 # images output to xxx..html # # If gpsavediff is present also create a set of scripts # xxx..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 # 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 Jan 2009 # set term canvas # # EAM May 2009 # generalized mousing support in the demos # each plot has its own mousebox # FIXME: mousing should depend on some recognizable line in the demo script, # or the mouseable demos should have a separate make target # # EAM May 2013 # I've always hated the dangling partial frame at the end of the html page. # Re-order the main loop to get rid of it. # 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 $grid = 0; my $name = "foo"; # options my $iar = 0; if ($ARGV[$iar] eq "--mouse") { $mousing = 1; $iar++; } if ($ARGV[$iar] eq "--grid") { $grid = 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 $ARGV[0].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 { print GNUPLOT "set term canvas name \"$name"."_$plot\" jsdir \".\" lw 1.6\n"; } print GNUPLOT "set output \"$name.$plot.js\"\n"; if ($grid) { print GNUPLOT "set grid x y mx my\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 "\n"; print OUT "\n\ngnuplot demo script: $name.dem \n"; print OUT "\n"; print OUT "\n" if (-e "gnuplot_demo.css"); print OUT "\n" if ($mousing && -e "gnuplot_mouse.css"); print OUT "\n"; print OUT "\n"; print OUT "\n"; print OUT "\n"; print OUT "\n" if ($mousing); print OUT "\n"; print OUT "\n"; print OUT "\n"; print OUT "\"Back\n"; print OUT "

gnuplot demo script: $name.dem

\n"; print OUT "autogenerated by webify.pl on $date"; # try to find gnuplot version $version = `$gnuplot --version`; print OUT "\n
gnuplot version $version"; print OUT "
\n"; # Echo plot commands to a temp file open(ECHO, "+>commands.tmp") or die "can't open commands.tmp"; binmode ECHO,":encoding(UTF-8)"; # Start processing while () { if (/^ *pause -1/) { print OUT "
"; print OUT "\n"; print OUT "\n"; print OUT "
Your browser does not support the HTML 5 canvas element
\n"; print OUT "
\n"; print OUT "\n"; print OUT "
\n"; # Copy mouse box into output stream if ($mousing && -e "mousebox.template") { my $spanid = $name."_".$plot; open(MOUSEBOX, ") { s/ACTIVE_PLOT_NAME/$spanid/; print OUT; } close MOUSEBOX; } print OUT "
\n";

			## FIXME: dump contents of ECHO into OUT here
			seek ECHO, 0, 0;
			print OUT ;
			close ECHO;
			open(ECHO, "+>commands.tmp") or die "can't open commands.tmp";
			binmode ECHO,":encoding(UTF-8)";

			if ($savescripts) {
			    print OUT "
Click here ", "for minimal script to generate this plot\n"; print GNUPLOT "save \"| gpsavediff > $name.$plot.gnu\"\n"; } print OUT "
\n
\n
\n"; $plot++; print GNUPLOT "set term canvas name \"$name"."_$plot\" jsdir \".\" lw 1.6\n"; print GNUPLOT "set output \"$name.$plot.js\"\n"; } elsif (/^pause/) { print GNUPLOT "set term canvas name \"$name"."_$plot\" jsdir \".\" lw 1.6\n"; print GNUPLOT "set output \"$name.$plot.js\"\n"; } elsif (/^ *reset/) { print GNUPLOT; } else { print ECHO HTML::Entities::encode($_); print GNUPLOT; } } # Amazingly enough, that's it. # Unlink leftover empty plot before leaving. close GNUPLOT; unlink("$name.$plot.js"); close ECHO; unlink("commands.tmp"); # The filled curve mechanism requires an associated canvas element somewhere in the document. # We stick it at the end and mark it "hidden". print OUT "\n"; print OUT "\n\n";