Skip to content

Commit

Permalink
fix(commands/doc): fix text cutoff, render warning shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Dec 18, 2023
1 parent a59a029 commit fb3915e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
26 changes: 16 additions & 10 deletions nature/commands/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ local function transformHTMLandMD(text)
return string.format('%s - %s', entry1, entry2)
end)
:gsub('^\n\n', '\n')
:gsub('<hr>', '{separator}')
:gsub('<.->', '')
: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)
return docfuncs.renderCodeBlock(text)
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('<hr>', '{separator}')
:gsub('<.->', '')
end

commander.register('doc', function(args, sinks)
Expand All @@ -54,10 +61,9 @@ Available sections: ]] .. table.concat(modules, ', ')
local vals = {}
local docs = d

local valsStr = docs:match '%-%-%-\n([^%-%-%-]+)\n'
print(valsStr)
local valsStr = docs:match '^%-%-%-\n.-\n%-%-%-'
if valsStr then
docs = docs:sub(valsStr:len() + 10, #docs)
docs = docs:sub(valsStr:len() + 2, #docs)

-- parse vals
local lines = string.split(valsStr, '\n')
Expand Down Expand Up @@ -154,7 +160,7 @@ Available sections: ]] .. table.concat(modules, ', ')
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
doc = doc .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end
Expand All @@ -172,8 +178,8 @@ Available sections: ]] .. table.concat(modules, ', ')
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))
local doc, vals = handleYamlInfo(formatDocText(f:read '*a'))
local page = Page(vals.title or sdName, doc)
page.description = vals.description
gh:addPage(page)
end
Expand Down
20 changes: 20 additions & 0 deletions nature/doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,24 @@ function M.renderCodeBlock(text)
return '\n' .. lunacolors.format('{greyBg}' .. table.concat(lines, '\n')) .. '\n'
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

0 comments on commit fb3915e

Please sign in to comment.