mike parr's pages

 
  Home . Downloads . Sheffield Hallam Stuff . My Band, Music . Photo Gallery .
Favourite links . Top Ten . Contact Me


Web pages with a macro processor

Including code in a web page with a macro processor

This article shows you how to insert standard text in a web page (or any text file).

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, 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. The drawback of m4 is that (like most macro-processors) is 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 macro), by adapting existing code. The resulting program is nowhere near as powerful as m4, but is more suitable for Html files.

Using Macro

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
<html>
<title>ABC and Co - param1  </title>

<center>
<h1>ABC and Co 
<p>
param1
</h1>
</center>
  etc
enddefine


define

  

 
  Home . Downloads . Sheffield Hallam Stuff . Music . Photo Gallery .
Favourite links . Top Ten . Contact Me

</td> </tr> </table> </html> enddefine

We have isolated the standard header, and the trailer. The rest is specific to each page.

Note the special text param1, define, enddefine in the file:



=PRODUCTS

rest of page, with product details...


  

 
  Home . Downloads . Sheffield Hallam Stuff . Music . Photo Gallery .
Favourite links . Top Ten . Contact Me


here is the file salesMaster.htm:


$trailer
=SALES

rest of page, about sales...


  

 
  Home . Downloads . Sheffield Hallam Stuff . Music . Photo Gallery .
Favourite links . Top Ten . Contact Me


We now 'call' or 'expand' the macros: prepare a bat file called e.g. expand.bat, containing:

macro defs.txt salesMaster.html sales.html
macro defs.txt productsMaster.html   products.html


After the macro command, we supply a definition file name. an input file, and an output file. The latter is the one we upload.

When we run the bat file, macro replaces a 'call' of a macro in the input file by the text between the macro name and the enddefine of the defs file. It also replaces the string param1, param2, param3 etc with the text we put underneath the call. Example of call:



=SALES                    (param1 is SALES)
                         (  end of parameters is detected by a line not starting with = )


The macro named header has one parameter here. Parameters start with =.

All calls end with a line containing a single =

So:



  

 
  Home . Downloads . Sheffield Hallam Stuff . Music . Photo Gallery .
Favourite links . Top Ten . Contact Me

is a call of trailer, with no parameters. Macro calls can have up to 9 parameters The general form of a call is:

=replacement for param1
=replacement for param2
=etc (up to param9


If a macro has no parameters, put:



Note that the replacement text can include spaces, commas... any text in fact. But they can only occupy one line each. Keywords and = cannot be indented.

Help on usage

When you have lots of files, a usable approach is:

Errors

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

Obtaining

...from my downloads page.

 
  Home . Downloads . Sheffield Hallam Stuff . Music . Photo Gallery .
Favourite links . Top Ten . Contact Me