fix <a> and <hn> order in section links
[c-standard.git] / ann2html.sh
blobad4413dbca7ac3bb04bf163f311af389b3337b4a
1 #!/bin/sh
3 export LC_ALL=C
4 awk '
5 BEGIN {
6 noteid = 1
7 sid = 1
8 ss[sid] = "<pre>"
12 gsub(/\&/, "\\&amp;")
13 gsub(/</, "\\&lt;")
14 gsub(/>/, "\\&gt;")
17 !title && /^[^@]/ {
18 title = $0
19 gsub(/ +/, " ", title)
20 gsub(/Committee Draft --/, "", title)
23 /^@sect Contents/ {
24 ss[sid] = ss[sid] "</pre>\n"
25 seencontents = 1
26 level = 0
29 seencontents && !seenfore && /^[^@]/ {
30 id = $1
31 if (id ~ /Annex/)
32 id = $2
33 sub(/\.$/, "", id)
35 s = $0
36 if (!sub(/ +\. .*/, "", s)) {
37 getline
38 sub(/^ */, " ")
39 s = s $0
40 sub(/ +\. .*/, "", s)
43 if (match(s, /&lt;[a-zA-Z0-9_]*\.h&gt;/)) {
44 h = substr($0,RSTART,RLENGTH)
45 if (!(h in header))
46 header[h] = id
49 s = "<a href=\"#" id "\">" s "</a>\n"
51 s = "<li>" s
52 n = split(id, a, /\./)
53 while (n > level) {
54 s = "<ul>\n" s
55 level++
57 while (n < level) {
58 s = "</ul>\n" s
59 level--
61 ss[sid] = ss[sid] s
62 next
65 /^@sect Foreword/ {
66 while (level--)
67 ss[sid] = ss[sid] "</ul>\n"
68 seenfore = 1
71 /^@sect Index/ {
72 seenindex = 1
75 /^@title/ {
76 if (!seencontents) {
77 ss[sid] = ss[sid] "</pre>\n"
79 sid++
80 getline
81 ss[sid] = ss[sid] "<h1>" $0 "</h1>\n"
82 if (!seencontents) {
83 ss[sid] = ss[sid] "<pre>\n"
85 next
88 /^@sect/ {
89 sid++
90 slevel = split($2,a,/\./)+1
91 if (slevel > 5)
92 slevel = 5
93 sect = $2
94 getline
95 # todo hX, back to top
96 ss[sid] = sprintf("<h%s><a name=\"%s\" href=\"#%s\">%s</a></h%s>\n", slevel, sect, sect, $0, slevel)
97 if ($0 == "Index")
98 ss[sid] = ss[sid] "<pre>\n"
99 next
102 /^@ul/ {
103 ss[sid] = ss[sid] "<ul>\n"
104 next
106 /^@end ul/ {
107 ss[sid] = ss[sid] "</ul>\n"
108 next
110 /^@ol/ {
111 ss[sid] = ss[sid] "<ol>\n"
112 next
114 /^@end ol/ {
115 ss[sid] = ss[sid] "</ol>\n"
116 next
119 /^@li/ {
120 ss[sid] = ss[sid] "<li>"
121 next
124 /^@pre/ {
125 pre = "<pre>"
126 next
129 /^@end pre/ {
130 if (!pre)
131 next
132 pre = pre "</pre>\n"
133 if (nn)
134 note[nn] = note[nn] "\n" pre
135 else
136 ss[sid] = ss[sid] pre
137 pre = ""
138 next
141 /^@note/ {
142 nn = $2+0
143 note[nn] = ""
144 next
147 /^@page/ {
148 nn = 0
149 p = $2
150 getline
151 ss[sid] = ss[sid] "<!--page " p " -->\n"
152 next
155 /^@para/ {
156 ss[sid] = ss[sid] "<p><!--para " $2 " -->\n"
157 next
160 /^ *(Syntax|Semantics|Description|Constraints|Synopsis|Returns)$/ {
161 ss[sid] = ss[sid] "<h6>" $0 "</h6>\n"
162 next
165 !seenfore {
166 ss[sid] = ss[sid] $0 "\n"
167 next
171 s = $0
172 p = ""
173 if (seenindex)
174 r = " [A-Z1-9][0-9.]*"
175 else
176 r = "[ ([][A-Z1-9]\\.[0-9.]*[0-9]"
177 # hack
178 s = " " s
179 while (match(s, r)) {
180 p = p substr(s,1,RSTART)
181 m = substr(s,RSTART+1,RLENGTH-1)
182 if (m ~ /\.0$/ || m ~ /[4-9][0-9]/ || m ~ /[0-3][0-9][0-9]/ ||
183 substr(s,RSTART+RLENGTH,1) ~ /[a-zA-Z_\-]/)
184 p = p m
185 else
186 p = p "<a href=\"#" m "\">" m "</a>"
187 s = substr(s,RSTART+RLENGTH)
189 s = p s
190 p = ""
191 while (match(s, /[Aa]nnex [A-Z]/)) {
192 p = p substr(s,1,RSTART-1)
193 m = substr(s,RSTART,RLENGTH)
194 p = p "<a href=\"#" substr(m,RLENGTH,1) "\">" m "</a>"
195 s = substr(s,RSTART+RLENGTH)
197 s = p s
198 p = ""
199 while (match(s, /&lt;[a-zA-Z0-9_]*\.h&gt;/)) {
200 p = p substr(s,1,RSTART-1)
201 m = substr(s,RSTART,RLENGTH)
202 if (m in header)
203 p = p "<a href=\"#" header[m] "\">" m "</a>"
204 else
205 p = p m
206 s = substr(s,RSTART+RLENGTH)
208 s = p s
209 p = ""
210 while (match(s, noteid "\\)")) {
211 if (noteid==1 && s !~ /\.1\)/)
212 break
213 p = p substr(s,1,RSTART-1)
214 p = p "<sup><a href=\"#note" noteid "\"><b>" noteid ")</b></a></sup>"
215 snote[sid] = snote[sid] " " noteid
216 noteid++
217 s = substr(s,RSTART+RLENGTH)
219 s = p s
220 sub(/^ *Forward references/, "<p><b>&</b>", s)
221 if (pre)
222 pre = pre "\n" s
223 else if (nn)
224 note[nn] = note[nn] s "\n"
225 else
226 ss[sid] = ss[sid] s "\n"
229 END {
230 ss[sid] = ss[sid] "</pre>"
232 print "<html><head><title>" title "</title></head><body>"
234 for (i = 1; i <= sid; i++) {
235 print ss[i]
236 n = split(snote[i],a)
237 if (n > 0) {
238 s = "<h6>footnotes</h6>\n"
239 for (j = 1; j <= n; j++) {
240 s = s "<p><small><a name=\"note" a[j] "\" href=\"#note" a[j] "\">" a[j] ")</a>" note[a[j]+0] "</small>\n"
242 print s
246 print "</body></html>"