]>
Commit | Line | Data |
---|---|---|
4bbf95fa | 1 | #!/usr/bin/awk |
2 | # | |
3 | # Version history: | |
4 | # v3, I put the program under a proper license | |
5 | # Dan Nelson <dnelson@allantgroup.com> added .An, .Aq and fixed a typo | |
6 | # v2, fixed to work on GNU awk --posix and MacOS X | |
7 | # v1, first attempt, didn't work on MacOS X | |
8 | # | |
9 | # Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org> | |
10 | # | |
11 | # Permission to use, copy, modify, and distribute this software for any | |
12 | # purpose with or without fee is hereby granted, provided that the above | |
13 | # copyright notice and this permission notice appear in all copies. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
16 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
17 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
18 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
19 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
20 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
21 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
22 | ||
23 | ||
24 | BEGIN { | |
25 | optlist=0 | |
26 | oldoptlist=0 | |
27 | nospace=0 | |
28 | synopsis=0 | |
29 | reference=0 | |
30 | block=0 | |
31 | ext=0 | |
32 | extopt=0 | |
33 | literal=0 | |
34 | prenl=0 | |
35 | line="" | |
36 | } | |
37 | ||
38 | function wtail() { | |
39 | retval="" | |
40 | while(w<nwords) { | |
41 | if(length(retval)) | |
42 | retval=retval OFS | |
43 | retval=retval words[++w] | |
44 | } | |
45 | return retval | |
46 | } | |
47 | ||
48 | function add(str) { | |
49 | for(;prenl;prenl--) | |
50 | line=line "\n" | |
51 | line=line str | |
52 | } | |
53 | ||
54 | ! /^\./ { | |
55 | for(;prenl;prenl--) | |
56 | print "" | |
57 | ||
58 | if(literal) | |
59 | print ".br" | |
60 | next | |
61 | } | |
62 | ||
63 | /^\.\\"/ { next } | |
64 | ||
65 | { | |
66 | option=0 | |
67 | parens=0 | |
68 | angles=0 | |
69 | sub("^\\.","") | |
70 | nwords=split($0,words) | |
71 | for(w=1;w<=nwords;w++) { | |
72 | skip=0 | |
73 | if(match(words[w],"^Li|Pf$")) { | |
74 | skip=1 | |
75 | } else if(match(words[w],"^Xo$")) { | |
76 | skip=1 | |
77 | ext=1 | |
78 | if(length(line)&&!(match(line," $")||prenl)) | |
aff51935 | 79 | add(OFS) |
4bbf95fa | 80 | } else if(match(words[w],"^Xc$")) { |
81 | skip=1 | |
82 | ext=0 | |
83 | if(!extopt) | |
aff51935 | 84 | prenl++ |
4bbf95fa | 85 | w=nwords |
86 | } else if(match(words[w],"^Bd$")) { | |
87 | skip=1 | |
88 | if(match(words[w+1],"-literal")) { | |
aff51935 | 89 | literal=1 |
90 | prenl++ | |
91 | w=nwords | |
4bbf95fa | 92 | } |
93 | } else if(match(words[w],"^Ed$")) { | |
94 | skip=1 | |
95 | literal=0 | |
96 | } else if(match(words[w],"^Ns$")) { | |
97 | skip=1 | |
98 | if(!nospace) | |
aff51935 | 99 | nospace=1 |
4bbf95fa | 100 | sub(" $","",line) |
101 | } else if(match(words[w],"^No$")) { | |
102 | skip=1 | |
103 | sub(" $","",line) | |
104 | add(words[++w]) | |
105 | } else if(match(words[w],"^Dq$")) { | |
106 | skip=1 | |
107 | add("``") | |
108 | add(words[++w]) | |
109 | while(w<nwords&&!match(words[w+1],"^[\\.,]")) | |
aff51935 | 110 | add(OFS words[++w]) |
4bbf95fa | 111 | add("''") |
112 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 113 | nospace=1 |
4bbf95fa | 114 | } else if(match(words[w],"^Sq|Ql$")) { |
115 | skip=1 | |
116 | add("`" words[++w] "'") | |
117 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 118 | nospace=1 |
4bbf95fa | 119 | } else if(match(words[w],"^Oo$")) { |
120 | skip=1 | |
121 | extopt=1 | |
122 | if(!nospace) | |
aff51935 | 123 | nospace=1 |
4bbf95fa | 124 | add("[") |
125 | } else if(match(words[w],"^Oc$")) { | |
126 | skip=1 | |
127 | extopt=0 | |
128 | add("]") | |
129 | } | |
130 | if(!skip) { | |
131 | if(!nospace&&length(line)&&!(match(line," $")||prenl)) | |
aff51935 | 132 | add(OFS) |
4bbf95fa | 133 | if(nospace==1) |
aff51935 | 134 | nospace=0 |
4bbf95fa | 135 | } |
136 | if(match(words[w],"^Dd$")) { | |
137 | date=wtail() | |
138 | next | |
139 | } else if(match(words[w],"^Dt$")) { | |
140 | id=wtail() | |
141 | next | |
142 | } else if(match(words[w],"^Os$")) { | |
143 | add(".TH " id " \"" date "\" \"" wtail() "\"") | |
144 | } else if(match(words[w],"^Sh$")) { | |
145 | add(".SH") | |
146 | synopsis=match(words[w+1],"SYNOPSIS") | |
147 | } else if(match(words[w],"^Xr$")) { | |
148 | add("\\fB" words[++w] "\\fP(" words[++w] ")" words[++w]) | |
149 | } else if(match(words[w],"^Rs$")) { | |
150 | split("",refauthors) | |
151 | nrefauthors=0 | |
152 | reftitle="" | |
153 | refissue="" | |
154 | refdate="" | |
155 | refopt="" | |
156 | reference=1 | |
157 | next | |
158 | } else if(match(words[w],"^Re$")) { | |
159 | prenl++ | |
160 | for(i=nrefauthors-1;i>0;i--) { | |
aff51935 | 161 | add(refauthors[i]) |
162 | if(i>1) | |
163 | add(", ") | |
4bbf95fa | 164 | } |
165 | if(nrefauthors>1) | |
aff51935 | 166 | add(" and ") |
4bbf95fa | 167 | add(refauthors[0] ", \\fI" reftitle "\\fP") |
168 | if(length(refissue)) | |
aff51935 | 169 | add(", " refissue) |
4bbf95fa | 170 | if(length(refdate)) |
aff51935 | 171 | add(", " refdate) |
4bbf95fa | 172 | if(length(refopt)) |
aff51935 | 173 | add(", " refopt) |
4bbf95fa | 174 | add(".") |
175 | reference=0 | |
176 | } else if(reference) { | |
177 | if(match(words[w],"^%A$")) { refauthors[nrefauthors++]=wtail() } | |
178 | if(match(words[w],"^%T$")) { | |
aff51935 | 179 | reftitle=wtail() |
180 | sub("^\"","",reftitle) | |
181 | sub("\"$","",reftitle) | |
4bbf95fa | 182 | } |
183 | if(match(words[w],"^%N$")) { refissue=wtail() } | |
184 | if(match(words[w],"^%D$")) { refdate=wtail() } | |
185 | if(match(words[w],"^%O$")) { refopt=wtail() } | |
186 | } else if(match(words[w],"^Nm$")) { | |
187 | if(synopsis) { | |
aff51935 | 188 | add(".br") |
189 | prenl++ | |
4bbf95fa | 190 | } |
191 | n=words[++w] | |
192 | if(!length(name)) | |
aff51935 | 193 | name=n |
4bbf95fa | 194 | if(!length(n)) |
aff51935 | 195 | n=name |
4bbf95fa | 196 | add("\\fB" n "\\fP") |
197 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 198 | nospace=1 |
4bbf95fa | 199 | } else if(match(words[w],"^Nd$")) { |
200 | add("\\- " wtail()) | |
201 | } else if(match(words[w],"^Fl$")) { | |
202 | add("\\fB\\-" words[++w] "\\fP") | |
203 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 204 | nospace=1 |
4bbf95fa | 205 | } else if(match(words[w],"^Ar$")) { |
206 | add("\\fI") | |
207 | if(w==nwords) | |
aff51935 | 208 | add("file ...\\fP") |
4bbf95fa | 209 | else { |
aff51935 | 210 | add(words[++w] "\\fP") |
211 | while(match(words[w+1],"^\\|$")) | |
212 | add(OFS words[++w] " \\fI" words[++w] "\\fP") | |
4bbf95fa | 213 | } |
214 | if(!nospace&&match(words[w+1],"^[\\.,]")) | |
aff51935 | 215 | nospace=1 |
4bbf95fa | 216 | } else if(match(words[w],"^Cm$")) { |
217 | add("\\fB" words[++w] "\\fP") | |
218 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) | |
aff51935 | 219 | add(words[++w]) |
4bbf95fa | 220 | } else if(match(words[w],"^Op$")) { |
221 | option=1 | |
222 | if(!nospace) | |
aff51935 | 223 | nospace=1 |
4bbf95fa | 224 | add("[") |
225 | } else if(match(words[w],"^Pp$")) { | |
226 | prenl++ | |
227 | } else if(match(words[w],"^An$")) { | |
228 | prenl++ | |
229 | } else if(match(words[w],"^Ss$")) { | |
230 | add(".SS") | |
231 | } else if(match(words[w],"^Pa$")&&!option) { | |
232 | add("\\fI") | |
233 | w++ | |
234 | if(match(words[w],"^\\.")) | |
aff51935 | 235 | add("\\&") |
4bbf95fa | 236 | add(words[w] "\\fP") |
237 | while(w<nwords&&match(words[w+1],"^[\\.,:;)]")) | |
aff51935 | 238 | add(words[++w]) |
4bbf95fa | 239 | } else if(match(words[w],"^Dv$")) { |
240 | add(".BR") | |
241 | } else if(match(words[w],"^Em|Ev$")) { | |
242 | add(".IR") | |
243 | } else if(match(words[w],"^Pq$")) { | |
244 | add("(") | |
245 | nospace=1 | |
246 | parens=1 | |
247 | } else if(match(words[w],"^Aq$")) { | |
248 | add("<") | |
249 | nospace=1 | |
250 | angles=1 | |
251 | } else if(match(words[w],"^S[xy]$")) { | |
252 | add(".B " wtail()) | |
253 | } else if(match(words[w],"^Ic$")) { | |
254 | plain=1 | |
255 | add("\\fB") | |
256 | while(w<nwords) { | |
aff51935 | 257 | w++ |
258 | if(match(words[w],"^Op$")) { | |
259 | w++ | |
260 | add("[") | |
261 | words[nwords]=words[nwords] "]" | |
262 | } | |
263 | if(match(words[w],"^Ar$")) { | |
264 | add("\\fI" words[++w] "\\fP") | |
265 | } else if(match(words[w],"^[\\.,]")) { | |
266 | sub(" $","",line) | |
267 | if(plain) { | |
268 | add("\\fP") | |
269 | plain=0 | |
270 | } | |
271 | add(words[w]) | |
272 | } else { | |
273 | if(!plain) { | |
274 | add("\\fB") | |
275 | plain=1 | |
276 | } | |
277 | add(words[w]) | |
278 | } | |
279 | if(!nospace) | |
280 | add(OFS) | |
4bbf95fa | 281 | } |
282 | sub(" $","",line) | |
283 | if(plain) | |
aff51935 | 284 | add("\\fP") |
4bbf95fa | 285 | } else if(match(words[w],"^Bl$")) { |
286 | oldoptlist=optlist | |
287 | if(match(words[w+1],"-bullet")) | |
aff51935 | 288 | optlist=1 |
4bbf95fa | 289 | else if(match(words[w+1],"-enum")) { |
aff51935 | 290 | optlist=2 |
291 | enum=0 | |
4bbf95fa | 292 | } else if(match(words[w+1],"-tag")) |
aff51935 | 293 | optlist=3 |
4bbf95fa | 294 | else if(match(words[w+1],"-item")) |
aff51935 | 295 | optlist=4 |
4bbf95fa | 296 | else if(match(words[w+1],"-bullet")) |
aff51935 | 297 | optlist=1 |
4bbf95fa | 298 | w=nwords |
299 | } else if(match(words[w],"^El$")) { | |
300 | optlist=oldoptlist | |
301 | } else if(match(words[w],"^It$")&&optlist) { | |
302 | if(optlist==1) | |
aff51935 | 303 | add(".IP \\(bu") |
4bbf95fa | 304 | else if(optlist==2) |
aff51935 | 305 | add(".IP " ++enum ".") |
4bbf95fa | 306 | else if(optlist==3) { |
aff51935 | 307 | add(".TP") |
308 | prenl++ | |
309 | if(match(words[w+1],"^Pa|Ev$")) { | |
310 | add(".B") | |
311 | w++ | |
312 | } | |
4bbf95fa | 313 | } else if(optlist==4) |
aff51935 | 314 | add(".IP") |
4bbf95fa | 315 | } else if(match(words[w],"^Sm$")) { |
316 | if(match(words[w+1],"off")) | |
aff51935 | 317 | nospace=2 |
4bbf95fa | 318 | else if(match(words[w+1],"on")) |
aff51935 | 319 | nospace=0 |
4bbf95fa | 320 | w++ |
321 | } else if(!skip) { | |
322 | add(words[w]) | |
323 | } | |
324 | } | |
325 | if(match(line,"^\\.[^a-zA-Z]")) | |
326 | sub("^\\.","",line) | |
327 | if(parens) | |
328 | add(")") | |
329 | if(angles) | |
330 | add(">") | |
331 | if(option) | |
332 | add("]") | |
333 | if(ext&&!extopt&&!match(line," $")) | |
334 | add(OFS) | |
335 | if(!ext&&!extopt&&length(line)) { | |
336 | print line | |
337 | prenl=0 | |
338 | line="" | |
339 | } | |
340 | } |