2011-09-11 19:17:05 +02:00
|
|
|
------ generating HTML output ---------
|
|
|
|
-- Although this can be generalized for outputting any format, since the template
|
|
|
|
-- is language-agnostic, this implementation concentrates on HTML.
|
|
|
|
-- This does the actual generation of HTML, and provides support functions in the ldoc
|
|
|
|
-- table for the template
|
|
|
|
--
|
|
|
|
-- A fair amount of the complexity comes from operating in two basic modes; first, where
|
|
|
|
-- there is a number of modules (classic LuaDoc) or otherwise, where there is only one
|
|
|
|
-- module and the index contains the documentation for that module.
|
|
|
|
--
|
|
|
|
-- Like LuaDoc, LDoc puts similar kinds of documentation files in their own directories.
|
|
|
|
-- So module docs go into 'modules/', scripts go into 'scripts/', and so forth. LDoc
|
|
|
|
-- generalizes the idea of these project-level categories and in fact custom categories
|
|
|
|
-- can be created (refered to as 'kinds' in the code)
|
|
|
|
|
2012-10-28 18:51:00 +01:00
|
|
|
local List = require 'pl.List'
|
|
|
|
local utils = require 'pl.utils'
|
|
|
|
local path = require 'pl.path'
|
|
|
|
local stringx = require 'pl.stringx'
|
2011-09-11 19:17:05 +02:00
|
|
|
local template = require 'pl.template'
|
2013-07-18 10:12:25 +02:00
|
|
|
local tablex = require 'pl.tablex'
|
2014-01-08 04:55:32 +01:00
|
|
|
local OrderedMap = require 'pl.OrderedMap'
|
2011-09-11 19:17:05 +02:00
|
|
|
local tools = require 'ldoc.tools'
|
2011-12-06 18:19:09 +01:00
|
|
|
local markup = require 'ldoc.markup'
|
2013-03-05 15:21:54 +01:00
|
|
|
local prettify = require 'ldoc.prettify'
|
2012-12-31 13:52:00 +01:00
|
|
|
local doc = require 'ldoc.doc'
|
2013-08-25 21:01:30 +02:00
|
|
|
local unpack = utils.unpack
|
2011-09-11 19:17:05 +02:00
|
|
|
local html = {}
|
|
|
|
|
|
|
|
|
|
|
|
local quit = utils.quit
|
|
|
|
|
2012-08-05 05:09:14 +02:00
|
|
|
local function cleanup_whitespaces(text)
|
2012-12-31 13:52:00 +01:00
|
|
|
local lines = stringx.splitlines(text)
|
|
|
|
for i = 1, #lines do
|
|
|
|
lines[i] = stringx.rstrip(lines[i])
|
|
|
|
end
|
|
|
|
lines[#lines + 1] = "" -- Little trick: file should end with newline
|
|
|
|
return table.concat(lines, "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_module_info(m)
|
2014-01-07 16:48:50 +01:00
|
|
|
local info = OrderedMap()
|
2012-12-31 13:52:00 +01:00
|
|
|
for tag in doc.module_info_tags() do
|
|
|
|
local val = m.tags[tag]
|
|
|
|
if type(val)=='table' then
|
|
|
|
val = table.concat(val,',')
|
|
|
|
end
|
|
|
|
tag = stringx.title(tag)
|
2014-01-07 16:48:50 +01:00
|
|
|
info:set(tag,val)
|
2012-12-31 13:52:00 +01:00
|
|
|
end
|
2014-01-07 16:48:50 +01:00
|
|
|
if #info:keys() > 0 then
|
2012-12-31 13:52:00 +01:00
|
|
|
return info
|
|
|
|
end
|
2012-08-05 05:09:14 +02:00
|
|
|
end
|
|
|
|
|
2012-12-13 18:18:24 +01:00
|
|
|
local escape_table = { ["'"] = "'", ["\""] = """, ["<"] = "<", [">"] = ">", ["&"] = "&" }
|
|
|
|
|
2011-09-11 19:17:05 +02:00
|
|
|
function html.generate_output(ldoc, args, project)
|
|
|
|
local check_directory, check_file, writefile = tools.check_directory, tools.check_file, tools.writefile
|
2013-07-18 10:12:25 +02:00
|
|
|
local original_ldoc
|
|
|
|
|
2013-07-31 14:25:33 +02:00
|
|
|
local function save_and_set_ldoc (set)
|
|
|
|
if not set then return end
|
2013-07-18 10:12:25 +02:00
|
|
|
if not original_ldoc then
|
|
|
|
original_ldoc = tablex.copy(ldoc)
|
|
|
|
end
|
2013-07-31 14:25:33 +02:00
|
|
|
for s in set:iter() do
|
|
|
|
local var,val = s:match('([^=]+)=(.+)')
|
|
|
|
local num = tonumber(val)
|
|
|
|
if num then val = num
|
|
|
|
elseif val == 'true' then val = true
|
|
|
|
elseif val == 'false' then val = false
|
|
|
|
end
|
|
|
|
print('setting',var,val)
|
|
|
|
ldoc[var] = val
|
|
|
|
end
|
2013-07-18 10:12:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function restore_ldoc ()
|
|
|
|
if original_ldoc then
|
|
|
|
ldoc = original_ldoc
|
|
|
|
end
|
|
|
|
end
|
2011-09-11 19:17:05 +02:00
|
|
|
|
2012-12-13 18:18:24 +01:00
|
|
|
function ldoc.escape(str)
|
|
|
|
return (str:gsub("['&<>\"]", escape_table))
|
|
|
|
end
|
2011-09-11 19:17:05 +02:00
|
|
|
|
2013-03-05 15:21:54 +01:00
|
|
|
function ldoc.prettify(str)
|
2013-03-06 16:07:28 +01:00
|
|
|
return prettify.code('lua','usage',str,0,false)
|
2013-03-05 15:21:54 +01:00
|
|
|
end
|
|
|
|
|
2013-03-07 12:09:48 +01:00
|
|
|
-- Item descriptions come from combining the summary and description fields
|
|
|
|
function ldoc.descript(item)
|
|
|
|
return (item.summary or '?')..' '..(item.description or '')
|
|
|
|
end
|
|
|
|
|
2013-08-27 13:17:11 +02:00
|
|
|
function ldoc.module_name (mod)
|
|
|
|
local name = mod.name
|
2013-11-28 10:57:13 +01:00
|
|
|
if args.unqualified and (mod.type == 'module' or mod.type == 'classmod') then -- leave out package
|
2013-08-27 13:17:11 +02:00
|
|
|
name = name:gsub('^.-%.','')
|
2013-11-18 09:26:40 +01:00
|
|
|
elseif mod.type == 'topic' then
|
|
|
|
if mod.display_name then
|
|
|
|
name = mod.display_name
|
|
|
|
else -- leave out md extension
|
|
|
|
name = name:gsub('%..*$','')
|
|
|
|
end
|
2013-08-27 13:17:11 +02:00
|
|
|
end
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
|
2011-09-11 19:17:05 +02:00
|
|
|
-- this generates the internal module/function references
|
|
|
|
function ldoc.href(see)
|
|
|
|
if see.href then -- explict reference, e.g. to Lua manual
|
|
|
|
return see.href
|
2013-03-23 16:51:55 +01:00
|
|
|
elseif doc.Module:class_of(see) then
|
|
|
|
return ldoc.ref_to_module(see)
|
2011-09-11 19:17:05 +02:00
|
|
|
else
|
|
|
|
return ldoc.ref_to_module(see.mod)..'#'..see.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- this is either called from the 'root' (index or single module) or
|
|
|
|
-- from the 'modules' etc directories. If we are in one of those directories,
|
|
|
|
-- then linking to another kind is `../kind/name`; to the same kind is just `name`.
|
|
|
|
-- If we are in the root, then it is `kind/name`.
|
|
|
|
function ldoc.ref_to_module (mod)
|
|
|
|
local base = "" -- default: same directory
|
|
|
|
mod = mod or ldoc.module
|
|
|
|
local kind, module = mod.kind, ldoc.module
|
|
|
|
local name = mod.name -- default: name of module
|
|
|
|
if not ldoc.single then
|
|
|
|
if module then -- we are in kind/
|
|
|
|
if module.type ~= type then -- cross ref to ../kind/
|
|
|
|
base = "../"..kind.."/"
|
|
|
|
end
|
|
|
|
else -- we are in root: index
|
|
|
|
base = kind..'/'
|
|
|
|
end
|
|
|
|
else -- single module
|
|
|
|
if mod == ldoc.single then
|
|
|
|
name = ldoc.output
|
|
|
|
if not ldoc.root then base = '../' end
|
|
|
|
elseif ldoc.root then -- ref to other kinds (like examples)
|
|
|
|
base = kind..'/'
|
|
|
|
else
|
|
|
|
if module.type ~= type then -- cross ref to ../kind/
|
|
|
|
base = "../"..kind.."/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return base..name..'.html'
|
|
|
|
end
|
|
|
|
|
|
|
|
function ldoc.use_li(ls)
|
|
|
|
if #ls > 1 then return '<li>','</li>' else return '','' end
|
|
|
|
end
|
|
|
|
|
2013-12-09 11:13:06 +01:00
|
|
|
function ldoc.default_display_name(item)
|
2011-09-11 19:17:05 +02:00
|
|
|
local name = item.display_name or item.name
|
2013-08-20 15:26:49 +02:00
|
|
|
if item.type == 'function' or item.type == 'lfunction' then
|
|
|
|
if not ldoc.no_space_before_args then
|
|
|
|
name = name..' '
|
|
|
|
end
|
|
|
|
return name..item.args
|
|
|
|
else
|
|
|
|
return name
|
|
|
|
end
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
|
|
|
|
2013-12-09 11:13:06 +01:00
|
|
|
function ldoc.display_name(item)
|
|
|
|
if ldoc.custom_display_name_handler then
|
|
|
|
return ldoc.custom_display_name_handler(item, ldoc.default_display_name)
|
|
|
|
else
|
|
|
|
return ldoc.default_display_name(item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-28 12:06:42 +01:00
|
|
|
function ldoc.no_spaces(s)
|
|
|
|
s = s:gsub('%s*$','')
|
|
|
|
return (s:gsub('%W','_'))
|
|
|
|
end
|
2011-09-11 19:17:05 +02:00
|
|
|
|
2013-08-07 15:19:46 +02:00
|
|
|
function ldoc.module_typename(m)
|
|
|
|
return doc.presentation_name(m.type)
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
|
|
|
|
2013-03-18 14:51:28 +01:00
|
|
|
function ldoc.is_list (t)
|
|
|
|
return type(t) == 'table' and t.append
|
|
|
|
end
|
|
|
|
|
2011-12-06 18:19:09 +01:00
|
|
|
function ldoc.typename (tp)
|
2013-08-27 12:47:47 +02:00
|
|
|
if not tp or tp == '' or tp:match '^@' then return '' end
|
2012-10-29 01:24:49 +01:00
|
|
|
local optional
|
2012-12-31 13:52:00 +01:00
|
|
|
-- ?<type> is short for ?nil|<type>
|
|
|
|
if tp:match("^%?") and not tp:match '|' then
|
|
|
|
tp = '?|'..tp:sub(2)
|
|
|
|
end
|
2012-10-29 01:24:49 +01:00
|
|
|
local tp2 = tp:match("%?|?(.*)")
|
|
|
|
if tp2 then
|
|
|
|
optional = true
|
|
|
|
tp = tp2
|
|
|
|
end
|
2013-09-26 15:34:53 +02:00
|
|
|
|
2012-10-29 01:24:49 +01:00
|
|
|
local types = {}
|
|
|
|
for name in tp:gmatch("[^|]+") do
|
2013-09-26 16:05:45 +02:00
|
|
|
local sym = name:match '([%w%.%:]+)'
|
|
|
|
local ref,err = markup.process_reference(sym,true)
|
2011-12-06 18:19:09 +01:00
|
|
|
if ref then
|
2013-09-26 16:05:45 +02:00
|
|
|
if ref.label and sym == name then
|
|
|
|
name = ref.label
|
|
|
|
end
|
|
|
|
types[#types+1] = ('<a class="type" href="%s">%s</a>'):format(ldoc.href(ref),name)
|
2011-12-06 18:19:09 +01:00
|
|
|
else
|
2012-10-29 01:26:07 +01:00
|
|
|
types[#types+1] = '<span class="type">'..name..'</span>'
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
2012-10-29 01:24:49 +01:00
|
|
|
end
|
|
|
|
local names = table.concat(types, ", ", 1, math.max(#types-1, 1))
|
|
|
|
if #types > 1 then names = names.." or "..types[#types] end
|
|
|
|
if optional then
|
2012-12-31 13:52:00 +01:00
|
|
|
if names ~= '' then
|
|
|
|
if #types == 1 then names = "optional "..names end
|
|
|
|
else
|
|
|
|
names = "optional"
|
2012-10-29 01:24:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return names
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
|
|
|
|
2013-03-27 14:50:37 +01:00
|
|
|
local function set_charset (ldoc,m)
|
|
|
|
m = m or ldoc.module
|
2013-04-03 12:20:54 +02:00
|
|
|
ldoc.doc_charset = (m and m.tags.charset) or ldoc.charset
|
2013-03-27 14:50:37 +01:00
|
|
|
end
|
|
|
|
|
2011-09-11 19:17:05 +02:00
|
|
|
local module_template,err = utils.readfile (path.join(args.template,ldoc.templ))
|
|
|
|
if not module_template then
|
|
|
|
quit("template not found at '"..args.template.."' Use -l to specify directory containing ldoc.ltp")
|
|
|
|
end
|
|
|
|
|
|
|
|
local css = ldoc.css
|
|
|
|
ldoc.output = args.output
|
2011-12-06 18:19:09 +01:00
|
|
|
ldoc.ipairs = ipairs
|
2012-12-31 13:52:00 +01:00
|
|
|
ldoc.pairs = pairs
|
2013-03-18 14:51:28 +01:00
|
|
|
ldoc.print = print
|
2011-09-11 19:17:05 +02:00
|
|
|
|
2013-08-01 12:14:57 +02:00
|
|
|
-- Bang out the index.
|
2011-09-11 19:17:05 +02:00
|
|
|
-- in single mode there is one module and the 'index' is the
|
|
|
|
-- documentation for that module.
|
|
|
|
ldoc.module = ldoc.single
|
|
|
|
if ldoc.single and args.one then
|
|
|
|
ldoc.kinds_allowed = {module = true, topic = true}
|
2013-03-28 12:28:05 +01:00
|
|
|
ldoc.one = true
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
|
|
|
ldoc.root = true
|
2013-01-02 12:04:14 +01:00
|
|
|
if ldoc.module then
|
|
|
|
ldoc.module.info = get_module_info(ldoc.module)
|
2013-07-31 14:25:33 +02:00
|
|
|
ldoc.module.ldoc = ldoc
|
|
|
|
save_and_set_ldoc(ldoc.module.tags.set)
|
2013-01-02 12:04:14 +01:00
|
|
|
end
|
2013-03-27 14:50:37 +01:00
|
|
|
set_charset(ldoc)
|
2011-09-11 19:17:05 +02:00
|
|
|
local out,err = template.substitute(module_template,{
|
|
|
|
ldoc = ldoc,
|
2012-12-31 13:52:00 +01:00
|
|
|
module = ldoc.module,
|
2013-08-05 19:27:42 +02:00
|
|
|
_escape = ldoc.template_escape
|
2011-09-11 19:17:05 +02:00
|
|
|
})
|
|
|
|
ldoc.root = false
|
|
|
|
if not out then quit("template failed: "..err) end
|
2013-07-31 14:25:33 +02:00
|
|
|
restore_ldoc()
|
2011-09-11 19:17:05 +02:00
|
|
|
|
|
|
|
check_directory(args.dir) -- make sure output directory is ok
|
|
|
|
|
|
|
|
args.dir = args.dir .. path.sep
|
|
|
|
|
2013-08-05 19:27:42 +02:00
|
|
|
if css then -- has CSS been copied?
|
2013-08-01 12:14:57 +02:00
|
|
|
check_file(args.dir..css, path.join(args.style,css))
|
|
|
|
end
|
2011-09-11 19:17:05 +02:00
|
|
|
|
|
|
|
-- write out the module index
|
2012-08-05 05:09:14 +02:00
|
|
|
out = cleanup_whitespaces(out)
|
2011-09-11 19:17:05 +02:00
|
|
|
writefile(args.dir..args.output..args.ext,out)
|
|
|
|
|
|
|
|
-- in single mode, we exclude any modules since the module has been done;
|
2013-08-01 12:14:57 +02:00
|
|
|
-- ext step is then only for putting out any examples or topics
|
2012-03-15 14:06:30 +01:00
|
|
|
local mods = List()
|
2011-09-11 19:17:05 +02:00
|
|
|
for kind, modules in project() do
|
2012-03-15 14:06:30 +01:00
|
|
|
local lkind = kind:lower()
|
|
|
|
if not ldoc.single or ldoc.single and lkind ~= 'modules' then
|
|
|
|
mods:append {kind, lkind, modules}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- write out the per-module documentation
|
|
|
|
-- note that we reset the internal ordering of the 'kinds' so that
|
|
|
|
-- e.g. when reading a topic the other Topics will be listed first.
|
2013-08-05 19:27:42 +02:00
|
|
|
if css then
|
|
|
|
ldoc.css = '../'..css
|
|
|
|
end
|
2012-03-15 14:06:30 +01:00
|
|
|
for m in mods:iter() do
|
|
|
|
local kind, lkind, modules = unpack(m)
|
|
|
|
check_directory(args.dir..lkind)
|
|
|
|
project:put_kind_first(kind)
|
|
|
|
for m in modules() do
|
|
|
|
ldoc.module = m
|
|
|
|
ldoc.body = m.body
|
2013-07-31 14:25:33 +02:00
|
|
|
m.ldoc = ldoc
|
2013-07-18 10:12:25 +02:00
|
|
|
if m.tags.set then
|
2013-07-31 14:25:33 +02:00
|
|
|
save_and_set_ldoc(m.tags.set)
|
2013-07-18 10:12:25 +02:00
|
|
|
end
|
2013-03-27 14:50:37 +01:00
|
|
|
set_charset(ldoc)
|
2012-12-31 13:52:00 +01:00
|
|
|
m.info = get_module_info(m)
|
2012-03-15 14:06:30 +01:00
|
|
|
if ldoc.body and m.postprocess then
|
|
|
|
ldoc.body = m.postprocess(ldoc.body)
|
|
|
|
end
|
|
|
|
out,err = template.substitute(module_template,{
|
|
|
|
module=m,
|
2013-08-05 19:27:42 +02:00
|
|
|
ldoc = ldoc,
|
|
|
|
_escape = ldoc.template_escape
|
2012-03-15 14:06:30 +01:00
|
|
|
})
|
|
|
|
if not out then
|
|
|
|
quit('template failed for '..m.name..': '..err)
|
|
|
|
else
|
2012-08-05 05:09:14 +02:00
|
|
|
out = cleanup_whitespaces(out)
|
2012-03-15 14:06:30 +01:00
|
|
|
writefile(args.dir..lkind..'/'..m.name..args.ext,out)
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
2013-07-18 10:12:25 +02:00
|
|
|
restore_ldoc()
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
|
|
|
end
|
2011-12-12 12:15:04 +01:00
|
|
|
if not args.quiet then print('output written to '..tools.abspath(args.dir)) end
|
2011-09-11 19:17:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return html
|
2012-03-15 14:06:30 +01:00
|
|
|
|