libhtml.gsl

libhtml.gsl is a library of GSL functions that can be found in $UNIDIR/lib. As the library is written in GSL you can easily modify or extend it. If you make any enhancements please send them to gsharp@avs.com

To make these functions available in your code you must add the following to the top of your script:

include "$UNIDIR/lib/libhtml.gsl";

The following functions are included in libhtml.gsl

Miscellaneous Functions

  • HTMLdrill_map
  • HTMLdrill_map2

Functions for Writing HTML

All these functions create HTML tags using the fwrite() function. You can combine these calls with your own calls to fwrite. e.g. fwrite(stdout, "<TAG>A special tag</TAG>");

The first argument to each function is the stream number. This is either stdout (1) for scripts running in cgi-bin or the value returned by fopen(filename, "w") if creating an actual HTML file.

Scripts can be written to handle both cases:

if webedition() then
  f = stdout;
else
  f = fopen("myfile.html", "w");
endif

HTMLheader(f,"Title","Author");
HTMLheading(f,1,"Hello Wide World");
HTMLfooter(f);

if not webedition() fclose(f); 
Tables

Maps

Forms

set_image - specify desired image size

Syntax: set_image(float npx, float npy, string filename)
Description: set_image() sets the canvas to the required size and then selects theoutput device based on the extension of the filename:
  • You should use set_image() to set the canvas size, when you want to use HTMLbounding_polygon() on an object that is effected by the canvas size, such as a note.
  • You don't need to call set_image as well as make_image - if all you want to do is make an image - then make_image is enough.
Code sample: set_image(400, 300, "bob.gif");

make_image - output image

Syntax: make_image(float npx, float npy, string filename)
Description:

make_image() creates an image of the specified size (in pixels). The format of the image is taken from the filename extension.

.gif
GIF
.png
PNG
.jpg
JPEG
.tif
TIFF
.cgm
CGM
.eps
Encapsulated PostScript

 

Code sample: make_image(400, 300, "bob.gif");

HTMLbounding_polygon - find position of graphic objects

Syntax: float HTMLbounding_polygon(string object, float nvertices, float x, float y, float imageWidth, float imageHeight)
Description:

HTMLbounding_polygon is a wrapper for the built-in function bounding_polygon().

  • You are recommended to use HTMLbounding_polygon() as it automatically deals with the conversion to pixel co-ordinates.
  • Use of this function is explained in more detail in the section Dynamically Generating Image Maps
Code sample: set_image(400, 300, "bob.gif");

HTTPheader - write HTTP header

Syntax: HTTPheader(string contentType)
Description: Writes the HTTP header
  • HTTPheader() should be used at the start of a cgi-bin script before outputting the HTML
Code sample: HTTPheader("text/html");
Output: Content-type: text/html

HTMLheader - write HTML header

Syntax: HTMLheader(float i, string title, string author)
Description: Writes the HTML header
Code sample: HTMLheader(stdout, "GSL Example", "AVS")
HTML: <HTML> <HEAD> <TITLE>GSL Example</TITLE> <META NAME="Author" CONTENT="AVS"> </HEAD> <BODY>

HTMLheader2 - alternative write HTML header

Syntax: HTMLheader2(float i, string title, string author, string color)
Description: Writes the HTML header
Code sample: HTMLheader2(stdout, "GSL Example", "AVS", "#fefefe")
HTML: <HTML> <HEAD> <TITLE>GSL Example</TITLE> <META NAME="Author" CONTENT="AVS"> </HEAD> <BODY BGCOLOR=#fefefe>

HTMLfooter - write HTML footer

Syntax: HTMLfooter(float i)
Description: Writes an HTML header tag
Code sample: HTMLheader(stdout, "GSL Example", "AVS")
HTML: </BODY> </HTML>

HTMLheading - write heading tag

Syntax: HTMLheading(float i, float h, string text)
Description: Writes an HTML heading tag
Code sample: HTMLheading(stdout, 1, "My First Web Page")
HTML: <H1>My First Web Page</H1>
Output:

My First Web Page

HTMLimage - write img tag

Syntax: HTMLimage(float i, string imageFile)
Description: Writes an HTML IMG tag
Code sample: HTMLimage(stdout, "/images/gsharp.gif")
HTML: <IMG SRC=/images/gsharp.gif>
Output:

HTMLimagemap - write image tag with imagemap

Syntax: HTMLimagemap(float i, string imageFile, string map)
Description: Writes an HTML IMG tag with a map specified
Code sample: HTMLimage(stdout, "/images/gsharp.gif", "mymap")
HTML: <IMG SRC=/images/gsharp.gif USEMAP=#mymap>
Output:

