2022-04-23 03:56:57 +00:00
|
|
|
local commander = require 'commander'
|
|
|
|
local fs = require 'fs'
|
|
|
|
local lunacolors = require 'lunacolors'
|
|
|
|
|
|
|
|
commander.register('doc', function(args)
|
|
|
|
local moddocPath = hilbish.dataDir .. '/docs/'
|
2023-01-07 18:01:57 +00:00
|
|
|
local stat = fs.stat '.git/refs/heads/extended-job-api'
|
|
|
|
if stat then
|
|
|
|
-- hilbish git
|
|
|
|
moddocPath = './docs/'
|
|
|
|
end
|
2022-12-15 04:00:54 +00:00
|
|
|
local apidocHeader = [[
|
|
|
|
# %s
|
|
|
|
{grayBg} {white}{italic}%s {reset}
|
|
|
|
|
2022-04-23 03:56:57 +00:00
|
|
|
]]
|
|
|
|
|
2023-01-07 18:10:15 +00:00
|
|
|
local modules = table.map(fs.readdir(moddocPath), function(f)
|
|
|
|
return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', '')))
|
|
|
|
end)
|
|
|
|
local doc = [[
|
|
|
|
Welcome to Hilbish's documentation viewer! Here you can find
|
|
|
|
documentation for builtin functions and other things related
|
|
|
|
to Hilbish.
|
|
|
|
|
|
|
|
Usage: doc <section> [subdoc]
|
|
|
|
Available sections: ]] .. table.concat(modules, ', ')
|
2022-04-23 03:56:57 +00:00
|
|
|
if #args > 0 then
|
|
|
|
local mod = args[1]
|
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
local f = io.open(moddocPath .. mod .. '.md', 'rb')
|
2022-04-23 03:56:57 +00:00
|
|
|
local funcdocs = nil
|
2022-12-20 05:38:19 +00:00
|
|
|
local subdocName = args[2]
|
2022-04-23 03:56:57 +00:00
|
|
|
if not f then
|
|
|
|
-- assume subdir
|
2022-12-15 04:00:54 +00:00
|
|
|
-- dataDir/docs/<mod>/<mod>.md
|
2022-04-23 03:56:57 +00:00
|
|
|
moddocPath = moddocPath .. mod .. '/'
|
|
|
|
if not subdocName then
|
2022-12-20 05:38:19 +00:00
|
|
|
subdocName = '_index'
|
2022-04-23 03:56:57 +00:00
|
|
|
end
|
2022-12-15 04:00:54 +00:00
|
|
|
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
|
2023-01-07 17:54:08 +00:00
|
|
|
if not f then
|
|
|
|
f = io.open(moddocPath .. subdocName:match '%w+' .. '/' .. subdocName .. '.md', 'rb')
|
|
|
|
end
|
2022-12-20 05:38:19 +00:00
|
|
|
if not f then
|
|
|
|
moddocPath = moddocPath .. subdocName .. '/'
|
2022-12-20 05:42:58 +00:00
|
|
|
subdocName = args[3] or '_index'
|
|
|
|
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
|
2022-12-20 05:38:19 +00:00
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
if not f then
|
|
|
|
print('No documentation found for ' .. mod .. '.')
|
2023-01-07 18:02:21 +00:00
|
|
|
return 1
|
2022-04-23 03:56:57 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-20 05:38:19 +00:00
|
|
|
funcdocs = f:read '*a':gsub('-([%d]+)', '%1')
|
2023-01-07 18:16:46 +00:00
|
|
|
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' or f ~= 'index.md' end)
|
2022-12-20 05:38:19 +00:00
|
|
|
local subdocs = table.map(moddocs, function(fname)
|
|
|
|
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
|
|
|
|
end)
|
|
|
|
if subdocName == '_index' then
|
|
|
|
funcdocs = funcdocs .. '\nSubdocs: ' .. table.concat(subdocs, ', ')
|
2022-04-23 03:56:57 +00:00
|
|
|
end
|
2022-12-20 05:38:19 +00:00
|
|
|
|
2022-12-15 04:00:54 +00:00
|
|
|
local valsStr = funcdocs:match '%-%-%-\n([^%-%-%-]+)\n'
|
|
|
|
local vals = {}
|
|
|
|
if valsStr then
|
|
|
|
local _, endpos = funcdocs:find('---\n' .. valsStr .. '\n---\n\n', 1, true)
|
|
|
|
funcdocs = funcdocs:sub(endpos + 1, #funcdocs)
|
|
|
|
|
|
|
|
-- parse vals
|
|
|
|
local lines = string.split(valsStr, '\n')
|
|
|
|
for _, line in ipairs(lines) do
|
|
|
|
local key = line:match '(%w+): '
|
|
|
|
local val = line:match '^%w+: (.-)$'
|
|
|
|
|
2022-12-20 05:38:19 +00:00
|
|
|
if key then
|
|
|
|
vals[key] = val
|
|
|
|
end
|
2022-12-15 04:00:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if mod == 'api' then
|
2022-12-20 05:38:19 +00:00
|
|
|
funcdocs = string.format(apidocHeader, vals.title, vals.description or 'no description.') .. funcdocs
|
2022-12-15 04:00:54 +00:00
|
|
|
end
|
2023-01-07 18:10:15 +00:00
|
|
|
doc = funcdocs:sub(1, #funcdocs - 1)
|
2022-04-23 03:56:57 +00:00
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
2023-01-07 18:10:15 +00:00
|
|
|
local backtickOccurence = 0
|
|
|
|
print(lunacolors.format(doc:gsub('`', function()
|
|
|
|
backtickOccurence = backtickOccurence + 1
|
|
|
|
if backtickOccurence % 2 == 0 then
|
|
|
|
return '{reset}'
|
|
|
|
else
|
|
|
|
return '{underline}{green}'
|
|
|
|
end
|
|
|
|
end):gsub('#+.-\n', function(t)
|
|
|
|
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
|
|
|
|
return '{bold}{yellow}' .. signature .. '{reset}'
|
|
|
|
end)))
|
2022-04-23 03:56:57 +00:00
|
|
|
end)
|