libarchive /doc/mdoc2mediawiki.awk

Language awk Lines 446
MD5 Hash 237d5f528dc8ea7c86a009cf0bc6f8b7 Estimated Cost $10,052 (why?)
Repository https://code.google.com/p/libarchive/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/awk
#
# Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Dramatically overhauled by Tim Kientzle.  This version almost
# handles library-style pages with Fn, Ft, etc commands.  Still
# a lot of problems...

BEGIN {
  displaylines = 0
  listdepth = 0
  trailer = ""
  out = ""
  sep = ""
  nextsep = " "
  spaces = "                    "
}

# Add a word with appropriate preceding whitespace
# Maintain a short queue of the expected upcoming word separators.
function add(str) {
  out=out sep str
  sep = nextsep
  nextsep = " "
}

# Add a word with no following whitespace
# Use for opening punctuation such as '('
function addopen(str) {
  add(str)
  sep = ""
}

# Add a word with no preceding whitespace
# Use for closing punctuation such as ')' or '.'
function addclose(str) {
  sep = ""
  add(str)
}

# Add a word with no space before or after
# Use for separating punctuation such as '='
function addpunct(str) {
  sep = ""
  add(str)
  sep = ""
}

# Emit the current line so far
function endline() {
  addclose(trailer)
  trailer = ""
  if(length(out) > 0) {
    print out
    out=""
  }
  if(displaylines > 0) {
    displaylines = displaylines - 1
    if (displaylines == 0)
      dispend()
  }
  # First word on next line has no preceding whitespace
  sep = ""
}

function linecmd(cmd) {
  endline()
  add(cmd)
  endline()
}

function breakline() {
  linecmd("<br>")
}

# Start an indented display
function dispstart() {
  linecmd("<pre>")
}

# End an indented display
function dispend() {
  linecmd("</pre>")
}

# Collect rest of input line
function wtail() {
  retval=""
  while(w<nwords) {
    if(length(retval))
      retval=retval " "
    retval=retval words[++w]
  }
  return retval
}

function splitwords(l, dest, n, o, w) {
  n = 1
  delete dest
  while (length(l) > 0) {
    sub("^[ \t]*", "", l)
    if (match(l, "^\"")) {
      l = substr(l, 2)
      o = index(l, "\"")
      if (o > 0) {
	w = substr(l, 1, o-1)
	l = substr(l, o+1)
	dest[n++] = w
      } else {
	dest[n++] = l
	l = ""
      }
    } else {
      o = match(l, "[ \t]")
      if (o > 0) {
	w = substr(l, 1, o-1)
	l = substr(l, o+1)
	dest[n++] = w
      } else {
	dest[n++] = l
	l = ""
      }
    }
  }
  return n-1
}

! /^\./ {
  out = $0 " "
  endline()
  next
}

/^\.\\"/ { next }

