mp4h.mp4h 67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
;;;
;;;  mp4h.mp4h -- Documentation for mp4h
;;;  Copyright 2000-2003, Denis Barbier   <barbier@linuxfr.org>
;;;

<set-var package=mp4h />
<set-var title="mp4h - Macro Processor for HTML Documents" />

<defvar srcdir "." />
<include file=<get-var format />.out
    alt=<exit status=1
        message="This file has to be compiled with one of the following lines
   ../src/mp4h -D format=html -D srcdir="<get-var srcdir />" <get-var srcdir />/mp4h.mp4h > mp4h.html
   ../src/mp4h -D format=pod -D srcdir="<get-var srcdir />" <get-var srcdir />/mp4h.mp4h | \\
   sed -e 's/_LT_/</g' -e 's/_GT_/>/g'  > mp4h.pod
" />
/>

<include file=list-tags />

;;;
;;;   We need to change comment characters to view original source code
;;;
<set-eol-comment "###" />

<header />
<section alt="Brief history and goals of mp4h">Introduction</section>

<para>
The &mp4h; software is a macro-processor specifically designed to deal
with HTML documents.  It allows powerful programming constructs, with a
syntax familiar to HTML authors.
</para>

<para>
This software is based on
<URL src="http://www.metahtml.org/" name="&Meta-HTML;" />,
written by Brian J. Fox,  Even if both syntaxes look similar, source
code is completely different.  Indeed, a subset of &Meta-HTML; was used
as a part of a more complex program, &WML;
41
(<URL src="http://www.engelschall.com/sw/wml/" name="Website Meta Language" />)
42 43 44 45 46 47 48 49 50
written by Ralf S. Engelschall and which I maintain since January 1999.
For licensing reasons, it was hard to hack &Meta-HTML; and so I decided
to write my own macro-processor.
</para>

<para>
Instead of rewriting it from scratch, I preferred using another
macro-processor engine. I chose
<URL src="http://www.gnu.org/software/m4/" name="GNU &m4;" />,
51
written by René Seindal, because of its numerous advantages :
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
this software is stable, robust and very well documented.
This version of &mp4h; is derived from GNU &m4; version 1.4n, which is a
development version.
</para>

<para>
The &mp4h; software is not an HTML editor; its unique goal is to
provide an easy way to define its own macros inside HTML documents.
There is no plan to add functionalities to automagically produce valid
HTML documents, if you want to clean up your code or validate it,
simply use a post-processor like
<URL src="http://www.w3.org/People/Raggett/tidy/" name="tidy" />.
</para>

<section alt="Invoking mp4h">Command line options</section>

<para>
Optional arguments are enclosed within square brackets.
All option synonyms have a similar syntax, so when a long option accepts
an argument, short option do too.
</para>

<para>
Syntax call is
</para>

<code>mp4h [options] [filename [filename] ...]</code>

<para>
Options are described below.  If no filename is specified, or if its
name is <file - />, then characters are read on standard input.
</para>

<subsection>Operation modes</subsection>

<option-list>
<option --help>display an help message and exit</option>
<option --version>output &mp4h; version information and exit</option>
<option -E --fatal-warnings>stop execution after first warning</option>
<option -Q --quiet --silent>suppress some warnings for builtins</option>
<option -S --safety-level=<optmand NUMBER />>disable risky functions;
0 means no filtering, 1 disable <command execute /> and 2 disable this
one too plus all filesystem related functions:
<command file-exists />,
<command real-path />,
<command get-file-properties />,
<command directory-contents /> and
<command include />.
</option>
</option-list>

<subsection>Preprocessor features</subsection>

<option-list>
<option -I --include=<optmand DIRECTORY />>###
search this directory for includes and packages</option>
<option -D --define=<optmand NAME<optopt =VALUE /> />>###
set variable NAME to VALUE, or empty</option>
<option -U --undefine=<optmand COMMAND />>###
delete builtin COMMAND</option>
<option -s --synclines>generate `#line NO "FILE"' lines</option>
</option-list>

<subsection>Parser features</subsection>

<option-list>
<option -c --caseless=<optmand NUMBER />>###
set case sensitiveness according to the bits of <optmand NUMBER />.
A null bit means symbol is case sensitive, and bits are defined as
followed: 0 for tags, 1 for variables and 2 for entities.
Default value is 3, i.e. only entities are case sensitive.</option>
<option -e --encoding=<optmand NAME />>###
specify document encoding.  Valid options are `8bit' (default) or `utf8'.</option>
<option -X --expansion=<optmand NUMBER />>###
set parser behaviour according to the bits of <optmand NUMBER /></option>
</option-list>

<para>
NUMBER is a combination of
</para>

<option-list>
<option 1>do not parse unknown tags</option>
<option 2>unknown tags are assumed being simple</option>
<option 4>trailing star in tag name do not make this tag simple</option>
<option 8>an unmatched end tag closes all previous unmatched begin tags</option>
<option 16>interpret backslashes as printf</option>
<option 32>remove trailing slash in tag attributes</option>
<option 64>do not remove trailing star in tag name</option>
<option 128>do not remove leading star in tag name</option>
<option 256>do not add a space before trailing slash in tag attributes</option>
<option 1024>suppress warnings about bad nested tags</option>
<option 2048>suppress warnings about missing trailing slash</option>
</option-list>

<para>
In version <__version__ />, default value is 3114=2+8+32+1024+2048.
</para>

<subsection>Limits control</subsection>

<option-list>
<option -H --hashsize=<optmand PRIME />>###
set symbol lookup hash table size (default 509)</option>
<option -L -nesting-limit=<optmand NUMBER />>###
change artificial nesting limit (default 250)</option>
</option-list>

<subsection>Debugging</subsection>

<option-list>
<option -d --debug=<optmand FLAGS />>###
set debug level (no FLAGS implies `aeq')</option>
<option -t --trace=<optmand NAME />>###
trace NAME when it will be defined</option>
<option -l --arglength=<optmand NUMBER />>###
restrict macro tracing size</option>
<option -o --error-output=<optmand FILE />>###
redirect debug and trace output</option>
</option-list>

<para>
Flags are any of:
</para>

<option-list>
<option t>trace for all macro calls, not only debugging-on'ed</option>
<option a>show actual arguments</option>
<option e>show expansion</option>
<option c>show before collect, after collect and after call</option>
<option x>add a unique macro call id, useful with c flag</option>
<option f>say current input file name</option>
<option l>say current input line number</option>
<option p>show results of path searches</option>
<option m>show results of module operations</option>
<option i>show changes in input files</option>
<option V>shorthand for all of the above flags</option>
</option-list>

<section alt="List of primitives">Description</section>

<para>
The &mp4h; software is a macro-processor, which means that keywords are
replaced by other text.  This chapter describes all primitives.
As &mp4h; has been specially designed for HTML documents, its syntax is
very similar to HTML, with tags and attributes.  One important feature
has no equivalent in HTML: comments until end of line.  All text
following three colons is discarded until end of line, like
</para>

<code>;;;  This is a comment</code>


###
###    Defining new macros
###

<subsection>Function Macros</subsection>

<note>
All examples in this documentation are processed through &mp4h; with
expansion flags set to zero (see a description of possible expansion
flags at the end of document), it is why simple tags contain a trailing
slash. But &mp4h; can output plain HTML files with other expansion
flags.
</note>

<para>
The definition of new tags is the most common task provided by &mp4h;.
As with HTML, macro names are case insensitive, unless <optflag c />
option is used to change this default behaviour.  In this documentation,
only lowercase letters are used.
There are two kinds of tags: simple and complex. A simple tag has the
following form:
</para>

<code><name [attributes]/></code>

whereas a complex tag looks like:

<code><name [attributes]>
body
</name></code>

<para>
Since version 0.9.1, &mp4h; knows XHTML syntax too, so your input file
may conform to HTML or XHTML syntax.  In this manual, we adopt the
latter, which is why simple tags have a trailing slash in attributes.
If you want to produce HTML files with this input file, you may either
choose an adequate <optflag -expansion /> flag or use a post-processor like
<URL src="http://www.w3.org/People/Raggett/tidy/" name="tidy" />.
</para>

<para>
When a simple tag is defined by &mp4h;, it can be parsed even if the
trailing slash is omitted, because &mp4h; knows that this tag is
simple.  But it is a good practice to always append a trailing slash to
simple tags.
</para>

<para>
<anchor name="noteattr" />
In macro descriptions below, a slash indicates a simple tag, and a V
letter that attributes are read verbatim (without expansion) (see the
chapter on macro expansion for further details).
</para>

<pseudo-func foo />
<tag:description define-tag complex>
<var name />
[attributes=verbatim]
[endtag=required]
[whitespace=delete]
</tag:description>

<para>
This function lets you define your own tags.  First argument is the
command name. Replacement text is the function body.
</para>

<example>
<define-tag foo>bar</define-tag>
<foo />
</example>

<para>
Even if spaces have usually few incidence on HTML syntax, it is
important to note that
</para>

<code><define-tag foo>bar</define-tag></code>

and

<code><define-tag foo>
bar
</define-tag></code>

are not equivalent, the latter form contains two newlines that were not
present in the former.

<pseudo-func foo />
<attributes:description>
<attributes:item "whitespace=delete">
Some spaces are suppressed in replacement text, in particular any
leading or trailing spaces, and newlines not enclosed within angle
brackets.
</attributes:item>

<attributes:item "endtag=required">
Define a complex tag

<pseudo-func foo />
<example>
<define-tag foo>bar</define-tag>
<foo />
</example>

<undef bar />
<example>
<define-tag bar endtag=required>;;;
body is: %body</define-tag>
<bar>Here it is</bar>
</example>
</attributes:item>

<attributes:item "attributes=verbatim">
By default attributes are expanded before text is replaced. If this
attribute is used, attributes are inserted into replacement text without
expansion.

<pseudo-func foo />
<example>
<define-tag foo>quux</define-tag>
<define-tag bar attributes=verbatim endtag=required>
Body: %Ubody
Attributes: %Uattributes
</define-tag>
<bar txt="<foo/>">Here we go</bar>
</example>
</attributes:item>
</attributes:description>

<tag:description provide-tag complex>
<var name />
[attributes=verbatim]
[endtag=required]
[whitespace=delete]
</tag:description>

<para>
This command is similar to the previous one, except that no operation is
performed if this command was already defined.
</para>

<tag:description let>
<var new />=<var old />
</tag:description>

<para>
Copy a function. This command is useful to save a macro definition
before redefining it.
</para>

<pseudo-func foo bar />
<example>
<define-tag foo>one</define-tag>
<let bar=foo />
<define-tag foo>two</define-tag>
<foo/><bar/>
</example>

<tag:description undef>
<var name />
</tag:description>

<para>
Delete a command definition.
</para>

<pseudo-func foo />
<example>
<define-tag foo>one</define-tag>
<undef foo />
<foo/>
</example>

<tag:description set-hook complex>
<var name />
[position=before|after]
[action=insert|append|replace]
</tag:description>

<para>
Add text to a predefined macro. This mechanism allows modifications
of existing macros without having to worry about its type, whether it is
complex or not.
</para>

<pseudo-func foo />
<example>
<let foo=add />
<set-hook foo position=before>
Before</set-hook>
<set-hook foo position=after>
After</set-hook>
<foo 1 2 3 4 />
</example>

<tag:description get-hook>
<var name />
[position=before|after]
</tag:description>

<para>
Print current hooks of a macro.
</para>

<example>
Text inserted with position=before:<get-hook foo position=before />!
Text inserted with position=after:<get-hook foo position=after />!
</example>

<tag:description attributes-quote>
<value %attributes />
</tag:description>

<para>
Like <value %attributes />, except that <value attr=value /> pairs are
printed with double quotes surrounding attribute values, and a leading
space is added if some text is printed.
</para>

<undef img />
<example>
<define-tag foo>;;;
%attributes
<img<attributes-quote %attributes />/>
</define-tag>
<foo id="logo" src="logo.gif" name="Logo" alt="Our logo" />
<foo />
</example>

<tag:description attributes-extract>
<var name1 />
[,<var name2 />[,<var name3 />...]]
<value %attributes />
</tag:description>

<para>
Extract from <value %attributes /> the <value attr=value /> pairs for
names matching any of <var name1 />, <var name2 />....
</para>

<example>
<define-tag img whitespace=delete>
<img* <attributes-extract name,src,alt %attributes /> />
</define-tag>
<img id="logo" src="logo.gif" name="Logo" alt="Our logo" />
</example>
<undef img />

<tag:description attributes-remove>
<var name1 />
[,<var name2 />[,<var name3 />...]]
<value %attributes />
</tag:description>

<para>
Remove from <value %attributes /> the <value attr=value /> pairs for
names matching any of <var name1 />, <var name2 />....
</para>

<example>
<define-tag img whitespace=delete>
<img* <attributes-quote <attributes-remove name,src,alt %attributes />/>/>
</define-tag>
<img id="logo" src="logo.gif" name="Logo" alt="Our logo" />
</example>
<undef img />

<note>
The two previous functions are special, because unlike all other macros,
their expansion do not form a group.  This is necessary to parse the
resulting list of attributes.
</note>

<para>
In those two functions, names of attributes may be regular expressions.
Main goal of these primitives is to help writing macros accepting any
kind of attributes without having to declare them. A canonical example
is
</para>
<example>
<define-tag href whitespace=delete>
<preserve url name />
<set-var <attributes-extract url,name %attributes />/>
<a <attributes-quote <attributes-remove url,name %attributes />/>
   href="<get-var url />"><get-var name /></a>
<restore  url name />
</define-tag>
<href class=web url="http://www.foo.com" name="Welcome" />
</example>

<para>
But we want now to add an image attribute.  So we may write
</para>
<example>
<define-tag href whitespace=delete>
<preserve url name image />
<set-var <attributes-extract url,name,image %attributes />/>
<a <attributes-quote <attributes-remove url,name,image %attributes />/>
   href="<get-var url />">
<if <get-var image />
   <img <attributes-quote <attributes-remove url,name,image %attributes />/>
      src="<get-var image />" alt="<get-var name />" border=0 />
  <get-var name />
/>
</a>
<restore  url name image />
</define-tag>
<href class=web url="http://www.foo.com" name="Welcome" image="foo.png"/>
</example>

<para>
We need a mechanism to tell &mp4h; that some attributes refer to
specific HTML tags.  A solution is to prepend attribute with tag name,
e.g.
</para>
<example>
<define-tag href whitespace=delete>
<preserve url name image />
<set-var <attributes-extract url,name,image %attributes />/>
<a <attributes-quote <attributes-extract a:.* %attributes />/>
   href="<get-var url />">
<if <get-var image />
   <img <attributes-quote <attributes-extract img:.* %attributes />/>
      src="<get-var image />" alt="<get-var name />" />
  <get-var name />
/>
</a>
<restore  url name image />
</define-tag>
<href a:class=web img:id=logo img:border=1
      url="http://www.foo.com" name="Welcome" image="foo.png" />
</example>

<para>
This example shows that regular expressions may be used within
attributes names, but it is still incomplete, because we want to remove
prefix from attributes.  One solution is with <command subst-in-string />,
but there is a more elegant one:
</para>

<example>
<define-tag href whitespace=delete>
<preserve url name image />
<set-var <attributes-extract url,name,image %attributes />/>
<a <attributes-quote <attributes-extract :a:(.*) %attributes />/>
   href="<get-var url />">
<if <get-var image />
   <img <attributes-quote <attributes-extract :img:(.*) %attributes />/>
      src="<get-var image />" alt="<get-var name />" />
  <get-var name />
/>
</a>
<restore  url name image />
</define-tag>
<href :a:class=web :img:id=logo :img:border=1
      url="http://www.foo.com" name="Welcome" image="foo.png" />
</example>

<para>
When there are subexpressions within regular expressions, they are
printed instead of the whole expression.  Note also that I put a colon
before the prefix in order not to mix them with XML namespaces.
</para>

###
###    Entity functions
###

<subsection>Entities</subsection>

<para>
Entities are macros in the same way as tags, but they do not take
any arguments. Whereas tags are normally used to mark up text,
entities contain already marked up text.  Also note that unlike tags,
entities are by default case sensitive.
</para>

<para>
An entity has the following form:
</para>

<code>&entity;</code>

<tag:description define-entity complex>
<var name />
</tag:description>

<para>
This function lets you define your own entities.
First argument is the entity name. Replacement text is the function body.
</para>

<example>
<define-entity foo>bar</define-entity>
&foo;
</example>

###
###    Variable functions
###

<subsection>Variables</subsection>

<para>
Variables are a special case of simple tags, because they do not accept
attributes. In fact their use is different, because variables contain
text whereas macros act like operators.
A nice feature concerning variables is their manipulation as arrays.
Indeed variables can be considered like newline separated lists, which
will allow powerful manipulation functions as we will see below.
</para>

<tag:description set-var>
<var name />[=<var value />]
[<var name />[=<var value />]] ...
</tag:description>

<para>
This command sets variables.
</para>

<tag:description set-var-verbatim verbatim>
<var name />[=<var value />]
[<var name />[=<var value />]] ...
</tag:description>

<para>
As above but attributes are read verbatim.
</para>

<tag:description set-var-x complex>
name=<var variable-name />
</tag:description>

<para>
This command assigns a variable the value of the body of the
command. This is particularly useful when variable values contain
newlines and/or quotes.
</para>

<para>
Note that the variable can not be indexed with this command.
Note also, that this command behaves as set-var-verbatim: The body
is not expanded until the variable is shown with get-var.
</para>

<tag:description get-var>
<var name />
[<var name />] ...
</tag:description>

<para>
Show variable contents. If a numeric value within square brackets is
appended to a variable name, it represents the index of an array.
The first index of arrays is 0 by convention.
</para>

<example>
<set-var version="0.10.1" />
This is version <get-var version />
<set-var-x name="osversion">Operating sytem is
"<include command="uname" /><include command="uname -r" />"</set-var-x>
<get-var osversion />
</example>

<example>
<set-var foo="0
1
2
3" />
<get-var foo[2] foo[0] foo />
</example>

<tag:description get-var-once verbatim>
<var name />
[<var name />] ...
</tag:description>

<para>
As above but attributes are not expanded.
</para>

<example>
<define-tag foo>0.10.1</define-tag>
<set-var version="<foo/>" />;;;
Here is version <get-var version />
<set-var-verbatim version="<foo/>" />;;;
Here is version <get-var version />
<set-var-verbatim version="<foo/>" />;;;
Here is version <get-var-once version />
</example>

<tag:description preserve>
<var name />
[<var name />] ...
</tag:description>

<para>
All variables are global, there is no variable or macro scope.
For this reason a stack is used to preserve variables.  When this
command is invoked, arguments are names of variables, whose values
are put at the top of the stack and variables are reset to an
empty string.
</para>

<tag:description restore>
<var name />
[<var name />] ...
</tag:description>

<para>
This is the opposite: arguments are names of variables, which are set to
the value found at the top of the stack, and stack is popped down.
</para>

<note>
The <command preserve /> tag pushes its last argument first, whereas
<command restore /> first pops its first argument.
</note>

<pseudo-func foo />
<example>
<define-tag foo whitespace=delete>
<preserve src name text />
<set-var %attributes />
Inside: src=<get-var src /> name=<get-var name /> text=<get-var text />
<restore  src name text />
</define-tag>
<set-var src=foo.png text="Hello, World!" />
Before: src=<get-var src /> name=<get-var name /> text=<get-var text />
<foo src=bar name=quux />
After: src=<get-var src /> name=<get-var name /> text=<get-var text />
</example>

<tag:description unset-var>
<var name />
[<var name />] ...
</tag:description>

<para>
Undefine variables.
</para>

<tag:description var-exists>
<var name />
</tag:description>

<para>
Returns <true/> when this variable exists.
</para>

<tag:description increment>
<var name />
[by=<var value />]
</tag:description>

<para>
Increment the variable whose name is the first argument.  Default
increment is one.
</para>

<attributes:description>
<attributes:item "by=<var value />">
Change increment amount.
</attributes:item>
</attributes:description>

<example>
<set-var i=10 />
<get-var i />
<increment i /><get-var i />
<increment i by="-3" /><get-var i />
</example>

<tag:description decrement>
<var name />
[by=<var value />]
</tag:description>

<para>
Decrement the variable whose name is the first argument.  Default
decrement is one.
</para>

<attributes:description>
<attributes:item "by=<var value />">
Change decrement amount.
</attributes:item>
</attributes:description>

<example>
<set-var i=10 />
<get-var i />
<decrement i /><get-var i />
<decrement i by="3" /><get-var i />
</example>

<tag:description copy-var>
<var src />
<var dest />
</tag:description>

<para>
Copy a variable into another.
</para>

<example>
<set-var i=10 />
<copy-var i j />
<get-var j />
</example>

<tag:description defvar>
<var name />
<var value />
</tag:description>

<para>
If this variable is not defined or is defined to an empty string, then
it is set to the second argument.
</para>

<example>
<unset-var title />
<defvar title "Title" /><get-var title />
<defvar title "New title" /><get-var title />
</example>

<tag:description symbol-info>
<var name />
</tag:description>

<para>
839
Show informations on symbols.  If it is a variable name, the <command
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
STRING /> word is printed as well as the number of lines contained within
this variable.

If it is a macro name, one of the following messages is printed:
<command PRIM COMPLEX />, <command PRIM TAG />,
<command USER COMPLEX /> or <command USER TAG />
</para>

<unset-var x foo bar symbol-info define-tag />
<pseudo-func foo bar />
<example>
<set-var x="0\n1\n2\n3\n4" />
<define-tag foo>bar</define-tag>
<define-tag bar endtag=required>quux</define-tag>
<symbol-info x />
<symbol-info symbol-info />
<symbol-info define-tag />
<symbol-info foo />
<symbol-info bar />
</example>
<undef bar />

###
###    String functions
###

<subsection>String Functions</subsection>

<tag:description string-length>
<var string />
</tag:description>

<para>
Prints the length of the string.
</para>

<example>
<set-var foo="0
1
2
3" />;;;
<string-length <get-var foo /> />
<set-var foo="0 1 2 3" />;;;
<set-var l=<string-length <get-var foo /> /> />;;;
<get-var l />
</example>

<tag:description downcase>
<var string />
</tag:description>

<para>
Convert to lowercase letters.
</para>

<example>
<downcase "Does it work?" />
</example>

<tag:description upcase>
<var string />
</tag:description>

<para>
Convert to uppercase letters.
</para>

<example>
<upcase "Does it work?" />
</example>

<tag:description capitalize>
<var string />
</tag:description>

<para>
Convert to a title, with a capital letter at the beginning of every
word.
</para>

<example>
<capitalize "Does it work?" />
</example>

<tag:description substring>
<var string />
[<var start /> [<var end />]]
</tag:description>

<para>
Extracts a substring from a string.  First argument is original string,
second and third are respectively start and end indexes.  By convention
first character has a null index.
</para>

<example>
<set-var foo="abcdefghijk" />
<substring <get-var foo /> 4 />
<substring <get-var foo /> 4 6 />
</example>

<tag:description string-eq>
<var string1 />
<var string2 />
[caseless=true]
</tag:description>

<para>
Returns <true/> if first two arguments are equal.
</para>

<example>
1:<string-eq "aAbBcC" "aabbcc" />
2:<string-eq "aAbBcC" "aAbBcC" />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<example>
1:<string-eq "aAbBcC" "aabbcc" caseless=true />
2:<string-eq "aAbBcC" "aAbBcC" caseless=true />
</example>

<tag:description string-neq>
<var string1 />
<var string2 />
[caseless=true]
</tag:description>

<para>
Returns <true/> if the first two arguments are not equal.
</para>

<example>
1:<string-neq "aAbBcC" "aabbcc" />
2:<string-neq "aAbBcC" "aAbBcC" />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<example>
1:<string-neq "aAbBcC" "aabbcc" caseless=true />
2:<string-neq "aAbBcC" "aAbBcC" caseless=true />
</example>

<tag:description string-compare>
<var string1 />
<var string2 />
[caseless=true]
</tag:description>

<para>
Compares two strings and returns one of the values less,
greater or equal depending on this comparison.
</para>

<example>
1:<string-compare "aAbBcC" "aabbcc" />
2:<string-compare "aAbBcC" "aAbBcC" />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<example>
1:<string-compare "aAbBcC" "aabbcc" caseless=true />
</example>

<tag:description char-offsets>
<var string />
<var character />
[caseless=true]
</tag:description>

<para>
Prints an array containing indexes where the character appear in the
string.
</para>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<example>
1:<char-offsets "abcdAbCdaBcD" a />
2:<char-offsets "abcdAbCdaBcD" a caseless=true />
</example>

<tag:description printf>
<var format />
<var string />
[<var string /> ...]
</tag:description>

<para>
Prints according to a given format. Currently only the
<command %s /> flag character is recognized, and  <command $ /> extension
is supported to change order of arguments.
</para>

<example>
1:<printf "foo %s bar %s" baz 10 />
2:<printf "foo %2$s bar %1$s" baz 10 />
</example>

###
###    Regexp functions
###

<subsection>Regular Expressions</subsection>

<para>
Regular expression support is provided by the PCRE (Perl Compatible
Regular Expressions) library package, which is open source software,
copyright by the University of Cambridge.
This is a very nice piece of software, latest versions are available at
<URL src="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/" />.
</para>

<para>
Before version 1.0.6, POSIX regular expressions were implemented.
For this reason, the following macros recognize two attributes,
<attribute caseless=true /> and <attribute singleline=true|false />.
But Perl allows a much better control on regular expressions with so
called modifiers, which are assed to the new <attribute reflags />
attribute.  It may contain one or more modifiers:
</para>

<option-list>
<option i>Matching is case insensitive</option>
<option m>Treat string as multiple lines. When set, a <value ^ /> matches
any beginning of line, and <value $ /> any end of line.  By default,
they match begin and end of string.</option>
<option s>Treat string as single line. A dot (<value . />) may also
match a newline, whereas it does not by default.</option>
<option x>Allow formatted regular expression, that means whitespaces,
newlines and comments are removed from regular expression before
processing.</option>
</option-list>

<note>
Attribute <attribute singleline=true /> is a synonym for the <attribute s />
modifier, whereas <attribute singleline=false /> is a synonym for the
<attribute m /> modifier.  This behaviour was different up to &mp4h; 1.0.6.
</note>

<tag:description subst-in-string>
<var string />
<var regexp />
[<var replacement />]
[caseless=true]
[singleline=true|false]
[reflags=[imsx]]
</tag:description>

<para>
Replace a regular expression in a string by a replacement text.
</para>

<example>
<set-var foo="abcdefghijk" />
<subst-in-string <get-var foo /> "[c-e]" />
<subst-in-string <get-var foo /> "([c-e])" "\\1 " />
</example>

<example>
<set-var foo="abcdefghijk\nabcdefghijk\nabcdefghijk" />
<subst-in-string <get-var foo /> ".$" "" />
<subst-in-string <get-var foo /> ".$" "" singleline=false />
<subst-in-string <get-var foo /> "
   ([a-c]) | [0-9]
     " ":\\1:" reflags=x />
</example>

<tag:description subst-in-var>
<var name />
<var regexp />
[<var replacement />]
[caseless=true]
[singleline=true|false]
[reflags=[imsx]]
</tag:description>

<para>
Performs substitutions inside variable content.
</para>

<tag:description match>
<var string />
<var regexp />
[caseless=true]
[singleline=true|false]
[reflags=[imsx]]
[action=report|extract|delete|startpos|endpos|length]
</tag:description>

<attributes:description>
<attributes:item "action=report">
Prints <true/> if string contains regexp.
</attributes:item>

<attributes:item "action=extract">
Prints the expression matching regexp in string.
</attributes:item>

<attributes:item "action=delete">
Prints the string without the expression matching regexp in string.
</attributes:item>

<attributes:item "action=startpos">
Prints the first char of the expression matching regexp in string.
If there is no match, returns <value -1 />.
</attributes:item>

<attributes:item "action=endpos">
Prints the last char of the expression matching regexp in string.
If there is no match, returns <value -1 />.
</attributes:item>

<attributes:item "action=length">
Prints the length of the expression matching regexp in string.
</attributes:item>
</attributes:description>

<example>
1:<match "abcdefghijk" "[c-e]+" />
2:<match "abcdefghijk" "[c-e]+" action=extract />
3:<match "abcdefghijk" "[c-e]+" action=delete />
4:<match "abcdefghijk" "[c-e]+" action=startpos />
5:<match "abcdefghijk" "[c-e]+" action=endpos />
6:<match "abcdefghijk" "[c-e]+" action=length />
</example>

###
###    Array functions
###

<subsection>Arrays</subsection>

<para>
With &mp4h; one can easily deal with string arrays. Variables can be
treated as a single value or as a newline separated list of strings.
Thus after defining
</para>

<code>
<set-var digits="0
1
2
3" />
</code>

one can view its content or one of these values:

<set-var digits="0\n1\n2\n3" />
<example>
<get-var digits />
<get-var digits[2] />
</example>

<tag:description array-size>
<var name />
</tag:description>

<para>
Returns an array size which is the number of lines present in the
variable.
</para>

<example>
<array-size digits />
</example>

<tag:description array-push>
<var name />
<var value />
</tag:description>

<para>
Add a value (or more if this value contains newlines) at the end of an
array.
</para>

<example>
<array-push digits "10\n11\n12" />
<get-var digits />
</example>

<tag:description array-pop>
<var name />
</tag:description>

<para>
Remove the toplevel value of an array and returns this string.
</para>

<tag:description array-topvalue>
<var name />
</tag:description>

<para>
Prints the last entry of an array.
</para>

<example>
<array-topvalue digits />
</example>

<tag:description array-add-unique>
<var name />
<var value />
[caseless=true]
</tag:description>

<para>
Add a value at the end of an array if this value is not already present
in this variable.
</para>

<example>
<array-add-unique digits 2 />
<get-var digits />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<tag:description array-concat>
<var name1 />
[<var name2 />] ...
</tag:description>

<para>
Concatenates all arrays into the first one.
</para>

<example>
<set-var foo="foo" />
<set-var bar="bar" />
<array-concat foo bar /><get-var foo />
</example>

<tag:description array-member>
<var name />
<var value />
[caseless=true]
</tag:description>

<para>
If value is contained in array, returns its index otherwise returns -1.
</para>

<example>
<array-member digits 11 />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>
</attributes:description>

<tag:description array-shift>
<var name />
<var offset />
[start=<var start />]
</tag:description>

<para>
Shifts an array.  If offset is negative, indexes below 0 are lost.
If offset is positive, first indexes are filled with empty strings.
</para>

<example>
<array-shift digits 2 />
Now: <get-var digits />
<array-shift digits -4 />
And: <get-var digits />
</example>

<attributes:description>
<attributes:item "start=<var start />">
Change origin of shifts (default is 0).
<example>
<array-shift digits -2 start=2 /><get-var digits />
</example>
</attributes:item>
</attributes:description>

<tag:description sort>
<var name />
[caseless=true]
[numeric=true]
[sortorder=reverse]
</tag:description>

<para>
Sort lines of an array in place. Default is to sort lines
alphabetically.
</para>

<example>
<sort digits /><get-var digits />
</example>

<attributes:description>
<attributes:item "caseless=true">
Comparison is case insensitive.
</attributes:item>

<attributes:item "numeric=true">
Sort lines numerically
<example>
<sort digits numeric=true /><get-var digits />
</example>
</attributes:item>

<attributes:item "sortorder=reverse">
Reverse sort order
<example>
<sort digits numeric=true sortorder=reverse />;;;
<get-var digits />
</example>
</attributes:item>
</attributes:description>

###
###    Numerical operators
###

<subsection>Numerical operators</subsection>

<para>
These operators perform basic arithmetic operations.  When all operands
are integers result is an integer too, otherwise it is a float.  These
operators are self-explanatory.
</para>

<tag:description add>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<tag:description substract>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<tag:description multiply>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<tag:description divide>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<tag:description min>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<tag:description max>
<var number1 />
<var number2 />
[<var number3 />] ...
</tag:description>

<example>
<add 1 2 3 4 5 6 />
<add 1 2 3 4 5 6. />
</example>

<pseudo-func factorial />
<example>
<define-tag factorial whitespace=delete>
<ifeq %0 1 1 <multiply %0 "<factorial <substract %0 1 /> />" /> />
</define-tag>
<factorial 6 />
</example>

<tag:description modulo>
<var number1 />
<var number2 />
</tag:description>

<para>
Unlike functions listed above the modulo function cannot handle more
than 2 arguments, and these arguments must be integers.
</para>

<example>
<modulo 345 7 />
</example>

<para>
Those functions compare two numbers and returns <true/> when this
comparison is true.  If one argument is not a number, comparison
is false.
</para>

<tag:description gt>
<var number1 />
<var number2 />
</tag:description>

<para>
Returns <true/> if first argument is greater than second.
</para>

<tag:description lt>
<var number1 />
<var number2 />
</tag:description>

<para>
Returns <true/> if first argument is lower than second.
</para>

<tag:description eq>
<var number1 />
<var number2 />
</tag:description>

<para>
Returns <true/> if arguments are equal.
</para>

<tag:description neq>
<var number1 />
<var number2 />
</tag:description>

<para>
Returns <true/> if arguments are not equal.
</para>

###
###    Relational operators
###

<subsection>Relational operators</subsection>

<tag:description not>
<var string />
</tag:description>

<para>
Returns <true/> if string is empty, otherwise returns an empty string.
</para>

<tag:description and>
<var string />
[<var string />] ...
</tag:description>

<para>
Returns the last argument if all arguments are non empty.
</para>

<tag:description or>
<var string />
[<var string />] ...
</tag:description>

<para>
Returns the first non empty argument.
</para>

###
###    Flow functions
###

<subsection>Flow functions</subsection>

<tag:description group verbatim>
<var expression />
[<var expression />] ...
[separator=<var string />]
</tag:description>

<para>
This function groups multiple statements into a single one.  Some
examples will be seen below with conditional operations.
</para>

<para>
A less intuitive but very helpful use of this macro is to preserve newlines
when <attribute whitespace=delete /> is specified.
</para>

<pseudo-func text1 text2 text3 />
<example>
<define-tag text1>
Text on
3 lines without
whitespace=delete
</define-tag>
<define-tag text2 whitespace=delete>
Text on
3 lines with
whitespace=delete
</define-tag>
<define-tag text3 whitespace=delete>
<group "Text on
3 lines with
whitespace=delete" />
</define-tag>
<text1 />
<text2 />
<text3 />
</example>

<para>
Note that newlines are suppressed in <value text2 /> and result is
certainly unwanted.
</para>

<tag:description compound complex>
<var expression />
[<var expression />] ...
[separator=<var string />]
</tag:description>

<para>
Like <command group />,  but this tag is complex.
</para>

<attributes:description>
<attributes:item "separator=<var string />">
By default arguments are put aside.  This attribute define a separator
inserted between arguments.
</attributes:item>
</attributes:description>

<tag:description disjoin>
<var expression />
</tag:description>

<para>
Does the opposite job to <command group />, its argument is no more
treated as a single object when processed by another command.
</para>

<tag:description noexpand verbatim>
<var command />
[<var command />] ...
</tag:description>

<para>
Prints its arguments without expansion.  They will never be expanded
unless the <command expand /> tag is used to cancel this <command
noexpand /> tag.
</para>

<tag:description expand>
<var command />
[<var command />] ...
</tag:description>

<para>
Cancels the <command noexpand /> tag.
</para>

<pseudo-func foo />
<example>
<subst-in-string "=LT=define-tag foo>bar=LT=/define-tag>" "=LT=" "<" />
<foo/>
<subst-in-string "=LT=define-tag foo>quux=LT=/define-tag>" "=LT="
   "<noexpand "<" />" />
<foo/>
</example>

<tag:description if verbatim>
<var string />
<var then-clause />
[<var else-clause />]
</tag:description>

<para>
If string is non empty, second argument is evaluated otherwise third
argument is evaluated.
</para>

<pseudo-func test />
<example>
<define-tag test whitespace=delete>
<if %0 "yes" "no" />
</define-tag>
<test "string" />
<test "" />
</example>

<tag:description ifeq verbatim>
<var string1 />
<var string2 />
<var then-clause />
[<var else-clause />]
</tag:description>

<para>
If first two arguments are identical strings, third argument is evaluated
otherwise fourth argument is evaluated.
</para>

<tag:description ifneq verbatim>
<var string1 />
<var string2 />
<var then-clause />
[<var else-clause />]
</tag:description>

<para>
If first two arguments are not identical strings, third argument is
evaluated otherwise fourth argument is evaluated.
</para>

<tag:description when complex>
<var string />
</tag:description>

<para>
When argument is not empty, its body is evaluated.
</para>

<tag:description while verbatim complex>
<var cond />
</tag:description>

<para>
While condition is true, body function is evaluated.
</para>

<example>
<set-var i=10 />
<while <gt <get-var i /> 0 />>;;;
  <get-var i /> <decrement i />;;;
</while>
</example>

<tag:description foreach complex>
<var variable />
<var array />
[start=<var start />]
[end=<var end />]
[step=<var pas />]
</tag:description>

<para>
This macro is similar to the <command foreach /> Perl's macro:
a variable loops over array values and function body is evaluated for each
value.

first argument is a generic variable name, and second is the name of an
array.
</para>

<example>
<set-var x="1\n2\n3\n4\n5\n6" />
<foreach i x><get-var i /> </foreach>
</example>

<attributes:description>
<attributes:item "start=<var start />">
Skips first indexes.
<example>
<set-var x="1\n2\n3\n4\n5\n6" />
<foreach i x start=3><get-var i /> </foreach>
</example>
</attributes:item>

<attributes:item "end=<var end />">
Stops after index has reached that value.
<example>
<set-var x="1\n2\n3\n4\n5\n6" />
<foreach i x end=3><get-var i /> </foreach>
</example>
</attributes:item>

<attributes:item "step=<var step />">
Change index increment (default is 1).
If step is negative, array is treated in reverse order.
<example>
<set-var x="1\n2\n3\n4\n5\n6" />
<foreach i x step=2><get-var i /> </foreach>
<foreach i x step=-2><get-var i /> </foreach>
</example>
</attributes:item>
</attributes:description>

<tag:description var-case verbatim>
<var var1 />=<var value1 /> <var action1 />
[<var var2 />=<var value2 /> <var action2 /> ...
</tag:description>

<pseudo-func test />
<para>
This command performs multiple conditions with a single instruction.
</para>

<example>
<set-var i=0 />
<define-tag test>
<var-case
  x=1   <group <increment i /> x<get-var i /> />
  x=2   <group <decrement i /> x<get-var i /> />
  y=1   <group <increment i /> y<get-var i /> />
  y=2   <group <decrement i /> y<get-var i /> />
/>
</define-tag>
<set-var x=1 y=2 /><test/>
<set-var x=0 y=2 /><test/>
</example>

<tag:description break />

<para>
Breaks the innermost <command while /> loop.
</para>

<example>
<set-var i=10 />
<while <gt <get-var i /> 0 />>;;;
  <get-var i /> <decrement i />;;;
  <ifeq <get-var i /> 5 <break/> />;;;
</while>
</example>

<tag:description return>
[up=number]
<var string />
</tag:description>

<para>
This command immediately exits from the innermost macro.
A message may also be inserted.  But this macro changes token parsing
so its use may become very hazardous in some situations.
</para>

<attributes:description>
<attributes:item "up=<var number />">
This attribute determines how much levels have to be exited.  By default
only one level is skipped.  With a null value, all current macros are
exited from.  A negative value do the same, and stops processing current
file.
</attributes:item>
</attributes:description>

<tag:description warning>
<var string />
</tag:description>

<para>
Prints a warning on standard error.
</para>

<tag:description exit>
[status=<var rc />]
[message=<var string />]
</tag:description>

<para>
Immediately exits program.
</para>

<attributes:description>
<attributes:item "message=<var string />">
Prints a message to the standard error.
</attributes:item>

<attributes:item "status=<var rc />">
Selects the code returned by the program (-1 by default).
</attributes:item>
</attributes:description>

<tag:description at-end-of-file complex />

<para>
This is a special command: its content is stored and will be
expanded after end of input.
</para>

###
###    File functions
###

<subsection>File functions</subsection>

<tag:description directory-contents>
<var dirname />
[matching=regexp]
</tag:description>

<para>
Returns a newline separated list of files contained in a given directory.
</para>

<example>
<directory-contents . matching=".*\\.mp4h$" />
</example>

<tag:description real-path>
patname=<var pathname />
</tag:description>

<para>
Resolves all symbolic links, extra ``/'' characters and references
to /./ and /../ in pathname, and expands into the resulting absolute pathname.
All but the last component of pathname must exist when real-path is called.
</para>

<para>
This tag is particularly useful when comparing if file or directory names
are identical.
</para>

<example>
<real-path pathname=<__file__/> />
</example>

<tag:description file-exists>
<var filename />
</tag:description>

<para>
Returns <true/> if file exists.
</para>

<tag:description get-file-properties>
<var filename />
</tag:description>

<para>
1895
Returns an array of informations on this file.  These informations are:
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
size, type, ctime, mtime, atime, owner and group.
</para>

<example>
<get-file-properties <__file__/> />
</example>

<tag:description include>
file=<var filename /> | command=<var command-line />
[alt=<var action />]
[verbatim=true]
</tag:description>

<para>
Insert the contents of a file in the file system - if the <attribute file />
attribute is given -, or the output from executing a system command - if the
<attribute command /> attribute is given - into the input stream. 
For backwards compatibility, if neither the <attribute file /> nor the
<attribute command /> attributes are given, the first argument is taken
as a file to include.
</para>

<attributes:description>
<attributes:item "file=<var filename />">
<para>
The given file is read and inserted into the input stream.
This attribute cannot be combined with the command attribute.
</para>
<para>
Files are first searched in current directory, then in
directories specified on command-line with the <optflag I /> option, next
in directories listed in the <texttt>MP4HLIB</texttt> environment
variable (it used to be <texttt>MP4HPATH</texttt> for versions prior to
1.3), and last under the compile-time location
(<texttt>/usr/local/lib/mp4h/<__version__ />:/usr/local/share/mp4h</texttt>
by default).
</para>
</attributes:item>

<attributes:item "command=<var command-line />">
<para>
The given command line is executed on the operating system, and the output
of it is inserted in the input stream. This attribute cannot be combined
with the file attribute.
</para>
<para>
The given command line is executed using the
popen(3) standard C library routine. The command is executed using the standard
system shell, which on Posix compliant systems is sh(1).
</para>
</attributes:item>

<attributes:item "alt=<var action />">
If file is not found, this alternate action is handled. If this atribute
is not set and file is not found, then an error is raised. This attribute has no effect
when the command attribute is specified.
</attributes:item>

<attributes:item "verbatim=true">
File content is included without expansion.
This is similar to using the &m4; undivert macro with a filename as argument.
</attributes:item>
</attributes:description>

<example>
<include command="uname -a" />
</example>


<tag:description use>
name=<var package />
</tag:description>

<para>
Load definitions from a package file.
</para>


<tag:description comment complex />

<para>
This tag does nothing, its body is simply discarded.
</para>

<tag:description set-eol-comment>
[<var string />]
</tag:description>

<para>
Change comment characters.
</para>

<tag:description set-quotes>
[<var string /> <var string />]
[display=visible]
</tag:description>

<para>
By default, all characters between <texttt><begin-quote/></texttt> and
<texttt><end-quote/></texttt> pairs are read without 
parsing.  When called without argument, this macro inhibates this
feature.  When called with two arguments, it redefines begin and end
delimiters.  Begin delimiter must begin with a left-angle bracket, and
end delimiter must end with a right-angle bracket.
</para>

<attributes:description>
<attributes:item "display=visible">
Delimiters are also written into output.
</attributes:item>
</attributes:description>

###
###    Diversion functions
###

<subsection>Diversion functions</subsection>

<para>
Diversions are a way of temporarily saving output.
The output of &mp4h; can at any time be diverted to a temporary file,
and be reinserted into the output stream, undiverted, again at a later time. 
</para>

<para>
Numbered diversions are counted from 0 upwards,
diversion number 0 being the normal output stream.
The number of simultaneous diversions is limited mainly by the memory used
to describe them, because &mp4h; tries to keep diversions in memory.
However, there is a limit to the overall memory usable by all diversions
taken altogether. When this maximum is about to be exceeded, a
temporary file is opened to receive the contents of the biggest
diversion still in memory, freeing this memory for other diversions.
So, it is theoretically possible that the number of diversions be
limited by the number of available file descriptors.
</para>

<tag:description divert>
[ divnum=<var diversion-number /> ]
</tag:description>

<para>
Output is diverted using this tag, where diversion-number is the diversion
to be used. If the divnum attribute is left out, diversion-number is assumed
to be zero.
If output is diverted to a non-existent diversion, it is simply discarded.
This can be used to suppress unwanted output. See the example below.
</para>

<para>
When all &mp4h; input will have been processed, all existing diversions
are automatically undiverted, in numerical order. 
</para>

<para>
Several calls of divert with the same argument do not overwrite the previous diverted text, but append to it.
</para>

<example>
<divert divnum="-1"/>
This is sent nowhere...
<divert/>
This is output.
</example>

<tag:description undivert>
[ divnum=<var diversion-number /> ]
</tag:description>

<para>
This tag explicitly undiverts diverted text saved in the diversion with the
specified number. If the divnum attribute is not given, all diversions
are undiverted, in numerical order.
</para>

<para>
When diverted text is undiverted, it is not reread by &mp4h;,
but rather copied directly to the current output. It is therefore
not an error to undivert into a diversion.
</para>

<para>
Unlike &m4;, the &mp4h; undivert tag does not allow a file name
as argument. The same can be accomplished with the include tag
with the verbatim="true" attribute.
</para>

<example>
<divert divnum="1"/>
This text is diverted.
<divert/>
This text is not diverted.
<undivert divnum="1"/>
</example>

<tag:description divnum>
</tag:description>

<para>
This tag expands to the number of the current diversion.
</para>

<example>
Initial <divnum/>
<divert divnum="1"/>
Diversion one: <divnum/>
<divert divnum="2"/>
Diversion two: <divnum/>
<divert/>
</example>
###
###    Debugging
###

<subsection>Debugging functions</subsection>

<para>
When constructs become complex it could be hard to debug them.
Functions listed below are very useful when you could not figure what is
wrong.  These functions are not perfect yet and must be improved in
future releases.
</para>

<tag:description function-def>
<var name />
</tag:description>

<para>
Prints the replacement text of a user defined macro. For instance, the
macro used to generate all examples of this documentation is
</para>
<example>
<function-def example />
</example>

<tag:description debugmode>
<var string />
</tag:description>

<para>
2136
This comand acts like the <optflag d /> flag but can be ynamically
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
changed.
</para>

<tag:description debugfile>
<var filename />
</tag:description>

<para>
Selects a file where debugging messages are diverted.
If this filename is empty, debugging messages are sent back to standard
error, and if it is set to <file - /> these messages are discarded.
</para>

<note>
There is no way to print these debugging messages into the document
being processed.
</note>

<tag:description debugging-on>
<var name />
[<var name />] ...
</tag:description>

<para>
2161
Declare these macros traced, i.e. informations about these macros will
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
be printed if <optflag d /> flag or <command debugmode /> macro are used.
</para>

<tag:description debugging-off>
<var name />
[<var name />] ...
</tag:description>

<para>
These macros are no more traced.
</para>

###
###    Miscellaneous
###

<subsection>Miscellaneous</subsection>

<tag:description __file__>
[<var name />]
</tag:description>

<para>
Without argument this macro prints current input filename.
With an argument, this macro sets the string returned by future
invocation of this macro.
</para>

<tag:description __line__>
[<var number />]
</tag:description>
<para>
Without argument this macro prints current number line in input file.
With an argument, this macro sets the number returned by future
invocation of this macro.
</para>

<example>
This is <__file__/>, line <__line__/>.
</example>

<para>
If you closely look at source code you will see that this number is
wrong. Indeed the number line is the end of the entire block containing
this instruction.
</para>

<tag:description __version__ />

<para>
Prints the version of &mp4h;.
</para>

<tag:description dnl />

<para>
Discard all characters until newline is reached. This macro ensures that
following string is a comment and does not depend of the value of
comment characters.
</para>

<example>
<dnl/>This is a comment
foo
<dnl/>This is a comment
bar
</example>

<tag:description date>
[epoch]
</tag:description>

<para>
Prints local time according to the epoch passed on argument.  If there
is no argument, current local time is printed.
</para>

<attributes:description>
<attributes:item "time">
An epoch time specification.
</attributes:item>
<attributes:item "format">
A format specification as used with the strftime(3) C library routine.
</attributes:item>
</attributes:description>


<example>
<date/>
<set-var info=<get-file-properties <__file__/> /> />
<date <get-var info[2] /> />
<date time="<get-var info[2] />" format="%Y-%m-%d %H:%M:%S" />
</example>

<tag:description timer />

<para>
Prints the time spent since last call to this macro.  The printed value
2260
is the number of clock ticks, and so is dependant of your CPU.
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
</para>

<example>
The number of clock ticks since the beginning of generation of
this documentation by &mp4h; is:
<timer/>
</example>

<tag:description mp4h-l10n>
<var name />=<var value />
</tag:description>

<para>
Set locale-specific variables.  By default, the portable "C" locale is
selected. As locales have different names on different platforms, you
must refer to your system documentation to find which values are adapted
to your system.
</para>

<tag:description mp4h-output-radix>
<var number />
</tag:description>

<para>
Change the output format of floats by setting the number of digits after
the decimal point. Default is to print numbers in the "%6.f" format.
</para>

<example>
<add 1.2 3.4 />
<mp4h-output-radix 2 />
<add 1.2 3.4 />
</example>
<mp4h-output-radix 6 />

<section alt="Extending mp4h with external packages">External packages</section>

<para>
It is possible to include external files with the <command include />
command.  Files are first searched in current directory, then in
directories specified on command-line with the <optflag I /> option, next
in directories listed in the <texttt>MP4HLIB</texttt> environment
variable (it used to be <texttt>MP4HPATH</texttt> for versions prior to
1.3), and last under the compile-time location
(<texttt>/usr/local/lib/mp4h/<__version__ />:/usr/local/share/mp4h</texttt>
by default).
</para>

<para>
Another way to include packages is with the <command use /> command.
There are two differences between <command use /> and <command include />:
first, package name has no suffix; and more important, a package cannot
be loaded more than once.
</para>

<section alt="Details about macro expansion">Macro expansion</section>

<para>
This part describes internal mechanism of macro expansion.
It must be as precise and exhaustive as possible so
<URL src="mailto:barbier@linuxfr.org" name="contact me" />
if you have any suggestion.
</para>

<subsection>Basics</subsection>

<para>
Let us begin with some examples:
</para>

<pseudo-func foo />
<undef bar />
<example>
<define-tag foo>
This is a simple tag
</define-tag>
<define-tag bar endtag=required>
This is a complex tag
</define-tag>
<foo/>
<bar>Body function</bar>
</example>
<undef foo bar />

<pseudo-func href mail1 />
<pseudo-func outer inner1 inner2 />
<pseudo-func show-attributes />
<pseudo-func show1 show2 bar />
<undef mail2 />
<para>
User defined macros may have attributes like HTML tags. To handle these
attributes in replacement text, following conventions have been adopted
(mostly derived from &Meta-HTML;):
</para>

<list>
<item>Sequence <value %name /> is replaced by the command name.</item>

<item>Attributes are numbered from 0. In replacement text,
<value %0 /> is replaced by first argument, <value %1 />
by the 2nd, etc.
As there is no limitation on the number of arguments,
<value %20 /> is the 21st argument and not the third followed by the 0
letter.
<example>
<define-tag href>
<a href="%0">%1</a>
</define-tag>
<href http://www.gimp.org "The Gimp" />
</example>
</item>

<item>Sequence <value %# /> prints number of attributes.</item>

<item>Sequence <value %% /> is replaced by <value % />, which is useful in
nested definitions.
<example>
<define-tag outer>;;;
outer, # attributes: %#
<define-tag inner1>;;;
inner1, # attributes: %#;;;
</define-tag>;;;
<define-tag inner2>;;;
inner2, # attributes: %%#;;;
</define-tag>;;;
<inner1 %attributes and some others />
<inner2 %attributes and some others />
</define-tag>
<outer list attributes />
</example>
</item>

<item>Sequence <value %attributes /> is replaced by the space separated
list of attributes.
<example>
<define-tag mail1>
<set-var %attributes />
<get-var name />
<get-var mail />
</define-tag>
<set-var name="" mail="" />
<mail1 name="Dr. Foo" mail="hello@foo.com" />
</example>
</item>

<item>Sequence <value %body /> is replaced by the body of a complex macro.
<example>
<define-tag mail2 endtag=required whitespace=delete>
<set-var %attributes />
<a href="mailto:<get-var mail />">%body</a>
</define-tag>
<mail2 mail="hello@foo.com">
<img src="photo.png" alt="Dr. Foo" border=0 />
</mail2>
</example>
</item>

<item>The two forms above accept modifiers. When <value %Aattributes />
or <value %Abody /> is used, a newline separated list of attributes is
printed.
<example>
<define-tag show-attributes whitespace=delete>
<set-var list="%Aattributes" i=0 />
<foreach attr list>
<group "%<get-var i />: <get-var attr />" />
<increment i />
</foreach>
</define-tag>
<show-attributes name="Dr. Foo" mail="hello@foo.com" />
</example>
</item>

<item>Another alternate form is obtained by replacing <value A />
by <value U />, in which case text is replaced but will not be
expanded.
This does make sense only when macro has been defined with
<attribute attributes=verbatim />, otherwise attributes are expanded
before replacement.
<example>
<define-tag show1>
Before expansion: %Uattributes
After expansion: %attributes
</define-tag>
<define-tag show2 attributes=verbatim>
Before expansion: %Uattributes
After expansion: %attributes
</define-tag>
<define-tag bar>and here %attributes</define-tag>
<show1 <bar we go /> />
<show2 <bar we go /> />
</example>
</item>

<item>Modifiers <value A /> and <value U /> can be combined.</item>
</list>

<note>
Input expansion is completely different in &Meta-HTML; and in &mp4h;.
With &Meta-HTML; it is sometimes necessary to use other constructs like
<value %xbody /> and <value %qbody />.
In order to improve compatibity with &Meta-HTML;, these constructs are
recognized and are interpreted like <value %body />.
Another feature provided for compatibility reason is the fact that for
simple tags <value %body /> and <value %attributes /> are equivalent.
These features are in the current &mp4h; version but may disappear in
future releases.
</note>

<subsection>Attributes</subsection>

<pseudo-func foo />
<para>
Attributes are separated by spaces, tabulations or newlines, and each
attribute must be a valid &mp4h; entity. For instance with the
definitions above, <full-command bar /> can not be an attribute since it
must be finished by <full-command /bar />.
But this is valid:
</para>

<code><foo <foo/> /></code>

or even

<code><foo <foo name=src url=ici /> /></code>

<para>
In these examples, the <command foo /> tag has only one argument.
</para>

<para>
Under certain circumstances it is necessary to group multiple statements
into a single one. This can be done with double quotes or with the
<command group /> primitive, e.g.
</para>

<code><foo "This is the 1st attribute"
           <group and the second /> /></code>

<note>
Unlike HTML single quotes can not replace doube quotes for this purpose.
</note>

<para>
If double quotes appear in an argument, they must be escaped by a
backslash <value "<bs/>" />.
</para>
<example>
  <set-var text="Text with double quotes \" inside" />
  <get-var text />
</example>

<subsection>Macro evaluation</subsection>

<para>
Macros are characterized by
</para>
<list>
<item>name</item>
<item>container status (simple or complex)</item>
<item>if attributes are expanded or not</item>
<item>function type (primitive or user defined macro)</item>
2522
<item>for primitives, adress of corresponding code in memory and for
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654
user defined macros the replacement text</item>
</list>

<para>
Characters are read on input until a left angle bracket is found.
Then macro name is read. After that attributes are read, verbatim or not
depending on how this macro as been defined. And if this macro is
complex, its body is read verbatim. When this is finished, some special
sequences in replacement text are replaced (like <value %body />, <value
%attributes />, <value %0 />, <value %1 />, etc.) and resulting text is put on
input stack in order to be rescanned.
</para>

<note>
By default attributes are evaluated before any replacement.
</note>

<define-tag text-tt endtag=required whitespace=delete>
<tt>%body</tt>
</define-tag>
<para>
Consider the following example, to change text in typewriter font:
</para>

<code>
<define-tag text-tt endtag=required whitespace=delete>
<tt>%body</tt>
</define-tag>
</code>

<para>
This definition has a major drawback:
</para>
<example>
<text-tt>This is an <text-tt>example</text-tt></text-tt>
</example>
We would like the inner tags be removed.

<set-var _text:tt=0 />
<define-tag text-tt endtag=required whitespace=delete>
<increment _text:tt />
<ifeq <get-var _text:tt /> 1 "<tt*>" />
%body
<ifeq <get-var _text:tt /> 1 "</tt*>" />
<decrement _text:tt />
</define-tag>
<para>
First idea is to use an auxiliary variable to know whether we still
are inside such an environment:
</para>

<code>
<set-var _text:tt=0 />
<define-tag text-tt endtag=required whitespace=delete>
<increment _text:tt />
<ifeq <get-var _text:tt /> 1 "<tt*>" />
%body
<ifeq <get-var _text:tt /> 1 "</tt*>" />
<decrement _text:tt />
</define-tag>
</code>
<para>
(the presence of asterisks in HTML tags is explained in next section).
</para>

<example>
<text-tt>This is an <text-tt>example</text-tt></text-tt>
</example>

<pseudo-func opt />
<para>
But if we use simple tags, as in the example below, our definition does
not seem to work. It is because attributes are expanded before they are
put into replacement text.
</para>
<example>
<define-tag opt><text-tt>%attributes</text-tt></define-tag>
<opt "This is an <opt example />" />
</example>

<pseudo-func opt />
<para>
If we want to prevent this problem we have to forbid attributes
expansion with
</para>
<example>
<define-tag opt attributes=verbatim>;;;
<text-tt>%attributes</text-tt>;;;
</define-tag>
<opt "This is an <opt example />" />
</example>

<subsection>Expansion flags</subsection>

<para>
When you want to embed some server-side scripting language in your
pages, you face up some weird problems, like in
</para>

<code>
<a href=<%= $url %>>Hello</a>
</code>

<para>
The question is how do &mp4h; know that this input has some extra
delimiters? The answer is that &mp4h; should not try to handle some
special delimiters, because it cannot handle all of them (there are ASP,
ePerl, PHP,... and some of them are customizable). Now, remember that &mp4h;
is a macro-processor, not an XML parser. So we must focus on macros,and
format our input file so that it can be parsed without any problem.
Previous example may be written
</para>

<code>
<a href="<%= $url %>">Hello</a>
</code>

<para>
because quotes prevent inner right-angle bracket from closing the
<command a /> tag. 
</para>

<para>
Another common problem is when we need to print only a begin or an end
tag alone.  For instance it is very desirable to define its own headers
and footers with
</para>

<code>
<define-tag header>
<html*>
<head>
2655
... put here some informations ....
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
</head>
<body* bgcolor="#ffffff" text="#000000">
</define-tag>
<define-tag footer>
</body*>
</html*>
</define-tag>
</code>

<para>
Asterisks mark these tags as pseudo-simple tags, which means that they
are complex HTML tags, but used as simple tags within &mp4h; because
tags would not be well nested otherwise.
</para>

<para>
This asterisk is called ``trailing star'', it appears at the end of the
tag name.
</para>

<para>
Sometimes HTML tags are not parsable, as in this javascript code:
</para>

<code>
  ...
    document.write('<*img src="foo.gif"');
    if (text) document.write(' alt="'+text+'"');
    document.write('>');
  ...
</code>
<para>
The ``leading star'' is an asterisk between left-angle bracket and
tag name, which prevents this tag from being parsed.
</para>

<para>
That said we can now understand what the <optflag -expansion /> flag is
for.  It controls how expansion is performed by mp4h.  It is followed by
an integer, which is a bit sum of the following values
</para>

<option-list>
<option 1>do not parse unknown tags.
<option-desc>
When set, HTML tags are not parsed.
When unset, HTML tags are parsed, i.e. that attributes and/or body is
collected.
</option-desc>
</option>
<option 2>unknown tags are assumed being simple.
<option-desc>
When set, HTML tags are simple by default.
When unset, HTML tags are complex by default, unless their attribute
contain a trailing slash or a trailing star appear just after tag name
(see below).
</option-desc>
</option>
<option 4>trailing star in tag name do not make this tag simple.
<option-desc>
When set, trailing star in tag name has no special effect.
When unset, it causes an HTML tag to be simple.
</option-desc>
</option>
<option 8>an unmatched end tag closes all previous unmatched begin tags.
<option-desc>
When set, all missing end closing tags are automatically inserted.
When unset, an unmatched end tag is discarded and interpreted as normal
text, so processing goes on until matching and tag is found.
</option-desc>
</option>
<option 16>interpret backslashes as printf.
<option-desc>
When set, backslashes before non special characters are removed.
When unset, they are preserved.
</option-desc>
</option>
<option 32>remove trailing slash in tag attributes.
<option-desc>
When set, remove trailing slash in tag attributes on output.
When unset, they are preserved.
</option-desc>
</option>
<option 64>do not remove trailing star in tag name.
<option-desc>
When set, trailing star after tag name are preserved on output.
When unset, they are removed.
</option-desc>
</option>
<option 128>do not remove leading star in tag name.
<option-desc>
When set, leading star before tag name are preserved on output.
When unset, they are removed.
</option-desc>
</option>
<option 256>do not add a space before trailing slash in tag attributes
<option-desc>
By default, a space is inserted before trailing slash in tag attributes.
When set, this space is not prepended.
</option-desc>
</option>
<option 1024>suppress warnings about bad nested tags.
<option-desc>
When set, warnings about bad nested tags are not displayed.
When unset, they are printed on standard error.
</option-desc>
</option>
<option 2048>suppress warnings about missing trailing slash.
<option-desc>
When set, warnings about missing trailing slash are not displayed.
When unset, they are printed on standard error.
</option-desc>
</option>
</option-list>

<para>
Run
</para>
<code>
  mp4h -h
</code>
to find default value. Current value matches HTML syntax, and it will
tend to zero when XHTML syntax becomes more familiar.

<section>Author</section>

<para>
<URL src="mailto:barbier@linuxfr.org"
     name="Denis Barbier" />
</para>

<para>
Mp4h has its own
2789
<URL src="http://www.engelschall.com/sw/mp4h/" name="homepage" />.
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799
</para>

<section>Thanks</section>

<para>
Sincere thanks to Brian J. Fox for writing &Meta-HTML; and Rene
Seindal for maintaining this wonderful macro parser called GNU &m4;.
</para>

<footer/>