Writing HTML with m4

Ease your creation and maintenance of web pages using this handy pre-processor called m4.
Automatic Numbering

m4 has a simple arithmetic facility with two operators m4_incr and m4_decr. This facility can be used to create automatic numbering, perhaps for headings, for example:

m4_define(_H, `m4_define(`_CARDINAL',
 m4_incr(_CARDINAL))<H2>_CARDINAL.0 $1</H2>')
_H(First Heading)
_H(Second Heading)

This produces:

<H2>1.0 First Heading</H2>
<H2>2.0 Second Heading</H2>

Automatic Date Stamping

For simple date stamping of HTML pages, I use the m4_esyscmd command to maintain an automatic timestamp on every page:

This page was updated on m4_esyscmd(date)

which produces:

This page was last updated on Fri May 9 10:35:03 HKT 1997

Generating Tables of Contents

Using m4 allows you to define commonly repeated phrases and use them consistently. I hate repeating myself because I am lazy and because I make mistakes, so I find this feature an absolute necessity.

A good example of the power of m4 is in building a table of contents in a big page. This involves repeating the heading title in the table of contents and then in the text itself. This is tedious and error-prone, especially when you change the titles. There are specialised tools for generating a table of contents from HTML pages, but the simple facility provided by m4 is irresistible to me.

Simple To Understand TOC

The following example is a fairly simple-minded table of contents generator. First, create some useful macros in stdlib.m4:

 <A HREF="#$1">$1</A>)
 <A NAME="$1"><H2>$1</H2></A>)

Then define all the section headings in a table at the start of the page body:

 `The difficulties of HTML')
m4_define(`_USING_M4', `Using
m4_define(`_SHARING', `Sharing HTML
 Elements Across Several Pages')
Then build the table:
Finally, write the text:
The advantages of this approach are twofold. If you change your headings you only need to change them in one place, and the table of contents is then automatically regenerated. Also, the links are guaranteed to work.

Simple To Use TOC

The table of contents generator that I normally use is a bit more complex and requires a bit more study, but it is much easier to use. It not only builds the table, but it also automatically numbers the headings on the fly—up to four levels of numbering (e.g., section, although this can be easily extended. It is very simple to use as follows:

  1. Where you want the table to appear, call Start_TOC.

  2. At every heading use _H1(`Heading for level 1') or _H2(`Heading for level 2') as appropriate.

  3. After the last line of HTML code (probably </HTML>), call End_TOC.

The code for these macros is shown in Listing 1. One restriction is that you should not use diversions (i.e., m4-divert) within your text, unless you preserve the diversion to file 1 used by this TOC generator.

Simple Tables

Other than Tables of Contents, many browsers support tabular information. Here are some funky macros as a short cut to producing these tables. First, an example (see Figure 1) of their use:

_Table_Hdr(,Apples, Oranges, Lemons)
_Table_Row(England, 100,250,300)

Figure 1. Example Table


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState