Date:2011-05-27 11:29:42 (8 years 29 days ago)
Author:Werner Almesberger
Commit:a99fbbfb27d99f6ce609ff49317ea61aa78db7ba
Message:prod/doc/: added HTML macro processor (hmac) and page-specific style

Files: prod/doc/hmac.pl (1 diff)
prod/doc/style.inc (1 diff)

Change Details

prod/doc/hmac.pl
1#!/usr/bin/perl
2#
3# hmac.pl - Simple macro pre-processor for HTML
4#
5# Written 2001 by Werner Almesberger
6# Copyright 2001 EPFL DSC-ICA, Network Robots
7#
8
9#------------------------------------------------------------------------------
10#
11# hmac processes macro definitions of the following type:
12#
13# <MACRO NAME=name param[=default] ...> ... </MACRO>
14#
15# The macro is invoked with <name param=value ...> or with
16# <name param=value ...> ... </name>
17#
18# HTML tags corresponding to the parameters (e.g. <param>) are expanded in
19# the macro body. Parameters for which no default value was given must be
20# specified.
21#
22# In the block-style invocation, the content of the block is assigned to
23# the parameter BODY. Block-style invocations cannot be nested.
24#
25# Macros and macro definitions are processed in the order in which they appear
26# in the input file. Macro definitions inside macros are processed when the
27# outer macro is expanded.
28#
29# Files can be included with the <INCLUDE FILE=name> tag. File inclusions
30# are made as we go.
31#
32# hmac is not case-sensitive.
33#
34#------------------------------------------------------------------------------
35
36#
37# Marker
38#
39$BM = "\001";
40$EM = "\002";
41
42#
43# Collect -Dmacro=value options
44#
45while ($ARGV[0] =~ /^-D([^=]+)=/) {
46    $mac{$1} = $';
47    shift(@ARGV);
48}
49
50#
51# Complain about unrecognized options
52#
53if ($ARGV[0] =~ /^-/) {
54    print STDERR "usage: $0 [-Dmacro=value ...] file ...\n";
55    exit(1);
56}
57
58#
59# Read input and put warning
60#
61$in = join("",<>);
62$in =~ s/\n/\n\n<!-- MACHINE-GENERATED FILE, DO NOT EDIT ! -->\n\n/;
63
64#
65# Scan text for macros or includes
66#
67while (1) {
68    $first_macdef = &find_macdef($in);
69    $first_macro = &find_macro($in);
70    $first_include = &find_include($in);
71    last if $first_macdef == -1 && $first_macro == -1 && $first_include == -1;
72    if ($first_include != -1 &&
73      ($first_include < $first_macro || $first_macro == -1) &&
74      ($first_include < $first_macdef || $first_macdef == -1)) {
75    $in = &include_file($in);
76    }
77    else {
78    if (($first_macdef < $first_macro && $first_macdef != -1) ||
79      $first_macro == -1) {
80        $in = &define_macro($in);
81    }
82    else {
83        $in = &expand_macro($in);
84    }
85    }
86}
87print $in;
88
89
90#
91# Find includes, and macros and their definitions
92#
93
94sub find_include
95{
96    local ($in) = @_;
97    return -1 unless $in =~ /<INCLUDE\s+file="[^"]*"/i;
98    return length($`)+length($&);
99}
100
101
102sub find_macdef
103{
104    local ($in) = @_;
105    return -1 unless $in =~ /<MACRO\b/i;
106    return length $`;
107}
108
109
110sub find_macro
111{
112    local ($in) = @_;
113    local ($first) = -1;
114
115    for $mac (keys %mac) {
116    if ($in =~ /<$mac\b/i) {
117        $first = length $` if $first == -1 || length $` < $first;
118    }
119    }
120    return $first;
121}
122
123
124#
125# Include a file
126#
127
128sub include_file
129{
130    local ($in) = @_;
131    local ($name,$f);
132
133    $in =~ /<INCLUDE\s+FILE=("([^"]*)"|\S+)\s*>\s*/i;
134    $name = defined $2 ? $2 : $1;
135    undef $f;
136    open(FILE,$name) || die "open $name: $!";
137    $f = $`.join("",<FILE>).$';
138    close FILE;
139    return $f;
140}
141
142
143#
144# Extract first macro definition
145#
146
147sub define_macro
148{
149    local ($in) = @_;
150    local ($a,$b,$c,$d);
151
152    $in =~ s/<MACRO\b/$BM/gi;
153    $in =~ s|</MACRO>|$EM|gi;
154    if ($in =~ /$BM(("[^"]*"|[^>])*)>/is) {
155    ($a,$b,$c) = ($`,$1,$');
156    $d = "";
157    $need = 1;
158        while ($need) {
159        $bm = index($c,$BM);
160        $em = index($c,$EM);
161        die "<MACRO> without </MACRO>" if $em == -1;
162        if ($bm < $em && $bm != -1) {
163        $d .= substr($c,0,$bm+1,"");
164        $need++;
165        }
166        else {
167        $d .= substr($c,0,$em+1,"");
168        $need--;
169        }
170    }
171    $c =~ s/^\s*//s;
172    $in = $a.$c;
173    chop($d); # remove last $EM
174    undef $name;
175    undef %arg;
176    $b =~ s/^\s*//;
177    while ($b =~ /^([a-z_][a-z0-9_]*)(=("([^"]*)"|\S+))?\s*/is) {
178        $b = $';
179        ($prm = $1) =~ tr/a-z/A-Z/;
180        if ($prm eq "NAME") {
181        die "duplicate NAME" if defined $name;
182        die "NAME without value" unless defined $2;
183        ($name = defined $4 ? $4 : $3) =~ tr/a-z/A-Z/;
184        next;
185        }
186        die "reserved parameter name BODY" if $prm eq "BODY";
187        die "duplicate parameter \"$prm\"" if exists $arg{$prm};
188        $arg{$prm} = defined $2 ? defined $4 ? $4 : $3 : undef;
189    }
190    die "syntax error" unless $b eq "";
191    die "NAME parameter is missing" unless defined $name;
192    $d =~ s/$BM/<MACRO/gi;
193    $d =~ s|$EM|</MACRO>|gi;
194    $mac{$name} = $d;
195    $args{$name} = { %arg };
196    }
197    else {
198    die "</MACRO> without <MACRO>" if $in =~ m|$EM|;
199    }
200    $in =~ s/$BM/<MACRO/gi;
201    $in =~ s|$EM|</MACRO>|gi;
202    return $in;
203}
204
205
206#
207# Expand first macro
208#
209
210sub expand_macro
211{
212    local ($in) = @_;
213    local ($found,$a,$b,$c);
214
215    undef $a;
216    for $mac (keys %mac) {
217    if ($in =~ /<$mac\b(("[^"]*"|[^>])*)>/is) {
218        ($a,$b,$c) = ($`,$1,$') if length $` < length $a || !defined $a;
219    }
220    next unless defined $a;
221    undef %arg;
222    %arg = %{ $args{$mac} };
223    if ($c =~ m|</$mac>|i) {
224        $arg{"BODY"} = $`;
225        $c = $';
226    }
227    else {
228        $arg{"BODY"} = undef;
229    }
230    $b =~ s/^\s*//;
231    while ($b =~ /^([a-z_][a-z0-9_]*)(=("([^"]*)"|\S+))\s*/is) {
232        $b = $';
233        ($prm = $1) =~ tr/a-z/A-Z/;
234        die "unrecognized parameter \"$prm\"" unless exists $arg{$prm};
235        $arg{$prm} = defined $2 ? defined $4 ? $4 : $3 : undef;
236    }
237    $b = $mac{$mac};
238    while (1) {
239        $done = 1;
240        for (keys %arg) {
241        while ($b =~ /<$_>/i) {
242            $done = 0;
243            die "required parameter $_ is missing in macro $mac"
244              unless defined $arg{$_};
245            $b = $`.$arg{$_}.$';
246        }
247        }
248        last if $done;
249    }
250    return $a.$b.$c;
251    }
252    return $in;
253}
prod/doc/style.inc
1<!-- Linux ben-wpan production testing Web page style -->
2
3<!-- Written 2011 by Werner Almesberger -->
4
5
6<MACRO name="__PI" bgcolor>
7  <TD align="center">
8  <TABLE border=0 cellspacing=3 cellpadding=2 bgcolor="<BGCOLOR>">
9    <TR><TD nowrap="nowrap"><FONT size="+3"><B><BODY></B>
10  </TABLE>
11</MACRO>
12
13<MACRO name="__PI_SEP">
14  <TD align="center" bgcolor="#000000">
15  <TABLE border=0 cellspacing=0 cellpadding=1>
16    <TR><TD>
17  </TABLE>
18</MACRO>
19
20<MACRO name="PAGE_BAR" title>
21  <TABLE width="99%" border=0 cellspacing=0 cellpadding=0>
22    <TR>
23    <__PI bgcolor="#d080ff"><A href="index.html"><B><TITLE></B></A></__PI>
24    <TD align="left">
25    <TABLE border=0 cellspacing=0 cellpadding=0>
26      <TR>
27      <BODY>
28      <__PI_SEP>
29    <TD align="left" width="99%" bgcolor="#d080ff">
30    </TABLE>
31</MACRO>
32
33<MACRO name="PAGE_ITEM" href>
34  <__PI_SEP>
35  <__PI bgcolor="#c0d0ff"><A href="<HREF>"><BODY></A></__PI>
36</MACRO>
37
38<MACRO name="PAGE_CURR" href>
39  <__PI_SEP>
40  <__PI bgcolor="#ffff00"><BODY></__PI>
41</MACRO>
42
43<MACRO name="SECTION_BAR" title>
44    <TR><TD align="left" bgcolor="#ffffff">
45    <TD align="left">
46    <TABLE border=0 cellspacing=0 cellpadding=0>
47      <TR>
48      <BODY>
49      <__PI_SEP>
50    <TD align="left" width="99%" bgcolor="#d080ff">
51    </TABLE>
52  </TABLE>
53</MACRO>
54
55<MACRO name="SECTION_ITEM" href>
56  <__PI_SEP>
57  <TD align="center">
58  <TABLE border=0 cellspacing=3 cellpadding=2 bgcolor="#ffff00">
59    <TR><TD nowrap="nowrap"><A href="<HREF>"<FONT size="+2"><B><BODY></B></A>
60  </TABLE>
61</MACRO>
62
63<MACRO name="SECTION" ref title>
64  <P>
65  <TABLE width="99%" border=0 cellspacing=0 cellpadding=2 bgcolor="#f0f0a0">
66    <TR><TD>&nbsp;<A name="<REF>"><FONT
67    size="+2"><B><TITLE></B></FONT></A></TD>
68  </TABLE></TD>
69  <P>
70</MACRO>
71
72<MACRO name="SUBSECTION" title>
73<P>
74<B><FONT size="+1"><TITLE></B></FONT>
75<P>
76</MACRO>

Archive Download the corresponding diff file



interactive