From 7ddece288be7cadc7c443b7a1d197cbbd6e4e0c0 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Tue, 12 May 2020 16:59:17 +1000 Subject: [PATCH] added doc() support for pre-compiled API documentation, wrote a generator for it, and included that in the build script --- build.sh | 3 ++- gendoc.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/doc.lua | 15 +++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 gendoc.lua diff --git a/build.sh b/build.sh index 8ab4d93..0ff5e23 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash LUA=${LUA:-lua} rm -r target/* -mkdir target &>/dev/null +mkdir -p target/doc &>/dev/null $LUA luapreproc.lua module/init.lua target/init.lua echo _OSVERSION=\"PsychOS 2.0a2-$(git rev-parse --short HEAD)\" > target/version.lua cat target/version.lua target/init.lua > target/tinit.lua @@ -10,4 +10,5 @@ cp -r service/ lib/ cfg/ target/ rm target/version.lua rm -r doc/ $LUA finddesc.lua doc/ $(find lib/ module/ -type f|sort) +$LUA gendoc.lua target/doc/kernel.dict $(find module/ -type f|sort) pandoc doc/apidoc.md docs-metadata.yml --template=template.tex -o doc/apidoc.pdf diff --git a/gendoc.lua b/gendoc.lua new file mode 100644 index 0000000..3016234 --- /dev/null +++ b/gendoc.lua @@ -0,0 +1,51 @@ +#!/usr/bin/env lua +local doc = require "lib/doc" +local ser = require "lib/serialization" +local tA = {...} + +local outpath = table.remove(tA,1) + +print(outpath) + +local function formatDocs(fd) + local rs = "" + for name,finfo in pairs(fd) do + if rs:len() > 0 then + rs = rs .. "\n\n" + end + local as = "" + for k,v in pairs(finfo.args) do + if k > 1 then + as = as .. ", " + end + as = as .. v[1] + if v[2] then + as = as .. "^"..v[2].."^" + end + end + local rt = "" + for k,v in pairs(finfo.outtypes or {}) do + if rt:len() > 0 then + rt = rt .. ", " + else + rt = ": " + end + rt = rt .. v + end + rs = string.format("%s## %s(%s)%s\n%s",rs,name,as,rt,finfo.description) + end + return rs +end + +local ad = io.open(outpath,"wb") + +for k,v in pairs(tA) do + local fd = doc.parsefile(v) + local ds = ser.serialize(fd) + local tn = v:match("/(.+)$") + if ds:len() > 3 then + ad:write(tn.."\t"..ds:gsub("\n",""):gsub(", +",",").."\n") + end +end + +ad:close() diff --git a/lib/doc.lua b/lib/doc.lua index d7dbef5..34faa46 100644 --- a/lib/doc.lua +++ b/lib/doc.lua @@ -1,3 +1,4 @@ +local _,serial = pcall(require,"serialization") local doc = {} doc.searchers = {} doc.tctab = { @@ -96,6 +97,20 @@ function doc.searchers.lib(name) -- string -- string string -- Tries to find a d if not dt then return false, "unable to find documentation for "..tostring(name) end return doc.format(dt) end +function doc.searchers.cdoc(topic) -- string -- string string -- Searches for documentation labelled as *topic* in .dict files under /boot/doc/ + if not serial then return end + for k,v in ipairs(fs.list("/boot/doc")) do + if v:sub(-5) == ".dict" then + local f=io.open("/boot/doc/"..v,"rb") + for line in f:lines() do + local mname, docs = line:match("^(.-)\t(.+)$") + if mname == topic or mname == topic..".lua" then + return doc.format(serial.unserialize(docs)) + end + end + end + end +end function doc.docs(topic) -- string -- boolean -- Displays the documentation for *topic*, returning true, or errors. Also callable as just doc(). local lib = os.getenv("LIB") or "/boot/lib"