HTMLrule - write hr tag

Syntax: HTMLrule(float i, float width)
Description: Writes an HR tag
Code sample: HTMLrule(stdout, 30)
HTML: <HR WIDTH=30%>
Output:

HTMLstartcomment - open comment tag

Syntax: HTMLstartcomment(float i)
Description: Opens a comment tag

Place comments around functions that produce output you can't turn off. N.B. You can use set_messages() to turn off Gsharp messages.

Code sample: HTMLstartcomment(stdout)
HTML: <!---

HTMLendcomment - close comment tag

Syntax: HTMLendcomment(float i)
Description: Close a comment tag
Code sample: HTMLendcomment(stdout)
HTML: --->

HTMLlink - write href tag

Syntax: HTMLlink(float i, string text, string url)
Description: Writes an href tag
Code sample 1: HTMLlink(stdout, "Back to top", "libhtml.htm")
HTML 1: <A HREF=libhtml.htm>Back to top</A>
Output 1: Back to top
Code sample 2: HTMLlink(stdout, "Back to Home Page", "../../index.htm target=_top")
HTML 2: <A HREF=../../index.htm target=_top>Back to Home Page</A>
Output 2: Back to Home Page

HTMLmap - write HTML map

Syntax: HTMLmap(float i, string map)
Description: Write map tag
Code sample: HTMLmap(stdout, "mymap")
HTML: <MAP NAME=mymap>

HTMLmaprect - write rectangle to map

Syntax: HTMLmaprect(float i, string url, float left, float bottom, float right, float top)
Description: Writes a rectangle to the map.

left, bottom, right and top are in pixels, measured from the top, left of the image.

Code sample 1: HTMLmaprect(stdout, "/Gsharp-bin/next.gsw?bar=1", 10, 90, 90, 10)
HTML 1: <AREA COORDS=10,90,90,10 SHAPE=rect HREF=/Gsharp-bin/next.gsw?bar=1>

HTMLmapcircle - write circle to map

Syntax: HTMLmapcircle(float i, string url, float center, float radius)
Description: Writes a circle to the map.
  • center and radius are in pixels. center is measured from the top, left of the image.
Code sample: HTMLmapcircle(stdout, "/through/roundWindow.htm", (10,90), 80)
HTML: <AREA COORDS=10,90,80 SHAPE=circle HREF=/through/roundWindow.htm>

HTMLmappoly - write polygon(s) to map

Syntax: HTMLmappoly(float i, string url, float x, float y)
Description: Writes a polygon to the map.
  • x and y are in pixels, measured from the top, left of the image.
Code sample: x = 00//10//10//00//00//undef//10//20//20//10//10; y = 00//00//10//10//00//undef//10//10//20//20//10; HTMLmappoly(stdout, "/through/boxes.htm", x, y)
HTML: <AREA COORDS=" 0,0, 10,0, 10,10, 0,10, 0,0, " SHAPE=polygon HREF=/through/boxes.htm> <AREA COORDS=" 10,10, 20,10, 20,20, 10,20, 10,10, " SHAPE=polygon HREF=/through/boxes.htm>

HTMLmapclose - close map tag

Syntax: HTMLmapclose(float i)
Description: Close map tag
Code sample: HTMLmapclose(stdout)
HTML: </MAP>

HTMLtable - write table tag

Syntax: HTMLtable(float i, float width)
Description: Write table tag. width is in percent
Code sample: HTMLtable(stdout, 80); HTMLtablerow(stdout); HTMLtabledata(stdout); fwrite(stdout, "X ="); HTMLtabledata(stdout); fwrite(stdout, X); HTMLtableclose
HTML: <TABLE WIDTH=80%> <TR><TD>X =<TD>0.45678 </TABLE>
Output:
X = 0.45678

HTMLtablerow - write table row tag

Syntax: HTMLtablerow(float i)
Description: Write table row tag.
Code sample: HTMLtablerow(stdout);
HTML: <TR>

HTMLtabledata - write table data tag

Syntax: HTMLtabledata(float i)
Description: Write table data tag.
Code sample: HTMLtabledata(stdout);
HTML: <TD>

HTMLtableclose - close table tag

Syntax: HTMLtableclose(float i)
Description: Close table tag
Code sample: HTMLtableclose(stdout)
HTML: </TABLE>

HTMLform - write form tag

Syntax: HTMLform(float i, string method, string url)
Description: Write form tag
  • If method is "GET" Gsharp gets the form variables from the environment. If it is "PUT" it gets them from stdin.
    In either case Gsharp will process the form input and create string datasets named FORM_name, where name is the name of the form field.
