update translit (to handle c++ drafts as well)
[c-standard] / annot.sh
1 #!/bin/sh
2
3 export LC_ALL=C
4 awk '
5 function initpage() {
6         if (innote)
7                 endpre()
8
9         indent = 10
10         para = 0
11         innote = 0
12         ok = 0
13         empty = ""
14         page = ""
15 }
16
17 function endlist() {
18         if (listindex) {
19                 listindent = 0
20                 listindex = 0
21                 print "@end ol"
22         } else if (listlevel) {
23                 listlevel--
24                 print "@end ul"
25         }
26 }
27
28 function donote(s) {
29         if (match(s, /^[1-9][0-9]*\) +/)) {
30                 # todo..
31                 endpre()
32                 print "@note " substr(s, 1, RLENGTH-2)
33                 s = substr(s, RLENGTH+1)
34                 innote = 1
35                 noteindent = RLENGTH
36         } else if (innote) {
37                 if (match(s, /^ +/) && RLENGTH >= noteindent)
38                         s = substr(s, noteindent+1)
39                 else
40                         innote = 0
41         }
42         return s
43 }
44
45 function doul(s) {
46         if (!listlevel) {
47                 if (s ~ /^--/) {
48                         s = dopre(s)
49                         listlevel++
50                         print "@ul"
51                 } else
52                         return s
53         }
54         if (listlevel == 1) {
55                 if (s ~ /^   *o /) {
56                         listlevel++
57                         print "@ul"
58                 } else if (s ~ /^--/) {
59                         s = dopre(s)
60                         s = substr(s , 3)
61                         print "@li"
62                 } else if (match(s, /^ +/) && RLENGTH > 1) {
63                         sub(/^  /, "", s)
64                 } else if (s !~ /^$/) {
65                         endpre()
66                         endlist()
67                 }
68         }
69         if (listlevel == 2) {
70                 if (s ~ /^   *o /) {
71                         sub(/ *o /, "", s)
72                         s = dopre(s)
73                         print "@li"
74                 } else if (match(s, /^ +/) && RLENGTH > 5) {
75                         sub(/^ +/, "", s)
76                 } else if (s !~ /^$/) {
77                         sub(/^  /, "", s)
78                         endlist()
79                 }
80         }
81         return s
82 }
83
84 function dool(s) {
85         if (listindex == 0) {
86                 if (s ~ /^   *1\. /)
87                         print "@ol"
88                 else
89                         return s
90         }
91
92         if (match(s, "^ +" (listindex+1) "\\. +")) {
93                 listindex++
94                 listindent = RLENGTH
95                 s = substr(s, RLENGTH)
96                 s = dopre(s)
97                 print "@li " listindex
98         } else if (match(s, /^ +/) && RLENGTH >= listindent) {
99                 s = substr(s, listindent+1)
100         } else if (s !~ /^$/) {
101                 endpre()
102                 endlist()
103         }
104         return s
105 }
106
107 function endpre() {
108         if (inpre) {
109                 print "@end pre"
110                 inpre = 0
111         }
112 }
113
114 function dopre(s) {
115         if (seenindex != 1)
116                 return s
117         if (!inpre) {
118                 if (s ~ /^    */) {
119                         print "@pre"
120                         inpre = 1
121                 } else
122                         return s
123         }
124         if (s !~ /^    */)
125                 endpre()
126         return s
127 }
128
129 function dosect(s,   n,a) {
130         if (seenindex > 1)
131                 return s
132         if (s ~ /^Programming languages/) {
133                 print "@title " s
134                 return s
135         }
136         if (s !~ /^([1-9]\.|[A-Z]\.[1-9]| *Annex |Contents|Index|Foreword|Introduction| *Bibliography)/)
137                 return s
138         if (s ~ /^[0-9.]+[^0-9. ]/)
139                 return s
140         n = split(s, a)
141         id = a[1]
142         if (id ~ /Annex/)
143                 id = a[2]
144         if (id ~ /^([A-Z]|[1-9]\.|[1-9A-Z]\.[0-9.]*[0-9]|Contents|Index|Foreword|Introduction|Bibliography)$/ &&
145             (n==1 || a[2] ~ /^[A-Z.v_]/)) {
146                 sub(/^ +/,"",s)
147                 if (id ~ /\.$/)
148                         id = substr(id,1,length(id)-1)
149                 if (seenindex || id == "Contents") {
150                         endpre()
151                         endlist()
152                         print "@sect " id
153                 }
154                 if (id == "Index")
155                         seenindex++
156         }
157         return s
158 }
159
160 BEGIN {
161         listlevel = 0
162         listindex = 0
163         listindent = 0
164         noteindent = 0
165         inpre = 0
166         seenindex = 0
167         pn = 1
168         initpage()
169 }
170
171 /^\[page/ {
172         if(!para && indent && ok)
173                 indent = -1
174
175         if (!ok)
176                 indent = 0
177
178         n = split(page, a, /\n/)
179         print "@page " pn
180         print "@indent " indent
181         if (indent < 0)
182                 indent = 0
183         for (i = 1; i < n; i++) {
184                 if (a[i] ~ /^@/) {
185                         if (a[i] ~ /^@para/) {
186                                 endpre()
187                                 endlist()
188                         }
189                         print a[i]
190                 } else {
191                         s = substr(a[i], indent+1)
192                         s = dosect(s)
193                         s = donote(s)
194                         if (!innote) {
195                                 s = doul(s)
196                                 s = dool(s)
197                         }
198                         s = dopre(s)
199                         print s
200                 }
201         }
202
203         pn++
204         initpage()
205         next
206 }
207
208 /^$/ {
209         if (ok)
210                 empty = empty "\n"
211         next
212 }
213
214 length(empty) > 0 {
215         page = page empty
216         empty = ""
217 }
218
219 {
220         ok = 1
221         if (match($0, /^[0-9]+ +/)) {
222                 para = 1
223                 i = RLENGTH
224                 match($0, /^[0-9]+/)
225                 page = page "@para " substr($0,1,RLENGTH) "\n"
226         } else if (match($0, /^ +/)) {
227                 i = RLENGTH
228         } else if ($0 !~ /^[0-9]*$/) {
229                 i = 0
230         } else
231                 i = 10
232         if (i < indent)
233                 indent = i
234         page = page $0 "\n"
235 }'
236