PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libarchive/doc/mdoc2wiki.awk

https://bitbucket.org/dwestfall/ios-libarchive
AWK | 448 lines | 392 code | 17 blank | 39 comment | 0 complexity | 831ffe83ebedcb2f9956e5b54b5033ef MD5 | raw file
Possible License(s): LGPL-2.0
  1. #!/usr/bin/awk
  2. #
  3. # Copyright (c) 2003 Peter Stuge <stuge-mdoc2man@cdy.org>
  4. #
  5. # Permission to use, copy, modify, and distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. # Dramatically overhauled by Tim Kientzle. This version almost
  17. # handles library-style pages with Fn, Ft, etc commands. Still
  18. # a lot of problems...
  19. BEGIN {
  20. displaylines = 0
  21. listdepth = 0
  22. trailer = ""
  23. out = ""
  24. sep = ""
  25. nextsep = " "
  26. spaces = " "
  27. }
  28. # Add a word with appropriate preceding whitespace
  29. # Maintain a short queue of the expected upcoming word separators.
  30. function add(str) {
  31. out=out sep str
  32. sep = nextsep
  33. nextsep = " "
  34. }
  35. # Add a word with no following whitespace
  36. # Use for opening punctuation such as '('
  37. function addopen(str) {
  38. add(str)
  39. sep = ""
  40. }
  41. # Add a word with no preceding whitespace
  42. # Use for closing punctuation such as ')' or '.'
  43. function addclose(str) {
  44. sep = ""
  45. add(str)
  46. }
  47. # Add a word with no space before or after
  48. # Use for separating punctuation such as '='
  49. function addpunct(str) {
  50. sep = ""
  51. add(str)
  52. sep = ""
  53. }
  54. # Emit the current line so far
  55. function endline() {
  56. addclose(trailer)
  57. trailer = ""
  58. if(length(out) > 0) {
  59. print out
  60. out=""
  61. }
  62. if(displaylines > 0) {
  63. displaylines = displaylines - 1
  64. if (displaylines == 0)
  65. dispend()
  66. }
  67. # First word on next line has no preceding whitespace
  68. sep = ""
  69. }
  70. function linecmd(cmd) {
  71. endline()
  72. add(cmd)
  73. endline()
  74. }
  75. function breakline() {
  76. linecmd("<br>")
  77. }
  78. # Start an indented display
  79. function dispstart() {
  80. linecmd("{{{")
  81. }
  82. # End an indented display
  83. function dispend() {
  84. linecmd("}}}")
  85. }
  86. # Collect rest of input line
  87. function wtail() {
  88. retval=""
  89. while(w<nwords) {
  90. if(length(retval))
  91. retval=retval " "
  92. retval=retval words[++w]
  93. }
  94. return retval
  95. }
  96. function splitwords(l, dest, n, o, w) {
  97. n = 1
  98. delete dest
  99. while (length(l) > 0) {
  100. sub("^[ \t]*", "", l)
  101. if (match(l, "^\"")) {
  102. l = substr(l, 2)
  103. o = index(l, "\"")
  104. if (o > 0) {
  105. w = substr(l, 1, o-1)
  106. l = substr(l, o+1)
  107. dest[n++] = w
  108. } else {
  109. dest[n++] = l
  110. l = ""
  111. }
  112. } else {
  113. o = match(l, "[ \t]")
  114. if (o > 0) {
  115. w = substr(l, 1, o-1)
  116. l = substr(l, o+1)
  117. dest[n++] = w
  118. } else {
  119. dest[n++] = l
  120. l = ""
  121. }
  122. }
  123. }
  124. return n-1
  125. }
  126. ! /^\./ {
  127. out = $0
  128. endline()
  129. next
  130. }
  131. /^\.\\"/ { next }
  132. {
  133. sub("^\\.","")
  134. nwords=splitwords($0, words)
  135. # TODO: Instead of iterating 'w' over the array, have a separate
  136. # function that returns 'next word' and use that. This will allow
  137. # proper handling of double-quoted arguments as well.
  138. for(w=1;w<=nwords;w++) {
  139. if(match(words[w],"^Li$")) { # Literal; rest of line is unformatted
  140. dispstart()
  141. displaylines = 1
  142. } else if(match(words[w],"^Dl$")) { # Display literal
  143. dispstart()
  144. displaylines = 1
  145. } else if(match(words[w],"^Bd$")) { # Begin display
  146. if(match(words[w+1],"-literal")) {
  147. dispstart()
  148. displaylines=10000
  149. w=nwords
  150. }
  151. } else if(match(words[w],"^Ed$")) { # End display
  152. displaylines = 0
  153. dispend()
  154. } else if(match(words[w],"^Ns$")) { # Suppress space before next word
  155. sep=""
  156. } else if(match(words[w],"^No$")) { # Normal text
  157. add(words[++w])
  158. } else if(match(words[w],"^Dq$")) { # Quote
  159. addopen("\"")
  160. add(words[++w])
  161. while(w<nwords&&!match(words[w+1],"^[\\.,]"))
  162. add(words[++w])
  163. addclose("\"")
  164. } else if(match(words[w],"^Do$")) {
  165. addopen("\"")
  166. } else if(match(words[w],"^Dc$")) {
  167. addclose("\"")
  168. } else if(match(words[w],"^Oo$")) {
  169. addopen("`[`")
  170. } else if(match(words[w],"^Oc$")) {
  171. addclose("`]`")
  172. } else if(match(words[w],"^Ao$")) {
  173. addopen("`<`")
  174. } else if(match(words[w],"^Ac$")) {
  175. addclose("`>`")
  176. } else if(match(words[w],"^Dd$")) {
  177. date=wtail()
  178. next
  179. } else if(match(words[w],"^Dt$")) {
  180. id=wtail()
  181. next
  182. } else if(match(words[w],"^Ox$")) {
  183. add("OpenBSD")
  184. } else if(match(words[w],"^Fx$")) {
  185. add("FreeBSD")
  186. } else if(match(words[w],"^Bx$")) {
  187. add("BSD")
  188. } else if(match(words[w],"^Nx$")) {
  189. add("NetBSD")
  190. } else if(match(words[w],"^St$")) {
  191. if (match(words[w+1], "^-p1003.1$")) {
  192. w++
  193. add("IEEE Std 1003.1 (``POSIX.1'')")
  194. } else if(match(words[w+1], "^-p1003.1-96$")) {
  195. w++
  196. add("ISO/IEC 9945-1:1996 (``POSIX.1'')")
  197. } else if(match(words[w+1], "^-p1003.1-88$")) {
  198. w++
  199. add("IEEE Std 1003.1-1988 (``POSIX.1'')")
  200. } else if(match(words[w+1], "^-p1003.1-2001$")) {
  201. w++
  202. add("IEEE Std 1003.1-2001 (``POSIX.1'')")
  203. } else if(match(words[w+1], "^-susv2$")) {
  204. w++
  205. add("Version 2 of the Single UNIX Specification (``SUSv2'')")
  206. }
  207. } else if(match(words[w],"^Ex$")) {
  208. if (match(words[w+1], "^-std$")) {
  209. w++
  210. add("The *" name "* utility exits 0 on success, and >0 if an error occurs.")
  211. }
  212. } else if(match(words[w],"^Os$")) {
  213. add("#summary " id " manual page")
  214. } else if(match(words[w],"^Sh$")) {
  215. section=wtail()
  216. linecmd("== " section " ==")
  217. } else if(match(words[w],"^Xr$")) {
  218. add("*" words[++w] "*(" words[++w] ")" words[++w])
  219. } else if(match(words[w],"^Nm$")) {
  220. if(match(section,"SYNOPSIS"))
  221. breakline()
  222. if(w >= nwords)
  223. n=name
  224. else if (match(words[w+1], "^[A-Z][a-z]$"))
  225. n=name
  226. else if (match(words[w+1], "^[.,;:]$"))
  227. n=name
  228. else {
  229. n=words[++w]
  230. if(!length(name))
  231. name=n
  232. }
  233. if(!length(n))
  234. n=name
  235. if (displaylines == 0)
  236. add("*" n "*")
  237. else
  238. add(n)
  239. } else if(match(words[w],"^Nd$")) {
  240. add("- " wtail())
  241. } else if(match(words[w],"^Fl$")) {
  242. if (displaylines == 0)
  243. add("*-" words[++w] "*")
  244. else
  245. add("-" words[++w])
  246. } else if(match(words[w],"^Ar$")) {
  247. if(w==nwords)
  248. add("_file ..._")
  249. else {
  250. ++w
  251. gsub("<", "`<`", words[w])
  252. add("_" words[w] "_")
  253. }
  254. } else if(match(words[w],"^Cm$")) {
  255. ++w
  256. if (displaylines == 0) {
  257. gsub("^_", "`_`", words[w])
  258. gsub("\\*$", "`*`", words[w])
  259. add("*" words[w] "*")
  260. } else
  261. add(words[w])
  262. } else if(match(words[w],"^Op$")) {
  263. addopen("`[`")
  264. option=1
  265. trailer="`]`" trailer
  266. } else if(match(words[w],"^Pp$")) {
  267. ++w
  268. endline()
  269. print ""
  270. } else if(match(words[w],"^An$")) {
  271. if (match(words[w+1],"-nosplit"))
  272. ++w
  273. endline()
  274. } else if(match(words[w],"^Ss$")) {
  275. add("===")
  276. trailer="==="
  277. } else if(match(words[w],"^Ft$")) {
  278. if (match(section, "SYNOPSIS")) {
  279. breakline()
  280. }
  281. l = wtail()
  282. gsub("\\*", "`*`", l)
  283. add("*" l "*")
  284. if (match(section, "SYNOPSIS")) {
  285. breakline()
  286. }
  287. } else if(match(words[w],"^Fn$")) {
  288. ++w
  289. F = "*" words[w] "*("
  290. Fsep = ""
  291. while(w<nwords) {
  292. ++w
  293. if (match(words[w], "^[.,:]$")) {
  294. --w
  295. break
  296. }
  297. gsub("\\*", "`*`", words[w])
  298. F = F Fsep "_" words[w] "_"
  299. Fsep = ", "
  300. }
  301. add(F ")")
  302. if (match(section, "SYNOPSIS")) {
  303. addclose(";")
  304. }
  305. } else if(match(words[w],"^Fo$")) {
  306. w++
  307. F = "*" words[w] "*("
  308. Fsep = ""
  309. } else if(match(words[w],"^Fa$")) {
  310. w++
  311. gsub("\\*", "`*`", words[w])
  312. F = F Fsep "_" words[w] "_"
  313. Fsep = ", "
  314. } else if(match(words[w],"^Fc$")) {
  315. add(F ")")
  316. if (match(section, "SYNOPSIS")) {
  317. addclose(";")
  318. }
  319. } else if(match(words[w],"^Va$")) {
  320. w++
  321. add("_" words[w] "_")
  322. } else if(match(words[w],"^In$")) {
  323. w++
  324. add("*#include <" words[w] ">*")
  325. } else if(match(words[w],"^Pa$")) {
  326. w++
  327. # if(match(words[w],"^\\."))
  328. # add("\\&")
  329. if (displaylines == 0)
  330. add("_" words[w] "_")
  331. else
  332. add(words[w])
  333. } else if(match(words[w],"^Dv$")) {
  334. linecmd()
  335. } else if(match(words[w],"^Em|Ev$")) {
  336. add(".IR")
  337. } else if(match(words[w],"^Pq$")) {
  338. addopen("(")
  339. trailer=")" trailer
  340. } else if(match(words[w],"^Aq$")) {
  341. addopen(" <")
  342. trailer=">" trailer
  343. } else if(match(words[w],"^Brq$")) {
  344. addopen("{")
  345. trailer="}" trailer
  346. } else if(match(words[w],"^S[xy]$")) {
  347. add(".B " wtail())
  348. } else if(match(words[w],"^Tn$")) {
  349. n=wtail()
  350. gsub("\\*$", "`*`", n)
  351. add("*" n "*")
  352. } else if(match(words[w],"^Ic$")) {
  353. add("\\fB")
  354. trailer="\\fP" trailer
  355. } else if(match(words[w],"^Bl$")) {
  356. ++listdepth
  357. listnext[listdepth]=""
  358. if(match(words[w+1],"-bullet")) {
  359. optlist[listdepth]=1
  360. addopen("<ul>")
  361. listclose[listdepth]="</ul>"
  362. } else if(match(words[w+1],"-enum")) {
  363. optlist[listdepth]=2
  364. enum=0
  365. addopen("<ol>")
  366. listclose[listdepth]="</ol>"
  367. } else if(match(words[w+1],"-tag")) {
  368. optlist[listdepth]=3
  369. addopen("<dl>")
  370. listclose[listdepth]="</dl>"
  371. } else if(match(words[w+1],"-item")) {
  372. optlist[listdepth]=4
  373. addopen("<ul>")
  374. listclose[listdepth]="</ul>"
  375. }
  376. w=nwords
  377. } else if(match(words[w],"^El$")) {
  378. addclose(listnext[listdepth])
  379. addclose(listclose[listdepth])
  380. listclose[listdepth]=""
  381. listdepth--
  382. } else if(match(words[w],"^It$")) {
  383. addclose(listnext[listdepth])
  384. if(optlist[listdepth]==1) {
  385. addpunct("<li>")
  386. listnext[listdepth] = "</li>"
  387. } else if(optlist[listdepth]==2) {
  388. addpunct("<li>")
  389. listnext[listdepth] = "</li>"
  390. } else if(optlist[listdepth]==3) {
  391. addpunct("<dt>")
  392. listnext[listdepth] = "</dt>"
  393. if(match(words[w+1],"^Xo$")) {
  394. # Suppress trailer
  395. w++
  396. } else if(match(words[w+1],"^Pa$|^Ev$")) {
  397. addopen("*")
  398. w++
  399. add(words[++w] "*")
  400. } else {
  401. trailer = listnext[listdepth] "<dd>" trailer
  402. listnext[listdepth] = "</dd>"
  403. }
  404. } else if(optlist[listdepth]==4) {
  405. addpunct("<li>")
  406. listnext[listdepth] = "</li>"
  407. }
  408. } else if(match(words[w],"^Xo$")) {
  409. # TODO: Figure out how to handle this
  410. } else if(match(words[w],"^Xc$")) {
  411. # TODO: Figure out how to handle this
  412. if (optlist[listdepth] == 3) {
  413. addclose(listnext[listdepth])
  414. addopen("<dd>")
  415. listnext[listdepth] = "</dd>"
  416. }
  417. } else if(match(words[w],"^[=]$")) {
  418. addpunct(words[w])
  419. } else if(match(words[w],"^[\[{(]$")) {
  420. addopen(words[w])
  421. } else if(match(words[w],"^[\\\])}.,;:]$")) {
  422. addclose(words[w])
  423. } else {
  424. sub("\\\\&", "", words[w])
  425. add(words[w])
  426. }
  427. }
  428. if(match(out,"^\\.[^a-zA-Z]"))
  429. sub("^\\.","",out)
  430. endline()
  431. }