fix(commands/doc): fix text cutoff, render warning shortcode

pull/260/head
sammyette 2023-12-18 13:09:09 -04:00
parent a59a0291b8
commit fb3915ea26
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 36 additions and 10 deletions

View File

@ -20,15 +20,22 @@ local function transformHTMLandMD(text)
return string.format('%s - %s', entry1, entry2) return string.format('%s - %s', entry1, entry2)
end) end)
:gsub('^\n\n', '\n') :gsub('^\n\n', '\n')
:gsub('<hr>', '{separator}')
:gsub('<.->', '')
:gsub('\n%s+\n', '\n\n') :gsub('\n%s+\n', '\n\n')
:gsub('#+ (.-\n)', function(heading) return lunacolors.blue(lunacolors.bold('' .. heading)) end) :gsub(' \n', '\n\n')
:gsub('{{< (%w+) `(.-)` >}}', function(shortcode, text)
return docfuncs.renderInfoBlock(shortcode, text)
end)
:gsub('```(%w+)\n(.-)```', function(lang, text) :gsub('```(%w+)\n(.-)```', function(lang, text)
return docfuncs.renderCodeBlock(text) return docfuncs.renderCodeBlock(text)
end) end)
:gsub('`(.-)`', lunacolors.cyan) :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) :gsub('%*%*(.-)%*%*', lunacolors.bold)
:gsub('<hr>', '{separator}')
:gsub('<.->', '')
end end
commander.register('doc', function(args, sinks) commander.register('doc', function(args, sinks)
@ -54,10 +61,9 @@ Available sections: ]] .. table.concat(modules, ', ')
local vals = {} local vals = {}
local docs = d local docs = d
local valsStr = docs:match '%-%-%-\n([^%-%-%-]+)\n' local valsStr = docs:match '^%-%-%-\n.-\n%-%-%-'
print(valsStr)
if valsStr then if valsStr then
docs = docs:sub(valsStr:len() + 10, #docs) docs = docs:sub(valsStr:len() + 2, #docs)
-- parse vals -- parse vals
local lines = string.split(valsStr, '\n') local lines = string.split(valsStr, '\n')
@ -154,7 +160,7 @@ Available sections: ]] .. table.concat(modules, ', ')
end end
local doc, vals = handleYamlInfo(#args == 0 and doc or formatDocText(f:read '*a':gsub('-([%d]+)', '%1'))) local doc, vals = handleYamlInfo(#args == 0 and doc or formatDocText(f:read '*a'))
if #moddocs ~= 0 and f then if #moddocs ~= 0 and f then
doc = doc .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n' doc = doc .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end end
@ -172,8 +178,8 @@ Available sections: ]] .. table.concat(modules, ', ')
end end
local f = io.open(moddocPath .. sdFile, 'rb') local f = io.open(moddocPath .. sdFile, 'rb')
local doc, vals = handleYamlInfo(f:read '*a':gsub('-([%d]+)', '%1')) local doc, vals = handleYamlInfo(formatDocText(f:read '*a'))
local page = Page(vals.title, formatDocText(doc)) local page = Page(vals.title or sdName, doc)
page.description = vals.description page.description = vals.description
gh:addPage(page) gh:addPage(page)
end end

View File

@ -24,4 +24,24 @@ function M.renderCodeBlock(text)
return '\n' .. lunacolors.format('{greyBg}' .. table.concat(lines, '\n')) .. '\n' return '\n' .. lunacolors.format('{greyBg}' .. table.concat(lines, '\n')) .. '\n'
end end
function M.renderInfoBlock(type, text)
local longest = 0
local lines = string.split(text:gsub('\t', ' '), '\n')
for i, line in ipairs(lines) do
local len = line:len()
if len > longest then longest = len end
end
for i, line in ipairs(lines) do
lines[i] = ' ' .. M.highlight(line:sub(0, longest))
.. string.rep(' ', longest - line:len()) .. ' '
end
local heading
if type == 'warning' then
heading = lunacolors.yellowBg(lunacolors.black(' ⚠ Warning '))
end
return '\n' .. heading .. '\n' .. lunacolors.format('{greyBg}' .. table.concat(lines, '\n')) .. '\n'
end
return M return M