Code sample: HTMLform(stdout, "GET", "/Gsharp-bin/processform.gsw")
HTML: <FORM METHOD=GET ACTION=/Gsharp-bin/processform.gsw>

HTMLsubmit - write submit button

Syntax: HTMLsubmit(float i, string text)
Description: Write submit button
  • When the submit button is pressed, the form's action is executed
Code sample: HTMLsubmit(stdout, "Hit me!")
HTML: <INPUT TYPE=submit VALUE="Hit me!">
Output:

HTMLhidden - write hidden field

Syntax: HTMLhidden(float i, string name, string text)
Description: Write hidden field
  • A hidden field is not visible on the page
  • Hidden fields are useful for passing on information to the next script. e.g. HTMLhidden(stdout, "section", FORM_section)
Code sample: HTMLhidden(stdout, "plottype", "bar");
HTMLhidden(stdout, "data", "Monday");
HTML: <INPUT TYPE=hidden VALUE="bar" NAME=plottype>
<INPUT TYPE=hidden VALUE="Monday" NAME=data>

HTMLselect - write select field

Syntax: HTMLselect(float i, string name, string text, string values)
Description: Write select field
  • text is a string array of possible choices
  • values is a string array of corresponding values
Code sample: plotNames = "Bar Graph"//"Line Graph"//"Scatter Plot";
plotTypes = "bar"//"line"//"scatter";
HTMLselect(stdout, "plottype", plotNames, plotTypes);
HTML: <SELECT NAME=plottype>
<OPTION VALUE="bar">Bar Graph
<OPTION VALUE="line">Line Graph
<OPTION VALUE="scatter">Scatter Plot
</SELECT>
Output:

HTMLselect2 - alternative write select field

Syntax: HTMLselect2(float i, string name, string text, string values, string def)
Description: Write select field
  • text is a string array of possible choices
  • values is a string array of corresponding values
  • def is the default value
Code sample: plotNames = "Bar Graph"//"Line Graph"//"Scatter Plot";
plotTypes = "bar"//"line"//"scatter";
HTMLselect(stdout, "plottype", plotNames, plotTypes, "line");
HTML: <SELECT NAME=plottype>
<OPTION VALUE="bar">Bar Graph
<OPTION VALUE="line" selected>Line Graph
<OPTION VALUE="scatter">ScatterPlot
</SELECT>
Output:

HTMLformtext - write text field

Syntax: HTMLformtext(float i, string name, float size, string value)
Description: Write text field
  • size is the number of columns
  • values is a string array of corresponding values
  • def is the default value
Code sample: HTMLformtext(stdout, "title", 30, "My Title");
HTML: <INPUT TYPE=text VALUE="My Title" SIZE=30 NAME=title>
Output:

HTMLformimage - write image field

Syntax: HTMLformimage(float i, string img, string name)
Description: Write image field
Code sample: HTMLimagefield(stdout, "/images/bar3d.gif", "barplot");
HTML: <INPUT TYPE=image SRC=/images/bar3d.gif NAME=barplot>
Output:

HTMLformclose - close form tag

Syntax: HTMLformclose(float i)
Description: Close form tag
Code sample: HTMLformclose(stdout)
HTML: </FORM>

HTMLarray - create a table of data

Syntax: HTMLarray(float i, float x)
Description: Create a table of data.

The calls to <TABLE> and </TABLE> are not included, so that you can add your own headers or footers

Code sample: x = reshape(rnd(16),4,4,1); HTMLtable(stdout, 80) HTMLtablerow(stdout) fwrite(stdout, "<TH>"+1:4); HTMLarray(stdout, x); HTMLtableclose();
HTML: <TABLE WIDTH=80%> <TR><TH>1<TH>2<TH>3<TH>4 <TR><TD>0.9219074<TD>0.9421443<TD>0.9183750<TD>0.1406383 <TR><TD>0.8540658<TD>0.6779069<TD>0.2192163<TD>0.9281892 <TR><TD>0.0451316<TD>0.7776184<TD>0.7849464<TD>0.3180923 <TR><TD>0.6518278<TD>0.3806676<TD>0.9212141<TD>0.5283498 </TABLE>
Output:
1 2 3 4
0.9219074 0.9421443 0.9183750 0.1406383
0.8540658 0.6779069 0.2192163 0.9281892
0.0451316 0.7776184 0.7849464 0.3180923
0.6518278 0.3806676 0.9212141 0.5283498

HTMLapplet - write applet tag

Syntax: HTMLapplet(float i, string app, float width, float height)
Description: Write applet tag
Code sample: HTMLapplet(stdout, "/applets/myapp", 100, 100)
HTML: <applet code=/applets/myapp width=100 height=100></applet>