#!/bin/env lua -- This source code form is subject to the terms of the MPL-v2 https://mozilla.org/MPL/2.0/ -- -- Any documentation generated is licensed under PDL-v1 https://www.openoffice.org/licenses/PDL.html local sourceFileName = assert(arg[1]) local textFileName = assert(arg[2]) local sourceFile = assert(io.open(sourceFileName)) local tf = assert(io.open(textFileName, "w")) local cmdArgs = {} for i=1,#arg do if skipNext then skipNext = false else local a = arg[i] if a == "-e" or a == "--ext" then cmdArgs.ext = arg[i+1] skipNext = true end end end local ext = cmdArgs.ext or sourceFileName:match("[^.]+$") local marker if ext == "c" then marker = "//%*" elseif ext == "lua" then marker = "--%*" else error("Unkown extension", ext) end local name = sourceFileName:match("([^/]+)%.%w+$") local functions = {} local description = {} function bold(str) return "\\fB"..str.."\\fR" end function italic(str) return "\\fI"..str.."\\fR" end for line in sourceFile:lines() do local s, e = line:find("^%s*"..marker.."%s*") if s then local str = line:sub(e+1) s, e = str:find("%w%([^)]*%)") if s then local proto = str:sub(1, e) local returnProto = "" local des = str:sub(e+2) if proto:match("^[%w|,[%]:.]+ ") then returnProto = proto:match("^([^(]+) ")--:gsub("%w+", italic("%1")):gsub("%|", " | "):gsub(",", ", ").." " returnProto = returnProto:gsub("^(%w+)", bold("%1")):gsub("| (%w+)", "| "..bold("%1")):gsub(", (%w+)", ", "..bold("%1")):gsub("%[(%w+)", "["..bold("%1")) returnProto = returnProto:gsub(" %w+", italic("%1")) returnProto = returnProto.." " proto = proto:gsub("^([^(]+) (%w+)", "%2") end proto = proto:gsub("%w+[,)%]]?", function(word) if word:match("[,)%]]") then return italic(word:sub(1, -2)) .. word:sub(-1) else return bold(word) end end) des = des:gsub("%*%*([%w$.]+)%*%*", italic("%1")) des = des:gsub("%*([%w$.]+)%*", bold("%1")) table.insert(functions, { proto = proto, returnProto = returnProto, des = des }) else s, e = str:find("^%.%w+") if e then local cmd = str:sub(2, e) local txt = str:sub(e+1):gsub("^%s+", "") if cmd == "name" then name = name .. " - " .. txt elseif cmd == "desc" then txt = txt:gsub("%*%*([%w$.]+)%*%*", italic("%1")) txt = txt:gsub("%*([%w$.]+)%*", bold("%1")) table.insert(description, txt) end end end end end table.sort(functions, function(a, b) return a.proto < b.proto end) for f,func in pairs(functions) do func.proto = func.returnProto..func.proto end tf:write(".TH thornWM 3\n.SH NAME\n"..name.."\n") tf:write(".SH SYNOPSIS\n") for f,func in pairs(functions) do tf:write(".TP\n") tf:write(func.proto.."\n") end tf:write(".SH DESCRIPTION\n") tf:write(table.concat(description, "\n").."\n") for f,func in pairs(functions) do tf:write(".TP\n"..func.proto.."\n") tf:write(func.des.."\n") end tf:write(".SH COPYRIGHT\nThe contents of this Documentation are subject to the Public Documentation License Version 1.0\n")