2023-10-25 04:41:53 +00:00
|
|
|
local ansikit = require 'ansikit'
|
2022-04-23 03:56:57 +00:00
|
|
|
local commander = require 'commander'
|
|
|
|
local fs = require 'fs'
|
|
|
|
local lunacolors = require 'lunacolors'
|
2023-10-25 04:41:53 +00:00
|
|
|
local Greenhouse = require 'nature.greenhouse'
|
|
|
|
local Page = require 'nature.greenhouse.page'
|
2023-12-26 03:08:29 +00:00
|
|
|
local docfuncs = require 'nature.doc'
|
|
|
|
|
|
|
|
local function strip(text, ...)
|
|
|
|
for _, pat in ipairs {...} do
|
|
|
|
text = text:gsub(pat, '\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
local function transformHTMLandMD(text)
|
|
|
|
return strip(text, '|||', '|%-%-%-%-|%-%-%-%-|')
|
|
|
|
:gsub('|(.-)|(.-)|', function(entry1, entry2)
|
|
|
|
return string.format('%s - %s', entry1, entry2)
|
|
|
|
end)
|
|
|
|
:gsub('<hr>', '{separator}')
|
|
|
|
:gsub('<.->', '')
|
|
|
|
--:gsub('^\n\n', '\n')
|
|
|
|
:gsub('\n%s+\n', '\n\n')
|
|
|
|
--:gsub(' \n', '\n\n')
|
|
|
|
:gsub('{{< (%w+) `(.-)` >}}', function(shortcode, text)
|
|
|
|
return docfuncs.renderInfoBlock(shortcode, text)
|
|
|
|
end)
|
|
|
|
:gsub('```(%w+)\n(.-)```', function(lang, text)
|
|
|
|
return docfuncs.renderCodeBlock(text)
|
|
|
|
end)
|
|
|
|
:gsub('```\n(.-)\n```', function(text)
|
|
|
|
return docfuncs.renderCodeBlock(text)
|
|
|
|
end)
|
|
|
|
:gsub('`[^\n].-`', lunacolors.cyan)
|
|
|
|
:gsub('#+ (.-\n)', function(heading) return lunacolors.blue(lunacolors.bold('→ ' .. heading)) end)
|
|
|
|
:gsub('%*%*(.-)%*%*', lunacolors.bold)
|
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
|
2023-01-20 23:07:42 +00:00
|
|
|
commander.register('doc', function(args, sinks)
|
2022-04-23 03:56:57 +00:00
|
|
|
local moddocPath = hilbish.dataDir .. '/docs/'
|
2023-02-05 17:02:50 +00:00
|
|
|
local stat = pcall(fs.stat, '.git/refs/heads/extended-job-api')
|
2023-01-07 18:01:57 +00:00
|
|
|
if stat then
|
|
|
|
-- hilbish git
|
|
|
|
moddocPath = './docs/'
|
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
|
2023-01-18 10:39:26 +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, ', ')
|
2023-10-25 04:41:53 +00:00
|
|
|
local f
|
|
|
|
local function handleYamlInfo(d)
|
|
|
|
local vals = {}
|
|
|
|
local docs = d
|
|
|
|
|
2023-12-26 03:08:29 +00:00
|
|
|
local valsStr = docs:match '^%-%-%-\n.-\n%-%-%-'
|
2023-10-25 04:41:53 +00:00
|
|
|
if valsStr then
|
2023-12-26 03:08:29 +00:00
|
|
|
docs = docs:sub(valsStr:len() + 2, #docs)
|
|
|
|
local pre = docs:sub(1, 1)
|
|
|
|
if pre == '\n' then
|
|
|
|
docs = docs:sub(2)
|
|
|
|
end
|
2023-10-25 04:41:53 +00:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2022-04-23 03:56:57 +00:00
|
|
|
if #args > 0 then
|
|
|
|
local mod = args[1]
|
|
|
|
|
2023-10-25 04:41:53 +00:00
|
|
|
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
|
|
|
|
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-10-25 04:41:53 +00:00
|
|
|
local oldmoddocPath = moddocPath
|
2023-01-07 17:54:08 +00:00
|
|
|
if not f then
|
2023-10-25 04:41:53 +00:00
|
|
|
moddocPath = moddocPath .. subdocName:match '%w+' .. '/'
|
|
|
|
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
|
2023-01-07 17:54:08 +00:00
|
|
|
end
|
2022-12-20 05:38:19 +00:00
|
|
|
if not f then
|
2023-10-25 04:41:53 +00:00
|
|
|
moddocPath = oldmoddocPath .. 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
|
2023-01-20 23:07:42 +00:00
|
|
|
sinks.out:writeln('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
|
|
|
|
2023-10-25 04:41:53 +00:00
|
|
|
end
|
2022-12-15 04:00:54 +00:00
|
|
|
|
2023-10-25 04:41:53 +00:00
|
|
|
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)
|
2022-12-15 04:00:54 +00:00
|
|
|
|
2023-10-25 04:41:53 +00:00
|
|
|
local gh = Greenhouse(sinks.out)
|
|
|
|
function gh:resize()
|
|
|
|
local size = terminal.size()
|
|
|
|
self.region = {
|
|
|
|
width = size.width,
|
2023-12-26 03:08:29 +00:00
|
|
|
height = size.height - 1
|
2023-10-25 04:41:53 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
gh:resize()
|
|
|
|
|
|
|
|
function gh:render()
|
|
|
|
local workingPage = self.pages[self.curPage]
|
|
|
|
local offset = self.offset
|
|
|
|
if self.isSpecial then
|
|
|
|
offset = self.specialOffset
|
|
|
|
workingPage = self.specialPage
|
2022-12-15 04:00:54 +00:00
|
|
|
end
|
2023-12-26 03:08:29 +00:00
|
|
|
local size = terminal.size()
|
2023-10-25 04:41:53 +00:00
|
|
|
|
2023-12-26 03:08:29 +00:00
|
|
|
self.sink:write(ansikit.getCSI(size.height - 1 .. ';1', 'H'))
|
|
|
|
self.sink:write(ansikit.getCSI(0, 'J'))
|
2023-10-25 04:41:53 +00:00
|
|
|
if not self.isSpecial then
|
|
|
|
if args[1] == 'api' then
|
2023-12-26 03:08:29 +00:00
|
|
|
self.sink:writeln(workingPage.title)
|
2023-10-25 04:41:53 +00:00
|
|
|
self.sink:write(lunacolors.format(string.format('{grayBg} ↳ {white}{italic}%s {reset}', workingPage.description or 'No description.')))
|
|
|
|
else
|
|
|
|
self.sink:write(lunacolors.reset(string.format('Viewing doc page %s', moddocPath)))
|
|
|
|
end
|
2022-12-15 04:00:54 +00:00
|
|
|
end
|
2022-04-23 03:56:57 +00:00
|
|
|
end
|
2023-01-18 10:39:26 +00:00
|
|
|
local backtickOccurence = 0
|
2023-10-25 04:41:53 +00:00
|
|
|
local function formatDocText(d)
|
2023-12-26 03:08:29 +00:00
|
|
|
return transformHTMLandMD(d)
|
|
|
|
--[[
|
|
|
|
return lunacolors.format(d:gsub('`(.-)`', function(t)
|
|
|
|
return docfuncs.renderCodeBlock(t)
|
2023-10-25 04:41:53 +00:00
|
|
|
end):gsub('\n#+.-\n', function(t)
|
|
|
|
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
|
|
|
|
return '{bold}{yellow}' .. signature .. '{reset}'
|
|
|
|
end))
|
2023-12-26 03:08:29 +00:00
|
|
|
]]--
|
2023-10-25 04:41:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2023-12-26 03:08:29 +00:00
|
|
|
local doc, vals = handleYamlInfo(#args == 0 and doc or formatDocText(f:read '*a'))
|
2023-10-25 04:41:53 +00:00
|
|
|
if #moddocs ~= 0 and f 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)
|
|
|
|
|
|
|
|
-- add subdoc pages
|
|
|
|
for _, sdName in ipairs(moddocs) do
|
|
|
|
local sdFile = fs.join(sdName, '_index.md')
|
|
|
|
if sdName:match '.md$' then
|
|
|
|
sdFile = sdName
|
2023-01-18 10:39:26 +00:00
|
|
|
end
|
2023-10-25 04:41:53 +00:00
|
|
|
|
|
|
|
local f = io.open(moddocPath .. sdFile, 'rb')
|
2023-12-26 03:08:29 +00:00
|
|
|
local doc, vals = handleYamlInfo(formatDocText(f:read '*a'))
|
|
|
|
local page = Page(vals.title or sdName, doc)
|
2023-10-25 04:41:53 +00:00
|
|
|
page.description = vals.description
|
|
|
|
gh:addPage(page)
|
|
|
|
end
|
|
|
|
ansikit.hideCursor()
|
|
|
|
gh:initUi()
|
2022-04-23 03:56:57 +00:00
|
|
|
end)
|