readme fix, +ann2html todo
[c-standard.git] / ann2html.sh
blob509e3a293e8799f42558b85601dd4cac8e523613
1 #!/bin/sh
3 # todo: <sub>opt</sub>
5 export LC_ALL=C
6 awk '
7 BEGIN {
8 noteid = 1
9 sid = 1
10 ss[sid] = "<pre>"
14 gsub(/\&/, "\\&amp;")
15 gsub(/</, "\\&lt;")
16 gsub(/>/, "\\&gt;")
19 !title && /^[^@]/ {
20 title = $0
21 gsub(/ +/, " ", title)
22 gsub(/Committee Draft --/, "", title)
25 /^@sect Contents/ {
26 ss[sid] = ss[sid] "</pre>\n"
27 seencontents = 1
28 level = 0
31 seencontents && !seenfore && /^[^@]/ {
32 id = $1
33 if (id ~ /Annex/)
34 id = $2
35 sub(/\.$/, "", id)
37 s = $0
38 if (!sub(/ +\. .*/, "", s)) {
39 getline
40 sub(/^ */, " ")
41 s = s $0
42 sub(/ +\. .*/, "", s)
45 if (match(s, /&lt;[a-zA-Z0-9_]*\.h&gt;/)) {
46 h = substr($0,RSTART,RLENGTH)
47 if (!(h in header))
48 header[h] = id
51 s = "<a href=\"#" id "\">" s "</a>\n"
53 s = "<li>" s
54 n = split(id, a, /\./)
55 while (n > level) {
56 s = "<ul>\n" s
57 level++
59 while (n < level) {
60 s = "</ul>\n" s
61 level--
63 ss[sid] = ss[sid] s
64 next
67 /^@sect Foreword/ {
68 while (level--)
69 ss[sid] = ss[sid] "</ul>\n"
70 seenfore = 1
73 /^@sect Index/ {
74 seenindex = 1
77 /^@title/ {
78 if (!seencontents) {
79 ss[sid] = ss[sid] "</pre>\n"
81 sid++
82 getline
83 ss[sid] = ss[sid] "<h1>" $0 "</h1>\n"
84 if (!seencontents) {
85 ss[sid] = ss[sid] "<pre>\n"
87 next
90 /^@sect 3\./ {
91 markdef = 1
94 /^@sect/ {
95 sid++
96 slevel = split($2,a,/\./)+1
97 if (slevel > 5)
98 slevel = 5
99 sect = $2
100 getline
101 # todo hX, back to top
102 ss[sid] = sprintf("<h%s><a name=\"%s\" href=\"#%s\">%s</a></h%s>\n", slevel, sect, sect, $0, slevel)
103 if ($0 == "Index")
104 ss[sid] = ss[sid] "<pre>\n"
105 next
108 /^@ul/ {
109 ss[sid] = ss[sid] "<ul>\n"
110 next
112 /^@end ul/ {
113 ss[sid] = ss[sid] "</ul>\n"
114 next
116 /^@ol/ {
117 ss[sid] = ss[sid] "<ol>\n"
118 next
120 /^@end ol/ {
121 ss[sid] = ss[sid] "</ol>\n"
122 next
125 /^@li/ {
126 ss[sid] = ss[sid] "<li>"
127 next
130 /^@pre/ {
131 pre = "<pre>"
132 next
135 /^@end pre/ {
136 if (!pre)
137 next
138 pre = pre "</pre>\n"
139 if (nn)
140 note[nn] = note[nn] "\n" pre
141 else
142 ss[sid] = ss[sid] pre
143 pre = ""
144 next
147 /^@note/ {
148 nn = $2+0
149 note[nn] = ""
150 next
153 /^@page/ {
154 nn = 0
155 p = $2
156 getline
157 ss[sid] = ss[sid] "<!--page " p " -->\n"
158 next
161 /^@para/ {
162 ss[sid] = ss[sid] "<p><!--para " $2 " -->\n"
163 next
166 /^ ?(Syntax|Semantics|Description|Constraints|Synopsis|Returns|Recommended practice|Implementation limits|Environmental limits)$/ {
167 ss[sid] = ss[sid] "<h6>" $0 "</h6>\n"
168 next
171 !seenfore {
172 ss[sid] = ss[sid] $0 "\n"
173 next
177 s = $0
178 p = ""
179 if (seenindex)
180 r = " [A-Z1-9][0-9.]*"
181 else
182 r = "[ ([][A-Z1-9]\\.[0-9.]*[0-9]"
183 # hack
184 s = " " s
185 while (match(s, r)) {
186 p = p substr(s,1,RSTART)
187 m = substr(s,RSTART+1,RLENGTH-1)
188 if (m ~ /\.0$/ || m ~ /[4-9][0-9]/ || m ~ /[0-3][0-9][0-9]/ ||
189 substr(s,RSTART+RLENGTH,1) ~ /[a-zA-Z_\-]/)
190 p = p m
191 else
192 p = p "<a href=\"#" m "\">" m "</a>"
193 s = substr(s,RSTART+RLENGTH)
195 s = p s
196 p = ""
197 while (match(s, /[Aa]nnex [A-Z]/)) {
198 p = p substr(s,1,RSTART-1)
199 m = substr(s,RSTART,RLENGTH)
200 p = p "<a href=\"#" substr(m,RLENGTH,1) "\">" m "</a>"
201 s = substr(s,RSTART+RLENGTH)
203 s = p s
204 p = ""
205 while (match(s, /&lt;[a-zA-Z0-9_]*\.h&gt;/)) {
206 p = p substr(s,1,RSTART-1)
207 m = substr(s,RSTART,RLENGTH)
208 if (m in header)
209 p = p "<a href=\"#" header[m] "\">" m "</a>"
210 else
211 p = p m
212 s = substr(s,RSTART+RLENGTH)
214 s = p s
215 p = ""
216 for (;;) {
217 while (match(s, noteid-1 "\\)")) {
218 p = p substr(s,1,RSTART-1)
219 p = p "<sup><a href=\"#note" noteid-1 "\"><b>" noteid-1 ")</b></a></sup>"
220 s = substr(s,RSTART+RLENGTH)
222 if (!match(s, noteid "\\)"))
223 break
224 if (noteid==1 && s !~ /\.1\)/)
225 break
226 p = p substr(s,1,RSTART-1)
227 p = p "<sup><a href=\"#note" noteid "\"><b>" noteid ")</b></a></sup>"
228 snote[sid] = snote[sid] " " noteid
229 noteid++
230 s = substr(s,RSTART+RLENGTH)
232 s = p s
233 sub(/^ *Forward references/, "<p><b>&</b>", s)
234 if (markdef) {
235 s = "<b>" s "</b><br>"
236 markdef = 0
238 if (pre)
239 pre = pre "\n" s
240 else if (nn)
241 note[nn] = note[nn] s "\n"
242 else
243 ss[sid] = ss[sid] s "\n"
246 END {
247 ss[sid] = ss[sid] "</pre>"
249 print "<html><head><title>" title "</title></head><body>"
251 for (i = 1; i <= sid; i++) {
252 print ss[i]
253 n = split(snote[i],a)
254 if (n > 0) {
255 s = "<h6>footnotes</h6>\n"
256 for (j = 1; j <= n; j++) {
257 s = s "<p><small><a name=\"note" a[j] "\" href=\"#note" a[j] "\">" a[j] ")</a>" note[a[j]+0] "</small>\n"
259 print s
263 print "</body></html>"