Web Development

A Forth HTML Generator

By Craig A. Lindley, December 23, 2008

A useful tool for web development -- and written entirely in ANS Forth

Craig A. Lindley is a degreed hardware engineer who, until recently, had been writing large scale Java applications. Craig has authored over 40 technical articles on various programming topics and has published five books dealing mostly with multimedia. He can be contacted via email at: calhjh@gmail.com.

It's been a long time since I've programmed in Forth but for my current project it seemed like the correct choice of programming language. My current project is the addition of Common Gateway Interface (CGI) like functionality to my <a href="http://www.ddj.com/embedded/211300170">Webster2 web server. Why Forth was chosen and how it was used in support of a CGI interface will be discussed in a future article. Here I would like to discuss a component of my current project that may be of interest to Dr. Dobb's readers -- an HTML generator written entirely in Forth.

If you're like me, you find manual coding of HTML pages a tedious and error-prone activity. Many times over the years I have written code similar to the following:

The problems I have encountered manually coding HTML include: improper nesting of HTML tags, improper embedding of special characters in text strings, forgetting closing tags, bad format of tag attributes and, in general, the generation of bad HTML understood by some browsers and not by others.

For my current project I decided to build in HTML generator functionality so that the pages returned from my web server would be of constantly higher quality. The challenge was the HTML generator had to be written in Forth.

In the Java world, I have used numerous HTML generator programs. One of the simplest and easiest to use was written by Cyrille Artho and is the Java HTML Generator. I have ported the concepts of this program to Forth -- MinForth actually -- and made some extensions along the way. The Forth code for the HTML generator is available here.

The Forth HTML generator has the following features:

Allows creation of any kind of HTML tag, with or without associated attributes.

Handles nesting of HTML tags automatically

Automatically handles the inclusion of closing tags where appropriate.

Handles the proper formatting of attribute strings including double quoting of attribute values and removal of redundant space characters.

Handles replacement of special characters in text strings such as &, < and > with their appropriate replacements: &, &ls; and >.

Table 1 shows the public API for the Forth HTML generator.

Forth Word

Stack Diagram

Comments

newtag

(addrn un -- addrt)

Create a new tag with the name given at address addrn with
length un. The returned address points at the tag data structure in memory
and can be thought of as the id of the tag for all subsequent operations.
Tags created with this method have opening and closing structures.

newtagnoclose

(addrn un -- addrt)

Same as above except tag is self contained

newtexttag

(addrt ut -- addrt)

Special type of tag for text. Here address addrt points at the
text and ut is the text length. The text is automatically parsed and any
special characters found are replaced before storage.

addattribtotag

(addra ua addrt --)

Adds the attribute string specified by addra ua to the tag
identified by addrt. The attribute string is parsed and properly formatted
before storage.

Connects the from tag addrf to the to tag addrt nesting the from
tag inside of the to tag.

renderpage

(addrt -- addrsb)

After the complete HTML tag network is created and linked
together this method is called to render the HTML. addrt identifies the top
level tag in the network. The rendered HTML is returned in a str (see text).
After the HTML is used, the str should be freed.

free-tags

(addrt --)

After the tag network has been rendered into a str, the dynamic
memory containing the tag data structures should be freed with this
method.

Example 3 shows a calendar for September 2008 generated by the Forth HTML generator. The code is too long to show here, but is available here for download. This example shows how various heading are used, how tables are generated and populated and how hypertext links are made. The rendered output is shown below:

Figure 3: Page generated by Forth HTML generator.

These three examples show the generation of static HTML pages but I hope you understand that the content of the generated pages can be as dynamic and dramatic as your application demands and your Forth programming skills allow.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!