feat(commands/doc): add subdocs as additional pages

pull/240/head
sammyette 2023-10-24 23:51:42 -04:00
parent 554fb009f8
commit 7288f85e9a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 88 additions and 41 deletions

View File

@ -23,12 +23,36 @@ to Hilbish.
Usage: doc <section> [subdoc] Usage: doc <section> [subdoc]
Available sections: ]] .. table.concat(modules, ', ') Available sections: ]] .. table.concat(modules, ', ')
local vals = {} local f
local function handleYamlInfo(d)
local vals = {}
local docs = d
local valsStr = docs:match '%-%-%-\n([^%-%-%-]+)\n'
print(valsStr)
if valsStr then
docs = docs:sub(valsStr:len() + 10, #docs)
-- parse vals
local lines = string.split(valsStr, '\n')
for _, line in ipairs(lines) do
local key = line:match '(%w+): '
local val = line:match '^%w+: (.-)$'
if key then
vals[key] = val
end
end
end
--docs = docs:sub(1, #docs - 1)
return docs, vals
end
if #args > 0 then if #args > 0 then
local mod = args[1] local mod = args[1]
local f = io.open(moddocPath .. mod .. '.md', 'rb') f = io.open(moddocPath .. mod .. '.md', 'rb')
local funcdocs = nil local funcdocs = nil
local subdocName = args[2] local subdocName = args[2]
if not f then if not f then
@ -52,35 +76,14 @@ Available sections: ]] .. table.concat(modules, ', ')
return 1 return 1
end end
end end
funcdocs = f:read '*a':gsub('-([%d]+)', '%1')
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' and f ~= 'index.md' end)
local subdocs = table.map(moddocs, function(fname)
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
end)
if #moddocs ~= 0 then
funcdocs = funcdocs .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end
local valsStr = funcdocs:match '%-%-%-\n([^%-%-%-]+)\n'
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+: (.-)$'
if key then
vals[key] = val
end
end
end
doc = funcdocs:sub(1, #funcdocs - 1)
f:close()
end end
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' and f ~= 'index.md' end)
local subdocs = table.map(moddocs, function(fname)
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
end)
local gh = Greenhouse(sinks.out) local gh = Greenhouse(sinks.out)
function gh:resize() function gh:resize()
local size = terminal.size() local size = terminal.size()
@ -102,26 +105,52 @@ Available sections: ]] .. table.concat(modules, ', ')
self.sink:write(ansikit.getCSI(self.region.height + 2 .. ';1', 'H')) self.sink:write(ansikit.getCSI(self.region.height + 2 .. ';1', 'H'))
if not self.isSpecial then if not self.isSpecial then
if args[1] == 'api' then if args[1] == 'api' then
self.sink:writeln(lunacolors.reset(string.format('%s', vals.title))) self.sink:writeln(lunacolors.reset(string.format('%s', workingPage.title)))
self.sink:write(lunacolors.format(string.format('{grayBg} ↳ {white}{italic}%s {reset}', vals.description or 'No description.'))) self.sink:write(lunacolors.format(string.format('{grayBg} ↳ {white}{italic}%s {reset}', workingPage.description or 'No description.')))
else else
self.sink:write(lunacolors.reset(string.format('Viewing doc page %s', moddocPath))) self.sink:write(lunacolors.reset(string.format('Viewing doc page %s', moddocPath)))
end end
end end
end end
local backtickOccurence = 0 local backtickOccurence = 0
local page = Page(nil, lunacolors.format(doc:gsub('`', function() local function formatDocText(d)
backtickOccurence = backtickOccurence + 1 return lunacolors.format(d:gsub('`', function()
if backtickOccurence % 2 == 0 then backtickOccurence = backtickOccurence + 1
return '{reset}' if backtickOccurence % 2 == 0 then
else return '{reset}'
return '{underline}{green}' else
end return '{underline}{green}'
end):gsub('\n#+.-\n', function(t) end
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<') end):gsub('\n#+.-\n', function(t)
return '{bold}{yellow}' .. signature .. '{reset}' local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
end))) return '{bold}{yellow}' .. signature .. '{reset}'
end))
end
local doc, vals = handleYamlInfo(#args == 0 and doc or formatDocText(f:read '*a':gsub('-([%d]+)', '%1')))
if #moddocs ~= 0 then
doc = doc .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end
if f then f:close() end
local page = Page(vals.title, doc)
page.description = vals.description
gh:addPage(page) gh:addPage(page)
-- add subdoc pages
for _, sdName in ipairs(moddocs) do
local sdFile = fs.join(sdName, '_index.md')
if sdName:match '.md$' then
sdFile = sdName
end
local f = io.open(moddocPath .. sdFile, 'rb')
local doc, vals = handleYamlInfo(f:read '*a':gsub('-([%d]+)', '%1'))
local page = Page(vals.title, formatDocText(doc))
page.description = vals.description
gh:addPage(page)
end
ansikit.hideCursor() ansikit.hideCursor()
gh:initUi() gh:initUi()
end) end)

View File

@ -74,6 +74,10 @@ function Greenhouse:draw()
workingPage = self.specialPage workingPage = self.specialPage
end end
if workingPage.lazy and not workingPage.loaded then
workingPage.initialize()
end
local lines = workingPage.lines local lines = workingPage.lines
self.sink:write(ansikit.getCSI(self.start .. ';1', 'H')) self.sink:write(ansikit.getCSI(self.start .. ';1', 'H'))
self.sink:write(ansikit.getCSI(2, 'J')) self.sink:write(ansikit.getCSI(2, 'J'))

View File

@ -5,6 +5,9 @@ local Page = Object:extend()
function Page:new(title, text) function Page:new(title, text)
self:setText(text) self:setText(text)
self.title = title or 'Page' self.title = title or 'Page'
self.lazy = false
self.loaded = true
self.children = {}
end end
function Page:setText(text) function Page:setText(text)
@ -15,4 +18,15 @@ function Page:setTitle(title)
self.title = title self.title = title
end end
function Page:dynamic(initializer)
self.initializer = initializer
self.lazy = true
self.loaded = false
end
function Page:initialize()
self.initializer()
self.loaded = true
end
return Page return Page