User's guide for HTML2CGI

Copyright (C) Morten Skaarup Jensen 2000

Synopsis


html2cgi htmlfile
 

Summary

Translates an HTML file to a Tcl CGI file that produces the same output. This output is written to the file with the same name as the input file, but with the extension .cgi instead of .htm or .html. This file is given execute permissions for all.

The typical use of html2cgi is to create an HTML file with a WYSIWYG HTML editor. This file will typically contain some comments that contain raw Tcl code that is used directly in the CGI script. In this way, the static part of a CGI script can be created with the help of a WYSIWYG HTML editor.
 

Initialisation

The default path of the interpreter is assumed to be /usr/bin/tclsh
If tclsh is located somewhere else, this can be specified with the meta tag cgi_interpreter.

The resulting CGI script will use Don Libes's cgi library. The first thing that the resulting CGI script will do is try to load this package. If this fails, the functions that are used from this library must be defined in another way.

One can instruct the resulting CGI script to source certain files immediately after trying to load Don Libes's cgi library. This is done with the meta tag cgi_source_files which is a tcl list containing the file names to be sourced. This can be used to define the functions that file names to be sourced. This can be used to define the functions that normally would come from Don Libes's cgi library. It is also a good opportunity to source files containing functions that one often uses in ones Tcl CGI scripts.
 

The main text

Only after these initialisations is the HTML for the page actually written by the CGI file. This is simply done with the Tcl puts command.

If a comment starts with CGI then the comment is interpreted as raw Tcl code that must be inserted at the corresponding position in the CGI script.
 

Example

The command

html2cgi foo.html

will produce an executable file foo.cgi
If the file foo.cgi already exists, it will be overwritten without warning unless noclobber is set in .cshrc.

If foo.html contains the following

<html>
<head>
<Title>Foo Home Page</Title>
<Meta  name="cgi_interpreter" content="/usr/bin/tclsh8.3">
<Meta  name="cgi_source_files" content="/home/msj/public_html/lib/myprocs.tcl /home/msj/public_html/donsprocs.t cl">
</head>
<body>
<H1>Welcome!</H1><br>
<!-- CGI        for {set i 0} {$i < 10} {incr i} {          -->
<!-- CGI           puts "$i : "                             -->
<b>Out of cash</b><br>
<!-- CGI        }                                           -->
<!-- CGI        foreach name [cgi_import_list] {
                   puts "<b>$name :</b> [cgi_import $name]<br>"
                }                                           -->

</body>
</html>

then foo.cgi will contain
#!/usr/bin/tclsh8.3
lappend auto_path /usr/local/lib
catch {package require cgi}
catch {source /home/msj/public_html/lib/myprocs.tcl} out
if {"$out" != ""} {puts stderr $out}
catch {source /home/msj/public_html/donsprocs.tcl} out
if {"$out" != ""} {puts stderr $out}
cgi_input
puts "Content-Type: text/html"
puts ""
puts {<html>
}
puts {<head>
}
puts {<Title>Foo Home Page}
puts {</Title>
}
puts {<Meta name="cgi_interpreter" content="/usr/bin/tclsh8.3">
}
puts {<Meta name="cgi_source_files" content="/home/msj/public_html/lib/myprocs.tcl
  /home/msj/public_html/donsprocs.tcl" >
}
puts {</head>
}
puts {<body>
}
puts {<H1>Welcome!}
puts {</H1>}
puts {<br>
}
        for {set i 0} {$i < 10} {incr i} {
           puts "$i : "
puts {<b>Out of cash}
puts {</b>}
puts {<br>
}
        }
        foreach name [cgi_import_list] {
                   puts "<b>$name :</b> [cgi_import $name]<br>"
                }
puts {</body>
}
puts {</html> }
 
If this CGI script was called from a submit button in a form with two fields name and age with the values John and 52 respectively, then the resulting output in a browser would look like the content below the following line

Welcome!

0 : Out of cash
1 : Out of cash
2 : Out of cash
3 : Out of cash
4 : Out of cash
5 : Out of cash
6 : Out of cash
7 : Out of cash
8 : Out of cash
9 : Out of cash
name : John
age : 52