GNU   davin.50webs.com/webdesign
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Dec/Hex Codes H.T.M.L. ASCII Codes Using Emacs Download Links


Web design course tutorial 6 : smart use of m4

Using the m4 language cleverly you can eliminate most of the tiresome repetitive work that is needed to build a complex Website. This tutorial will take you through some uses of m4 for this purpose.

§ 6.1 Smart comments

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.

§ 6.2 Number of years since a given date

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.

§ 6.3 Questions and answers in the same source file

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)

§ 6.4 Two versions of your CV

By moving most of the code from your CV from cv.hts to cv.hts.m4 you can have two versions of your CV, once called cv.hts for your Website, and another called cv-private.hts containing private information and more suitable for printing than your public CV. Your private CV can differ in the following ways from your public CV:

§ 6.5 Automatic numbering of sections

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>
I use a similar system to this for these Web tutorials.

§ 6.6 Questions

You can view the answers to this tutorial. A password is required.

Back to Web Design Course
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Dec/Hex Codes | H.T.M.L. ASCII Codes | Using Emacs | Download Links
Last modified: Sat 20 Aug 2022 16:54:12 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2022 Davin Pearson.
Please report any broken links to