KNOW HOW GNU m4
GNU m4 Creating HTML pages m4 is a macro language used for text processing. It simply copies its input to its output, while expanding built-in or user-defined macros. It can also capture output from standard Linux commands. BY STIG BRAUTASET
e will cover the basics of m4 configure script familiar to anyone At this point you may ask “What’s the by separating out content and installing software directly from source. point then? I may as well be writing the Wlayout of HTML web pages. As mentioned earlier, m4 allows us to HTML code in the usual way, instead of The techniques shown are not by any easily define our own macros, and this is using this m4 mumbo-jumbo!” The means restricted to HTML, but are the feature we will focus on. We will astute reader would, of course, be 100% applicable elsewhere as well. learn how to hide layout specifics, long- correct in this observation. However, winded code or arcane syntax behind read on as the benefits will be explained Before we begin… our own simple macros. in the next couple of paragraphs. Very basic knowledge of HTML would be Consider a snip of HTML you write a lot; beneficial, but should not be necessary to How m4 can help you write a simple example is the code for creating read and comprehend the material HTML links. Chances are it will be like this: covered in this article. It is m4’s There are no (to the author’s knowledge) capabilities as a macro processor ready-made macros for writing HTML, U language the author wishes to communi- so we we will have to write them ourself. www.w3.org cate; HTML was chosen because it is something most people should be at least Naming conventions vaguely familiar with. When splitting the content from the layout of web pages, the author prefers to call the common What is m4? macro file for html.m4.The content of each page goes in a file with the ending “.mac”,e.g. index.mac and so on.When explaining this,however,we develop our macro and .mac files bit by m4 is a macro processor used by bit,so we need to refer to several different files.Thus it is convenient to name them first.m4, Sendmail and GNU Autoconf, etc. first.mac and so on. Sendmail relies on m4 for creating its See the sidebar “Joining the content and layout”how to create the resulting HTML file from the (in)famous sendmail.cf configuration file macros and the content. while Autoconf uses m4 to create the
40 November 2002 www.linux-magazine.com GNU m4 KNOW HOW
Now, what if we instead define a simple Comments: documenting The only change is that we now have macro that will allow us to write “$1” and “$2” instead of “$*”. “$1” and our macros “$2” refer to the first and the second __elink(www.w3.org) If a macro is not self-explanatory,we would argument of our macro. The arguments like to put an explanatory comment along are separated by the comma character. side the macro definition. m4 naturally and let m4 do the tedious job of filling in So, Sherlock, what now if we want to allows us to do this; it provides the built-in the necessary bits? That’s less than half create a macro that can take an dnl which reads and discards all characters, the number of characters already. up to and including the first newline. argument with a comma in it? That, my Additionally, observe that we only had to good Watson, is simple. We just have to dnl Iamanexample comment. write the link name once, so there is less put quote the argument. When you dnl Iamhighly unhelpful, chance of us spelling it wrong. quote something, everything between dnl but 100% correct. Another example is if we have a note the quote-characters will be treated as a Using dnl as part of a string does not exhibit on our page telling visitors the date of single argument, even if it consist in this behaviour. the last update. It is tedious work entirety of a string of, say, 90 commas. searching through all the files you have The default opening quote is a “`” changed, searching for and updating body. We will refer to the whole line as (back-tick) and the default closing quote date stamps. Instead we can simply the macro definition. The definition line is a “‘” (single quote). We can now define a macro named, say, __today that above in English: “Define a new macro invoke __link2 with a comma in the will expand into today’s date. The named __link. Everywhere where this second argument thus: search-and-replace business will then be macro occurs, substitute the macro name taken care of for us automatically. How for the body of the macro, but substitute __link2(www.gnu.org, U to do this will be shown later; we need to the macro’s arguments (whatever is 'www.GNU.org, the home of much U take care of the basics first. inside the parentheses following the software') macro name) for “$*” wherever “$*” Getting your hands dirty occurs in the macro body.” The second comma is now quoted, so As mentioned earlier, m4 allow us to We will store the macros we write the macro is indeed invoked with only define our own macros with ease. The ourselves, such as the above, in a file two arguments. Note that only one layer command to let us do this is cunningly called html.m4. Se sidebar “Comments: named define. Here’s how to define a documenting our macros” for details Listing 1: sample.html macro to let us use the link shorthand about how we can mix comments and above: macros in this file.
It’s worth noting that macro names do $*) is just a convention, because we need to 8859-1"> make sure that we do not pick a string the parentheses) is the macro name, and Otherwise we may get spurious layoutHello, World
name with. Considering again the $m4html.m4 index.mac > example with the __link macro above, index.html This invokes the m4 processor with two what should we do if we don’t want to arguments.The m4 command will take the use the URL as the visible, “clickable” macro definitions it finds,do the necessary link? We could simply create a new Listing 2: first.mac macro that takes several arguments, and substitutions and output the result on its __title({Hello World, HTML U standard output.However,we make use of invoke it thus (in context this time, just version}) the shell’s redirection facilities to make the to show that it is possible): “Here isHello World
output go to a file instead of the screen (if __link2(www.w3.org, a link to w3.org).Look at this link: U this makes no sense to you,just tag along It is an informative website.” Here’s how __link(www.w3.org)
and follow the directions,but you should to define such a beast: consider reading up on shell basics).NowLast updated: __today
open first.html in a browser,and voil! We