{
  sub("^\\.","")
  nwords=splitwords($0, words)
  # TODO: Instead of iterating 'w' over the array, have a separate
  # function that returns 'next word' and use that.  This will allow
  # proper handling of double-quoted arguments as well.
  for(w=1;w<=nwords;w++) {
    if(match(words[w],"^Li$")) { # Literal; rest of line is unformatted
      dispstart()
      displaylines = 1
    } else if(match(words[w],"^Dl$")) { # Display literal
      dispstart()
      displaylines = 1
    } else if(match(words[w],"^Bd$")) { # Begin display
      if(match(words[w+1],"-literal")) {
        dispstart()
	displaylines=10000
	w=nwords
      }
    } else if(match(words[w],"^Ed$")) { # End display
      displaylines = 0
      dispend()
    } else if(match(words[w],"^Ns$")) { # Suppress space before next word
      sep=""
    } else if(match(words[w],"^No$")) { # Normal text
      add(words[++w])
    } else if(match(words[w],"^Dq$")) { # Quote
      addopen("\"")
      add(words[++w])
      while(w<nwords&&!match(words[w+1],"^[\\.,]"))
	add(words[++w])
      addclose("\"")
    } else if(match(words[w],"^Do$")) {
      addopen("\"")
    } else if(match(words[w],"^Dc$")) {
      addclose("\"")
    } else if(match(words[w],"^Oo$")) {
      addopen("<nowiki>[</nowiki>")
    } else if(match(words[w],"^Oc$")) {
      addclose("<nowiki>]</nowiki>")
    } else if(match(words[w],"^Ao$")) {
      addopen("&lt;")
    } else if(match(words[w],"^Ac$")) {
      addclose("&gt;")
    } else if(match(words[w],"^Dd$")) {
      date=wtail()
      next
    } else if(match(words[w],"^Dt$")) {
      id=words[++w] "(" words[++w] ")"
      next
    } else if(match(words[w],"^Ox$")) {
      add("OpenBSD")
    } else if(match(words[w],"^Fx$")) {
      add("FreeBSD")
    } else if(match(words[w],"^Bx$")) {
      add("BSD")
    } else if(match(words[w],"^Nx$")) {
      add("NetBSD")
    } else if(match(words[w],"^St$")) {
      if (match(words[w+1], "^-p1003.1$")) {
         w++
         add("<nowiki>IEEE Std 1003.1 (``POSIX.1'')</nowiki>")
      } else if(match(words[w+1], "^-p1003.1-96$")) {
         w++
         add("<nowiki>ISO/IEC 9945-1:1996 (``POSIX.1'')</nowiki>")
      } else if(match(words[w+1], "^-p1003.1-88$")) {
         w++
         add("<nowiki>IEEE Std 1003.1-1988 (``POSIX.1'')</nowiki>")
      } else if(match(words[w+1], "^-p1003.1-2001$")) {
         w++
         add("<nowiki>IEEE Std 1003.1-2001 (``POSIX.1'')</nowiki>")
      } else if(match(words[w+1], "^-susv2$")) {
         w++
         add("<nowiki>Version 2 of the Single UNIX Specification (``SUSv2'')</nowiki>")
      }
    } else if(match(words[w],"^Ex$")) {
      if (match(words[w+1], "^-std$")) {
         w++
         add("The '''" name "''' utility exits 0 on success, and &gt;0 if an error occurs.")
      }
    } else if(match(words[w],"^Os$")) {
      add(id " manual page")
    } else if(match(words[w],"^Sh$")) {
      section=wtail()
      linecmd("== " section " ==")
    } else if(match(words[w],"^Xr$")) {
      add("'''" words[++w] "'''(" words[++w] ")" words[++w] " ")
    } else if(match(words[w],"^Nm$")) {
      if(match(section,"SYNOPSIS"))
        breakline()
      if(w >= nwords)
	n=name
      else if (match(words[w+1], "^[A-Z][a-z]$"))
	n=name
      else if (match(words[w+1], "^[.,;:]$"))
	n=name
      else {
        n=words[++w]
        if(!length(name))
          name=n
      }
      if(!length(n))
        n=name
      if (displaylines == 0)
	add("'''" n "'''")
      else
	add(n)
    } else if(match(words[w],"^Nd$")) {
      add("- " wtail())
    } else if(match(words[w],"^Fl$")) {
      addopen("-")
    } else if(match(words[w],"^Ar$")) {
      if(w==nwords)
	add("''file ...''" " ")
      else {
	++w
	gsub("<", "&lt;", words[w])
	add("''" words[w] "''" " ")
      }
    } else if(match(words[w],"^Cm$")) {
      ++w
      if (displaylines == 0) {
	add("'''" words[w] "'''" " ")
      } else
	add(words[w])
	l = wtail()
	add(l " ")
    } else if(match(words[w],"^Op$")) {
      addopen("<nowiki>[</nowiki>")
      option=1
      trailer="<nowiki>]</nowiki>" trailer
    } else if(match(words[w],"^Pp$")) {
      ++w
      endline()
      print ""
    } else if(match(words[w],"^An$")) {
      if (match(words[w+1],"-nosplit"))
	++w
      endline()
    } else if(match(words[w],"^Ss$")) {
      add("===")
      trailer="==="
    } else if(match(words[w],"^Ft$")) {
      if (match(section, "SYNOPSIS")) {
	breakline()
      }
      l = wtail()
      add("'''" l "'''")
      if (match(section, "SYNOPSIS")) {
	breakline()
      }
    } else if(match(words[w],"^Fn$")) {
      ++w
      F = "'''" words[w] "'''("
      Fsep = ""
      while(w<nwords) {
	++w
	if (match(words[w], "^[.,:]$")) {
	  --w
	  break
	}
	F = F Fsep "''"  words[w] "''"
	Fsep = ", "
      }
      add(F ")")
      if (match(section, "SYNOPSIS")) {
	addclose(";")
      }
    } else if(match(words[w],"^Fo$")) {
      w++
      F = "'''" words[w] "'''("
      Fsep = ""
    } else if(match(words[w],"^Fa$")) {
      w++
      F = F Fsep "''"  words[w] "''"
      Fsep = ", "
    } else if(match(words[w],"^Fc$")) {
      add(F ")")
      if (match(section, "SYNOPSIS")) {
	addclose(";")
      }
    } else if(match(words[w],"^Va$")) {
      w++
      add("''" words[w] "''")
    } else if(match(words[w],"^In$")) {
      w++
      add("'''<nowiki>#include <" words[w] "></nowiki>'''")
    } else if(match(words[w],"^Pa$")) {
      w++
#      if(match(words[w],"^\\."))
#	add("\\&")
      if (displaylines == 0)
	add("''" words[w] "''")
      else
	add(words[w])
    } else if(match(words[w],"^Dv$")) {
      linecmd()
      l = wtail()
      add(l " ")
    } else if(match(words[w],"^Em|Ev$")) {
      add(".IR")
    } else if(match(words[w],"^Pq$")) {
      addopen("(")
      trailer=")" trailer
    } else if(match(words[w],"^Aq$")) {
      addopen(" &lt;")
      trailer="&gt;" trailer
    } else if(match(words[w],"^Brq$")) {
      addopen("<nowiki>{</nowiki>")
      trailer="<nowiki>}</nowiki>" trailer
    } else if(match(words[w],"^S[xy]$")) {
      add(".B " wtail())
    } else if(match(words[w],"^Tn$")) {
      n=wtail()
      add("'''" n "'''")
    } else if(match(words[w],"^Ic$")) {
      add("''")
      trailer="''" trailer
    } else if(match(words[w],"^Bl$")) {
      ++listdepth
      listnext[listdepth]=""
      if(match(words[w+1],"-bullet")) {
	optlist[listdepth]=1
	addopen("<ul>")
	listclose[listdepth]="</ul>"
      } else if(match(words[w+1],"-enum")) {
	optlist[listdepth]=2
	enum=0
	addopen("<ol>")
	listclose[listdepth]="</ol>"
      } else if(match(words[w+1],"-tag")) {
	optlist[listdepth]=3
	addopen("<dl>")
	listclose[listdepth]="</dl>"
      } else if(match(words[w+1],"-item")) {
	optlist[listdepth]=4
	addopen("<ul>")
	listclose[listdepth]="</ul>"
      }
      w=nwords
    } else if(match(words[w],"^El$")) {
      addclose(listnext[listdepth])
      addclose(listclose[listdepth])
      listclose[listdepth]=""
      listdepth--
    } else if(match(words[w],"^It$")) {
      addclose(listnext[listdepth])
      if(optlist[listdepth]==1) {
	addpunct("<li>")
	listnext[listdepth] = "</li>"
      } else if(optlist[listdepth]==2) {
	addpunct("<li>")
	listnext[listdepth] = "</li>"
      } else if(optlist[listdepth]==3) {
	addpunct("<dt>")
	listnext[listdepth] = "</dt>"
	if(match(words[w+1],"^Xo$")) {
	  # Suppress trailer
	  w++
	} else if(match(words[w+1],"^Pa$|^Ev$")) {
	  addopen("'''")
	  w++
	  add(words[++w] "'''")
	} else {
	  trailer = listnext[listdepth] "<dd>" trailer
	  listnext[listdepth] = "</dd>"
	}
      } else if(optlist[listdepth]==4) {
	addpunct("<li>")
	listnext[listdepth] = "</li>"
      }
    } else if(match(words[w], "^Vt$")) {
      w++
      add("''" words[w] "''")
    } else if(match(words[w],"^Xo$")) {
      # TODO: Figure out how to handle this
    } else if(match(words[w],"^Xc$")) {
      # TODO: Figure out how to handle this
      if (optlist[listdepth] == 3) {
	addclose(listnext[listdepth])
	addopen("<dd>")
	listnext[listdepth] = "</dd>"
      }
    } else if(match(words[w],"^[=]$")) {
      addpunct(words[w])
    } else if(match(words[w],"^[[{(]$")) {
      addopen(words[w])
    } else if(match(words[w],"^[\\])}.,;:]$")) {
      addclose(words[w])
    } else {
      sub("\\\\&", "", words[w])
      add(words[w])
    }
  }
  if(match(out,"^\\.[^a-zA-Z]"))
    sub("^\\.","",out)
  endline()
}
Back to Top