2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-05-17 01:33:23 +00:00

feat: add basics for man parser

This commit is contained in:
sammyette 2025-04-25 20:20:52 -04:00
parent e676c095c2
commit e0672df2bb
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 46 additions and 0 deletions

View File

@ -47,3 +47,7 @@ end)
bait.catch('hilbish.notification', function(notif)
doNotifyPrompt()
end)
local human = require 'nature.human'
local manfile = human.where('man', {1})
human.parse(manfile)

42
nature/human.lua Normal file
View File

@ -0,0 +1,42 @@
local fs = require 'fs'
local M = {}
-- Find where a manpage is.
function M.where(name, sections)
local manpath = os.getenv 'MANPATH'
if not manpath then
manpath = '/usr/local/share/man:/usr/share/man'
end
local paths = string.split(manpath, ':')
for _, path in ipairs(paths) do
-- man directory structure:
-- <manpath>/man[sectionNumber]/manpage.[sectionNumber].gz
-- example: <manpath>/man1/Xorg.1.gz
local manSubPaths = fs.glob(fs.join(path, string.format('man%s/', sections and '[' .. table.concat(sections, '') .. ']' or '*')))
for _, subPath in ipairs(manSubPaths) do
local currentSection = subPath:match '/man([%w%d]+)$'
local assumedPath = fs.join(subPath, string.format('%s%s', name, '.*' .. currentSection .. '.gz'))
local globbedPages = fs.glob(assumedPath)
if globbedPages[1] then
return globbedPages[1]
end
end
end
end
function M.parse(path)
assert(fs.stat(path), 'file does not exist')
local _, contents = hilbish.run(string.format('gzip -d %s -c', path), false)
local sections = {}
local sectionPattern = '\n%.SH%s+([^\n]+)\n(.-)\n%.SH'
for sectionName, sectionContent in string.gmatch(contents, sectionPattern) do
sections[string.lower(sectionName)] = sectionContent
end
return sections
end
return M