MC logo

Macro Translation Example

^  CSc 231 Perl Assignments

Here is a small example of what the macro translator can do. Presume the input file hmac.p3:
!define STYLE=<style type="text/css">#1</style>
!define TIT=<h3 style="width: 100%; text-align: center; font-family: sans;">#1</h3>
!define HDR=<html><head><title>#1</title>@STYLE(#2)</head><body>
!define TAIL=</body></html>
!define LN=<a href="#1">#2</a>
!define TR_=@if(#1,,</tr>,<td>#1</td>@TR_(#-))
!define TR=<tr>@TR_(#*)
!define TRt=<tr><td style="text-align: right; font-weight: bold">#1:</td>@TR_(#-)
!define TH_=@if(#1,,</tr>,<th>#1</th>@TH_(#-))
!define TH=<tr>@TH_(#*)
!define BF=<b>#1</b>
This will be includec by this file, page.p3:
!include hmac.p3
@HDR(This is a test page., body { background-color: blue; color: #aaaaff; })
This is an exciting test page.  One of the most exciting things
about it is that it has a table.  Here is the very,
very exciiting table:
<table align="center">
<b>@TH( ,Dogs, Cats, Fish, Squrrels, Snakes)</b>
@TRt(Says,Woof,Meow, , ,Sssssss)
@TRt(Is,Loyal,Indifferent,Wet,Nutty,Sneaky)
@TRt(Group,pack,cluster,school,scurry,knot)
</table>
@TAIL()
When you run page.p3 through the macro processor, it produces the following lovely mess: page.htm:
<html><head><title>This is a test page.</title><style type="text/css"> body { background-color: blue; color: #aaaaff; }</style></head><body>
This is an exciting test page.  One of the most exciting things
about it is that it has a table.  Here is the very,
very exciiting table:
<table align="center">
<b><tr><th> </th><th>Dogs</th><th> Cats</th><th> Fish</th><th> Squrrels</th><th> Snakes</th></tr></b>
<tr><td style="text-align: right; font-weight: bold">Says:</td><td>Woof</td><td>Meow</td><td> </td><td> </td><td>Sssssss</td></tr>
<tr><td style="text-align: right; font-weight: bold">Is:</td><td>Loyal</td><td>Indifferent</td><td>Wet</td><td>Nutty</td><td>Sneaky</td></tr>
<tr><td style="text-align: right; font-weight: bold">Group:</td><td>pack</td><td>cluster</td><td>school</td><td>scurry</td><td>knot</td></tr>
</table>
</body></html>
Which, being translated, is: