mike parr's pages

Home

HTML and Macros

This article shows you how to insert standard text in a web page (or any text file).
(It is very out-of-date, and will not be updated (E.g. my use of tables). However, I still use this program to insert standard chunks of text. I'm sure you could do similar things with the M4 macro-processor, but mine might be slightly easier to use.)

Here is why you might want to do this. Here are two imaginary pages from a company website:


+----------------------------------------------+
|                                              |
|                  ABC and Co                  |
|                   PRODUCTS                   |
|                                              |
+----------------------------------------------+
|                                              |
   ... rest of page, with product info here


|                                              |
+----------------------------------------------+
and


+----------------------------------------------+
|                                              |
|                  ABC and Co                  |
|                    SALES                     |
|                                              |
+----------------------------------------------+
|                                              |
   ... rest of page, with sales info here

|                                              |
+----------------------------------------------+

I've not bothered to do the above in Html - instead I've roughed-out the pages. The main thing is that - like most websites - every page has a standard heading. This often deals with beginning some table definitions, navigation links, perhaps a logo etc.

Obviously, we don't wish to duplicate identical text in each file, because it makes it hard to apply consistent changes.

But look at the above two pages - the headings are not identical - one says SALES, the other PRODUCTS. In fact these could equally be an image of a warehouse, and an image of a banknote.

So - we have a standard heading ,with minor variations dependent on which page the heading prefixes.

You can use a program known as a macro-processor to do the insertion of standard text, and it can also use parameters, to allow for small variations. Don't get confused between a macro- processor and a keystroke recorder. We are talking about files of text here, not keystroke commands.

Which macro-processor?

There are several. Unix has one known as m4, and you can probably get a port of this for MS Windows, or run it under Linux. The drawback of m4 is that (like most macro-processors) it uses a range of special characters, and often, these characters exist in Html text. You can program round this, but - because I had the code for a simple macro-processor along m4 lines, I decided to write my own (named webmacro), by adapting existing code. The resulting program is nowhere near as powerful as m4, but is more suitable for Html files.

Using Webmacro

We create a file containing 'definitions'. Each definition has a name -which we choose - and a body, and some optional 'parameters'. In this example, we will create a file named defs.txt, with two definitions, which we name header and trailer (you can choose the names). Here is the file defs.txt:


 define
 $header
 <html>
 <title>ABC and Co - param1  </title>

 <center>
 <h1>ABC and Co
 <p>
 param2
 </h1>
 </center>
 <table> <tr><td>   ... etc
 enddefine


 define
 $trailer
 </td></tr>
 </table> ....etc
 </html>
 enddefine
 

The above file holds 2 macro definitions. Each one starts and ends with define and enddefine. The line after define is your choice of macro name, starting with $. These 3 special lines must not be indented. If you indent them, they will be treated as normal lines.

After the name, we get the 'body' - any text. Within the body, the strings param1 param2 ...up to param9 are treated specially. When we call the macro, they will be replaced by parameters.

Here is the sales.htm file, showing calls:


 $header
 =Sales Page
 =SALES

 Here is our sales information...
 blah blah...

 $trailer

When we call a macro - by stating its name (e.g. $trailer) the call line is replaced by the lines in the body of the macro.

If a call is followed by lines starting with =, these items are regarded as parameters, and are inserted into the body in place of param1, param 2 etc.

In the above, the string 'Sales Page' replaces ' every occurence of param1'
and the string 'SALES' replaces 'param2'

The end of the parameters is shown by a line not starting with an =

A call and its parameters cannot be indented - if they are, they are copied to the output file as normal text.

The real benefit comes when we want a products page: it looks like this:


 $header
 =Products Page
 =PRODUCTS

 Our company makes a wide range of items, ...
 blah blah...

 $trailer

When the calls happen (i.e. the macro is 'expanded' ), our standard html boilerplating is inserted, but modified slightly - by parameters - for a particular page.

Using Macro.

To run webmacro, use the command line:
webmacro defsFileName callsFileName outputFilename

the output file is the html that you upload to your site.

Here is how I use it:

put webmacro.exe in a folder called ToProcess, among with your html files containing calls.

In ToProcess, create a bat file containing e.g:


webmacro def1.txt index.html   ..\upload\index.html
webmacro def1.txt music.htm   ..\upload\music.htm
webmacro def1.txt photos.htm   ..\upload\photos.htm
webmacro def1.txt downloads.htm   ..\upload\downloads.htm
webmacro def1.txt contact.htm   ..\upload\contact.htm


Create an upload folder at the same level as ToProcess.

When you run the above bat, all the files get expanded , and copied into upload.

Of Course, style sheets also reduce repetitive html as well - but you can us them alongside macros.

Errors

If you get and I/O ERROR message from webmacro.exe, the likely cause is that you have got an input file name or path wrong.

Undefined calls also cause an error, with an explanatory message.

Other features (version 3)

Counter

The string nextnumber is treated specially. It is replaced by a number, which is incremented automatically. Here is a macro for chapter 3 of some notes.
define
 $ch3section
 Section 3.nextnumber
 enddefine

Here are 2 calls:

 $ch3section
 $ch3section

They produce:

Section 3.0

Section 3.1
etc

Nested calls

You can put a call in a definition, as in:
 define
 $macro1
 Start macro1
 $macro2
 =fish
 End macro1
 enddefine

 define
 $macro2
 in macro2 with a parameter of param1
 enddefine

This is processed in 2 passes. First, macro1 is expanded. Then macro2 is expanded. The result from a call of macro1 is:
Start macro1
in macro2 with a parameter of fish
End macro1

This can be useful. You can build macros based on old macros you have created already, rather than having to duplicate large amouts of text.

New features (version 4 - feb 2011)

1. The string thisfilename is treated specially. it is replaced by the name of the file currently being expanded (e.g. calls.txt).

2. Conditional output, based on the equality of 2 strings. The magic words doifequal and doifnotequal are used, along with the :: delimiter, as in


doifequal::fred::fred::see this
Here, 'see this' is copied to the output file, because fred = fred. This becomes more useful when the strings include parameters and thisfilename. Here is an example, which personalises a web page:

 define
 $clicked

 doifequal::thisfilename::param1::
 doifnotequal::thisfilename::param1::
 enddefine
We call it by supplying a file name, as in

 $clicked
 =index.html

 $clicked
 =contact.html
If the calls are made within a file named contact.html, then the 'orange' html code is output. From another file, the blue html code is output.

Obtaining it

Download webmacro.exe here (50 Kb only!)

The program is zero-cost and open-source. The Delphi Pascal code is available on request.