|
With the following macro definition in place:
m4_define([m4_ignore],) |
you can add comments to your m4 source code that do not appear in the H.T.M.L. output. Conventional H.T.M.L. comments <!-- ... --> can be viewed in a Web browser by selecting the View Source (Internet Explorer) or View Page Source (Mozilla Firefox) menu button. Calls to the m4_ignore macro such as
m4_ignore(I do not show up in the H.T.M.L. code)
|
do not show up in the H.T.M.L. output and are therefore a safe place to put comments that you don't want the users of your Web pages to read, such as swear words and secret information.
With the following macro definition in place:
m4_define([m4_years],[m4_eval((((12 * 32 * m4_esyscmd(date +%Y) + 32 * (m4_esyscmd(date +%m) - 1) + m4_esyscmd(date +%d)) - (($1 * 12 * 32) + ($2 * 32) + $3))) / (12 * 32))]) |
The following macro call: m4_years(1973,06,17)
prints out
the number of years elapsed since my date of birth, i.e. my age in
years.
When writing Web tutorials (such as this one) it is useful to keep your questions and the answers to those questions in a single source file. In Question 6.15 you will write a macro ANSWER so that answers are not visible in question pages but are visible in the answer pages. For symmetry it is also useful to have a macro called QUESTION so that your single source file can look something like this:
QUESTION(1, What is George Bernard Shaw's middle name?) ANSWER(1, Bernard) QUESTION(2, How do you spell H.T.M.L.?) ANSWER(2, H.T.M.L.) QUESTION(3, What is the square root of 144?) ANSWER(3, 12) QUESTION(4, How many loaves are there in a baker's dozen?) ANSWER(4, 13) QUESTION(5, What is the meaning of life?) ANSWER(5, 42) |
With the following definitions in place:
m4_define([H2_COUNT],0) m4_define([H2],[m4_define([H2_COUNT],m4_incr([H2_COUNT]) <h2>H2_COUNT. $1 </h2>)]) |
You can write the following m4 code:
H2(First section) H2(Second section) H2(Conclusion) |
and it will generate the following H.T.M.L. code:
<h2>1. First section</h2> <h2>2. Second section</h2> <h2>3. Conclusion</h2> |
†
(the H.T.M.L. code for
dagger).
ö
. The dumb way to do this is
by replacing ever occurrence of Godel with Gö
del.
However this makes your H.T.M.L. quite ugly to read. The smart way to
to define an m4 macro "Godel" so that every occurrence of Godel is
replaced by Gö
del. Define such a macro. This is a
rare case where your macro name should not be in all capitals. Run
m4 on the source file by changing to the tutorial-6 folder in
Emacs and pressing F9.
&xxx;
characters. To do this you will need the
ï
character. Like Godel (above), this is a rare case
where your macro name should not be in all capitals.
You can find the image file under-const.png in the tutorial-6 folder.
<a href="http://www.davinpearson.com"><code>www.davinpearson.com</code></a>
This makes it easier to add links to Websites in your H.T.M.L. code.
<font color="red"><b>Here is some text</b></font>
<table align=center> ENTRY(apple.jpg, 200, 186, This is a picture of an apple.) ENTRY(banana.jpg, 200, 150, This is a picture of a banana.) ENTRY(carrot.jpg, 200, 150, This is a picture of a carrot.) </table> |
expands to the following:
<table align=center> <tr> <td> <img src="apple.jpg" width=200 height=186 alt="apple"> </td> <td> This is a picture of an apple. </td> </tr> <tr> <td> <img src="banana.jpg" width=200 height=150 alt="banana"> </td> <td> This is a picture of a banana. </td> </tr> <tr> <td> <img src="carrot.jpg" width=200 height=150 alt="carrot"> </td> <td> This is a picture of a carrot. </td> </tr> </table> |
|
||||||
The code in Emacs. | The code in a Web browser. |
NOTE: the second ($2) and third ($3) arguments represent the width and height of the image, respectively.
<table align=center> <tr> <td> <img src="apple.jpg" width=200 height=186 alt="apple"><br> <center> This is a picture of an apple. </center> </td> </tr> <tr> <td> <img src="banana.jpg" width=200 height=150 alt="banana"><br> <center> This is a picture of a banana. </center> </td> </tr> <tr> <td> <img src="carrot.jpg" width=200 height=150 alt="carrot"><br> <center> This is a picture of a carrot. </center> </td> </tr> </table> |
|
|||
The code in Emacs. | The code in a Web browser. |
What is the advantage in separating the logic from the layout? (As is done in the above.)
<table align=center> ENTRY(apple.jpg, 200, 186, This is a picture, a picture of an apple.) ENTRY(banana.jpg, 200, 150, This is a picture of a banana.) ENTRY(carrot.jpg, 200, 150, This is a picture of a carrot.) </table> |
Use the definition of the ENTRY symbol from the previous question and run m4 on the H.T.S. source file. When you load the corresponding H.T.M.L. code into a Web browser you will notice that only part of the sentence for the first call to the ENTRY macro is printed. Your task is to change the above code so that the full sentence is printed out. I can think of four ways of doing this: Two using square brackets to quote the fourth argument, one using the H.T.M.L. Ascii code for the comma character and the fourth using the COMMA macro that you learnt about in Section Section § 4.9. See if you can think of all of the ways. A bonus mark is available for anyone who can think of another different way!
m4_define([COL1],#ffffee) m4_define([COL2],#ffeedd) |
to the following code:
<table border=1 width="25%" align=center> <tr><td bgcolor="#ffffee">Apple</td></tr> <tr><td bgcolor="#ffeedd">Banana</td></tr> <tr><td bgcolor="#ffffee">Carrot</td></tr> <tr><td bgcolor="#ffeedd">Dog</td></tr> <tr><td bgcolor="#ffffee">Egg</td></tr> <tr><td bgcolor="#ffeedd">Fish</td></tr> </table> |
|
||||||
The code in Emacs. | The code in a Web browser. |
so that every occurrence of #ffffee is replaced by COL1 and every occurrence of #ffeedd is replaced by COL2. What is the advantage achieved by making this change?
m4_define([QUESTION],<p><b>Question $1:</b>$2<p>)
|
Create a file answers.hts which contains the following m4 code:
m4_define([ANSWER],<p><b>Answer $1:</b>$2<p>) m4_include(both.m4) |
When you run m4 on this file it creates a file called answers.html containing the questions and the answers to those questions. The next step is the challenge: Write another file called questions.hts which is like the above file (answers.hts), except that the answers do not show up. You will need to redefine the ANSWER macro to do this.
BEGIN_CENTER <pre> Hello Adam Hello Babara Hello Colin </pre> END_CENTER |
shows up centralised in a Web browser like so:
Hello Adam Hello Babara Hello Colin |
These macros are useful for centering diagrams, equations and other special text. I use them a lot for these tutorials. You will need to use the following H.T.M.L. tags to get the right result: <table>, <tr>, <td> and possibly <center>.
m4_define([m4_ignore],) |
write some m4 code that evaluates to m4_ignore without
calling the macro m4_ignore. The seemingly obvious way to do
this is to use square brackets [
and ]
to quote the
macro call. But this technique fails if you are inside one or more
macro calls. Inside more macro calls needs more square brackets so
in general it is difficult to use the quoting technique. I can
think of two better ways to do this. The first and best way is to
use the H.T.M.L. Ascii code for one
of the letters in the symbol m4_ignore so that it doesn't
evaluate the macro. The second (not as good) way is to temporarily
undefine the m4_ignore macro and redefine it afterwards.
m4_patsubst(hello@example.com, \.,
<i> dot </i>
)
expands to hello@example <i> dot </i> com.
m4_patsubst(hello@example.com, @,
<i> at </i>
)
expands to hello <i> at </i> example.com.
Back to Web Design Course |
This page has the following hit count:
|