--- LuaDist documentation generation functions
-- Peter Kapec, LuaDist Project, 2008
module ("dist.doc", package.seeall)
local config = require "dist.config"
local sys = require "dist.sys"
--- Create documentation index for a package
-- @param dest string: Path to Dist folder in which to create an doc. index
-- @param name string: Name of the package to search for (case sensitive)
-- @param ver string or nil: Version constraint or nil for newest version
-- @return boolean or (nil, error): Succes
function packageDoc(dest, name, version)
assert(type(dest) == "string")
assert(type(name) == "string")
assert(type(version) == "string")
local page = {}
sys.makeDir(dest.."/" .. config.share)
local package = dest.."/" .. config.share .. "/"..name
sys.makeDir(package)
-- If package provides then skip
if sys.exists(package.."/index.html") then return true end
-- Header & footer templates
local header = string.format([[
%s-%s
Documentation for %s-%s package:
]], name, version, name, version)
local footer =
[[
Generated by the LuaDist project.
]]
local function iter(p)
local page
for _,file in pairs(sys.dir(p) or {}) do
page = page or {}
if sys.isDir(p.."/"..file) then
table.insert(page,""..file.."")
table.insert(page, "")
table.insert(page, iter(p.."/"..file) or "")
table.insert(page, "
")
else
local f = string.sub(p.."/"..file, string.len(package)+2)
table.insert(page, string.format("%s", f, file))
end
end
if page then return table.concat(page,"\n") end
return nil
end
table.insert(page, header)
local list = iter(package)
if not list then list = "No documentation available." end
table.insert(page, list)
table.insert(page, footer)
local html = assert(io.open(package.."/index.html", "w"))
html:write(table.concat(page, "\n"))
html:close()
return true
end
--- Create documentation index for all installed packages in a Dist
-- @param dest string: Path to Dist folder in which to create an doc. index
-- @return boolean or (nil, error): Succes
function repoDoc(dest)
assert(type(dest) == "string")
local share = dest .. "/" .. config.share
local packages = {}
for _,file in pairs(sys.dir(share)) do
if sys.isDir(share .. "/" ..file) and sys.exists(share .. "/" .. file .. "/index.html") then
table.insert(packages, string.format("%s", file, file))
end
end
local list = table.concat(packages, "\n")
-- Header & footer templates
local header = [[
LuaDist
LuaDist
Following packages are currently installed and documentation for them is
available.
packages:
Generated by the LuaDist project.
]]
-- Create the index.html
local page = header.."\n"..list.."\n"..footer
-- Write the index.html
local html = assert(io.open(share .. "/index.html", "w"))
html:write(page)
html:close()
return true
end