Small Docs test
This commit is contained in:
@@ -1,50 +1,99 @@
|
||||
local arg = arg or {...}
|
||||
|
||||
local oldPath = package.path
|
||||
local SRC_DIR = arg[1] or 'src'
|
||||
local OUT_DIR = arg[2] or 'docs'
|
||||
|
||||
local scriptSource = debug.getinfo(1, "S").source
|
||||
local scriptDir = nil
|
||||
if scriptSource and scriptSource:sub(1,1) == "@" then
|
||||
local scriptPath = scriptSource:sub(2)
|
||||
scriptDir = scriptPath:match("^(.*)[/\\]") or "."
|
||||
local BasaltDoc = require('BasaltDoc')
|
||||
|
||||
local fileSystem
|
||||
if fs then
|
||||
fileSystem = {
|
||||
list = fs.list,
|
||||
combine = fs.combine,
|
||||
isDir = fs.isDir,
|
||||
exists = fs.exists,
|
||||
makeDir = fs.makeDir,
|
||||
open = function(path, mode) return fs.open(path, mode) end,
|
||||
getDir = fs.getDir,
|
||||
readAll = function(file) return file.readAll() end,
|
||||
write = function(file, data) file.write(data) end,
|
||||
close = function(file) file.close() end
|
||||
}
|
||||
else
|
||||
scriptDir = "."
|
||||
local lfs = require("lfs")
|
||||
fileSystem = {
|
||||
list = function(dir)
|
||||
local items = {}
|
||||
for item in lfs.dir(dir) do
|
||||
if item ~= "." and item ~= ".." then
|
||||
table.insert(items, item)
|
||||
end
|
||||
end
|
||||
return items
|
||||
end,
|
||||
combine = function(a, b) return a .. "/" .. b end,
|
||||
isDir = function(path) return lfs.attributes(path).mode == "directory" end,
|
||||
exists = function(path) return lfs.attributes(path) ~= nil end,
|
||||
makeDir = lfs.mkdir,
|
||||
open = io.open,
|
||||
getDir = function(path) return path:match("(.+)/") end,
|
||||
readAll = function(file) return file:read("*all") end,
|
||||
write = function(file, data) file:write(data) end,
|
||||
close = function(file) file:close() end
|
||||
}
|
||||
end
|
||||
|
||||
local parserSrc = scriptDir .. "/BasaltDoc/ldoc-markdown-parser/src/"
|
||||
package.path = package.path .. ";" .. parserSrc .. "?.lua;" .. parserSrc .. "?/init.lua"
|
||||
local ok, parser = pcall(require, "parser.init")
|
||||
local ioAdaptor = require("tools.io")
|
||||
package.path = oldPath
|
||||
if not fileSystem.exists(OUT_DIR) then
|
||||
fileSystem.makeDir(OUT_DIR)
|
||||
end
|
||||
|
||||
if not ok or not parser then
|
||||
-- try dofile fallback
|
||||
local initPath = parserSrc .. "/parser/init.lua"
|
||||
local ok2, module = pcall(dofile, initPath)
|
||||
if ok2 and module then
|
||||
parser = module
|
||||
local function getLuaFiles(dir)
|
||||
local files = {}
|
||||
local list = fileSystem.list(dir)
|
||||
for _, item in ipairs(list) do
|
||||
local path = fileSystem.combine(dir, item)
|
||||
if fileSystem.isDir(path) then
|
||||
local subFiles = getLuaFiles(path)
|
||||
for _, subFile in ipairs(subFiles) do
|
||||
table.insert(files, subFile)
|
||||
end
|
||||
elseif item:match("%.lua$") then
|
||||
table.insert(files, path)
|
||||
end
|
||||
end
|
||||
return files
|
||||
end
|
||||
|
||||
local luaFiles = getLuaFiles(SRC_DIR)
|
||||
|
||||
for _, filePath in ipairs(luaFiles) do
|
||||
local file = fileSystem.open(filePath, "r")
|
||||
if file then
|
||||
local content = fileSystem.readAll(file)
|
||||
fileSystem.close(file)
|
||||
|
||||
local ast = BasaltDoc.parse(content)
|
||||
local markdown = BasaltDoc.generateMarkdown(ast)
|
||||
|
||||
local relativePath = filePath:gsub("^" .. SRC_DIR .. "/", ""):gsub("%.lua$", ".md")
|
||||
local outPath = fileSystem.combine(OUT_DIR, relativePath)
|
||||
|
||||
local outDir = fileSystem.getDir(outPath)
|
||||
if outDir and not fileSystem.exists(outDir) then
|
||||
fileSystem.makeDir(outDir)
|
||||
end
|
||||
|
||||
local outFile = fileSystem.open(outPath, "w")
|
||||
if outFile then
|
||||
fileSystem.write(outFile, table.concat(markdown, "\n"))
|
||||
fileSystem.close(outFile)
|
||||
print("Generated: " .. outPath)
|
||||
else
|
||||
print("Error writing: " .. outPath)
|
||||
end
|
||||
else
|
||||
error("Failed to load parser.init via require and dofile (tried: "..tostring(initPath)..")")
|
||||
print("Error reading: " .. filePath)
|
||||
end
|
||||
end
|
||||
|
||||
local function processFile(inputFile)
|
||||
local content = ioAdaptor.readFile(inputFile)
|
||||
if not content then
|
||||
io.stderr:write("Failed to read: " .. tostring(inputFile) .. "\n")
|
||||
return
|
||||
end
|
||||
|
||||
local commentBlocks = parser.extractComments(content)
|
||||
local md = parser.generateMarkdown(commentBlocks)
|
||||
|
||||
local outputFile = "build_docs/docs/references/" .. inputFile:match("^src/(.+)"):gsub("%.lua$", "")
|
||||
ioAdaptor.ensureDirectory(outputFile)
|
||||
ioAdaptor.writeFile(outputFile .. ".md", md)
|
||||
end
|
||||
|
||||
local files = ioAdaptor.listFiles("src", "*.lua")
|
||||
for _, file in ipairs(files) do
|
||||
if not file:match("LuaLS.lua$") then
|
||||
processFile(file)
|
||||
end
|
||||
end
|
||||
print("Documentation generation complete.")
|
||||
Reference in New Issue
Block a user