lua build scripts
[chaz/yoink] / doc / man2html.lua
1 #!/usr/bin/env lua
2
3 --
4 -- Yoink
5 -- Run this script to convert the manual page to html.
6 --
7
8 function showhelp()
9 print("Usage: "..arg[0].." [-eh] [-i manpage] [-o htmlfile]")
10 print(" Convert the manual page to html with groff.")
11 print("")
12 print(" -e Hide email addresses in the manual page.")
13 print(" -h Show this help an exit.")
14 print(" -i Specify the manual page to convert.")
15 print(" -o Specify output path of html file.")
16 end
17
18 -----
19
20 function die(...) print("die:", ...); os.exit(1) end
21 function isReadable(file) return os.execute("test -r "..file) == 0 end
22
23 arg.hideEmail = false
24 arg.output = "yoink.html"
25
26 arg.input = "yoink.6"
27 if not isReadable(arg.input) then arg.input = "doc/yoink.6" end
28
29 do
30 local t = {
31 ["-e"] = function(a,i) a.hideEmail = true end,
32 ["-h"] = function(a,i) showhelp(); os.exit() end,
33 ["-i"] = function(a,i) a.input = a[i+1]; return 1 end,
34 ["-o"] = function(a,i) a.output = a[i+1]; return 1 end
35 }
36 function parseArgs(args)
37 local skip = 0
38 for i,v in ipairs(args) do
39 if skip <= 0 then
40 if type(t[v]) ~= "function" then
41 print("unknown option: "..v)
42 showhelp(); os.exit(1)
43 end
44 skip = t[v](args, i)
45 if tonumber(skip) then skip = skip + 1 else skip = 1 end
46 end
47 skip = skip - 1
48 end
49 end
50 end
51 parseArgs(arg)
52
53 filters = {
54 function(t) -- 1. Edit page title
55 return t:gsub("(<title>).*(</title>)", "%1Yoink Manual%2")
56 end,
57 function(t) -- 2. Insert footer before </body>
58 return t:gsub("</body>", [[
59 <p style="font-size: 9px; text-align: center;">
60 This manual page was generated on ]]..os.date("%d %b %Y")..[[.
61 For more information, go to the
62 <a href="http://www.dogcows.com/yoink/">Yoink website</a>.
63 </p>
64 </body> ]])
65 end,
66 arg.hideEmail and function(t) -- 3. Hide email addresses
67 return t:gsub("&lt;.+@.+&gt;", "&lt;email address hidden&gt;")
68 end or nil
69 }
70
71 output, err = io.open(arg.output, "w")
72 if not output then die("io.open", err) end
73 input = io.popen("groff -t -e -mandoc -Thtml "..arg.input)
74
75 for line in input:lines() do
76 for _,filter in ipairs(filters) do line = filter(line) end
77 output:write(line.."\n")
78 end
79
80 output:close()
81 input:close()
82
This page took 0.033117 seconds and 4 git commands to generate.