How It Works
The Forth HTML generator is implemented using MinForth which is an ANS Standard Forth written in C. I chose MinForth because it was possible to port it to my Webster2 web server that doesn't have an operating system per se. I needed dynamic strings (like StringBuffer in Java) for my implementation in addition to the functionality provided by MinForth. For this I turned to the Forth Foundation Library (FFL) instead of reinventing the wheel. It was a small task to port str.fs and stc.fs to the MinForth environment which says something for the Forth standardization process. Dynamic memory words, which are part of ANS Forth, also came in handy.
Tags in the Forth HTML generator are implements as a data structure in dynamic memory which contains storage for a name, for an attribute string, for a series of links to other tags and some flags. Normal tags have a name and optionally an attribute string. Text tags are not named and have their text stored in the attribute portion of the tag structure. Flags in the data structure mark a tag as a normal tag or a text tag and whether or not the tag is closed. As tags are added to other tags, an entry is made in the containing tag for the tag being linked. An HTML document is a network of linked tags.
When a new tag is created, dynamic memory is allocated for the tag data structure, the name of the tag is copied into the structure along with an optional attribute string and the appropriate flags are set. All processing and formatting of attribute strings is performed before copying into the tag structure. The same is true for text strings.
An HTML document is defined by a hierarchical network of tags. The renderpage word is used to convert the network of tags into HTML statements. This is done by performing a depth first traverse of the tag network and rendering the tags along with way. Recursion is used in the code extensively for traversal of the tag network.
Using the Forth HTML Generator
The examples I've presented show how the HTML generator code is used, but a couple of cautions are in order. The HTML generator will always output a properly formed HTML document but whether it is the document you wanted or expected is another matter entirely. For this reason it is important to feed the generated HTML to a browser during the development process to be sure you are on the right path. During your development keep the following in mind:
- Be careful that all tags created are in fact linked into the tag network. 2. The order tags are linked together is very important. Tags linked into the body of an HTML document appear in that order.
- Nesting of tags is very important especially for hypertext links. See the code for example three.
- After you have rendered your HTML page you should free the memory associated with the tag network by calling free-tags on the top-level tag. After you have used the generated HTML you should free the str memory by calling str-free on the str returned from the renderpage word. In MinForth I always execute show-heap to be sure all memory has been freed correctly.
It has been an interesting experience dusting off my old Forth skills and learning ANS Forth as well. Writing the Forth HTML generator got my chops back up and provided me with a useful tool for my on going web server development project.
In doing research into the state of Forth I have concluded that Forth is not a dying language as I first thought but is thriving in many areas. The availability of a standard language definition, foundation, and scientific libraries and a port for almost any processor/operating system you care to mention helps to insure that Forth will continue to have a place in modern system design.