mirror of https://github.com/Hilbis/Hilbish
feat(commands/doc): add subdocs as additional pages
parent
554fb009f8
commit
7288f85e9a
|
@ -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 f
|
||||||
|
local function handleYamlInfo(d)
|
||||||
local vals = {}
|
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,34 +76,13 @@ Available sections: ]] .. table.concat(modules, ', ')
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
funcdocs = f:read '*a':gsub('-([%d]+)', '%1')
|
|
||||||
|
end
|
||||||
|
|
||||||
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' and f ~= 'index.md' 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)
|
local subdocs = table.map(moddocs, function(fname)
|
||||||
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
|
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
|
||||||
end)
|
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
|
|
||||||
|
|
||||||
local gh = Greenhouse(sinks.out)
|
local gh = Greenhouse(sinks.out)
|
||||||
function gh:resize()
|
function gh:resize()
|
||||||
|
@ -102,15 +105,16 @@ 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)
|
||||||
|
return lunacolors.format(d:gsub('`', function()
|
||||||
backtickOccurence = backtickOccurence + 1
|
backtickOccurence = backtickOccurence + 1
|
||||||
if backtickOccurence % 2 == 0 then
|
if backtickOccurence % 2 == 0 then
|
||||||
return '{reset}'
|
return '{reset}'
|
||||||
|
@ -120,8 +124,33 @@ Available sections: ]] .. table.concat(modules, ', ')
|
||||||
end):gsub('\n#+.-\n', function(t)
|
end):gsub('\n#+.-\n', function(t)
|
||||||
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
|
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
|
||||||
return '{bold}{yellow}' .. signature .. '{reset}'
|
return '{bold}{yellow}' .. signature .. '{reset}'
|
||||||
end)))
|
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)
|
||||||
|
|
|
@ -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'))
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue