mirror of
https://github.com/Hilbis/Hilbish
synced 2025-05-17 09:43:22 +00:00
feat: add basics for man parser
This commit is contained in:
parent
e676c095c2
commit
e0672df2bb
@ -47,3 +47,7 @@ end)
|
|||||||
bait.catch('hilbish.notification', function(notif)
|
bait.catch('hilbish.notification', function(notif)
|
||||||
doNotifyPrompt()
|
doNotifyPrompt()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local human = require 'nature.human'
|
||||||
|
local manfile = human.where('man', {1})
|
||||||
|
human.parse(manfile)
|
||||||
|
42
nature/human.lua
Normal file
42
nature/human.lua
Normal 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
|
Loading…
x
Reference in New Issue
Block a user