Docs Parser fixes

This commit is contained in:
Robert Jelic
2025-09-13 22:28:10 +02:00
parent b5f9ced023
commit fc44bc4d8b
4 changed files with 57 additions and 62 deletions

View File

@@ -2,6 +2,7 @@ local VisualElement = require("elements/VisualElement")
local sub = string.sub local sub = string.sub
---@cofnigDescription The tree element provides a hierarchical view of nodes that can be expanded and collapsed, with support for selection and scrolling. ---@cofnigDescription The tree element provides a hierarchical view of nodes that can be expanded and collapsed, with support for selection and scrolling.
--- This is the tree class. It provides a hierarchical view of nodes that can be expanded and collapsed, --- This is the tree class. It provides a hierarchical view of nodes that can be expanded and collapsed,
--- with support for selection and scrolling. --- with support for selection and scrolling.
---@class Tree : VisualElement ---@class Tree : VisualElement

View File

@@ -4,22 +4,26 @@ local defaultPath = package.path
if fs then if fs then
local args = {...} local args = {...}
local docsPath = fs.getDir(args[2]) local docsPath = fs.getDir(args[2])
local format = "path;/path/?.lua;/path/?/init.lua;" local format = "path/?.lua;path/?/init.lua;"
local main = format:gsub("path", docsPath) local main = format:gsub("path", docsPath)
package.path = main.."rom/?;"..defaultPath package.path = main.."rom/?;"..defaultPath
else
local format = "path/?.lua;path/?/init.lua;"
local main = format:gsub("path", "tools/BasaltDoc")
package.path = main .. ";" .. defaultPath
end end
local ok1, classParser = pcall(require, "parsers.classParser") local classParser = require("parsers.classParser")
local ok2, functionParser = pcall(require, "parsers.functionParser") local functionParser = require("parsers.functionParser")
local ok3, propertyParser = pcall(require, "parsers.propertyParser") local propertyParser = require("parsers.propertyParser")
local ok4, eventParser = pcall(require, "parsers.eventParser") local eventParser = require("parsers.eventParser")
local ok6, globalParser = pcall(require, "parsers.globalParser") local globalParser = require("parsers.globalParser")
local ok5, markdownGenerator = pcall(require, "utils.markdownGenerator") local markdownGenerator = require("utils.markdownGenerator")
BasaltDoc.annotationHandlers = {} BasaltDoc.annotationHandlers = {}
@@ -141,11 +145,11 @@ BasaltDoc.registerAnnotation("@globalDescription", function(target, args)
end end
end) end)
if ok1 then classParser.setHandlers(BasaltDoc.annotationHandlers) end if classParser then classParser.setHandlers(BasaltDoc.annotationHandlers) end
if ok2 then functionParser.setHandlers(BasaltDoc.annotationHandlers) end if functionParser then functionParser.setHandlers(BasaltDoc.annotationHandlers) end
if ok3 then propertyParser.setHandlers(BasaltDoc.annotationHandlers) end if propertyParser then propertyParser.setHandlers(BasaltDoc.annotationHandlers) end
if ok4 then eventParser.setHandlers(BasaltDoc.annotationHandlers) end if eventParser then eventParser.setHandlers(BasaltDoc.annotationHandlers) end
if ok6 then globalParser.setHandlers(BasaltDoc.annotationHandlers) end if globalParser then globalParser.setHandlers(BasaltDoc.annotationHandlers) end
---------------------------------------------------------------- ----------------------------------------------------------------
-- Main Parser -- Main Parser
@@ -174,7 +178,7 @@ function BasaltDoc.parse(content)
if i <= #rawLines and rawLines[i]:match("%]%]") then if i <= #rawLines and rawLines[i]:match("%]%]") then
i = i + 1 i = i + 1
end end
if #globalAnnotations > 0 and ok6 then if #globalAnnotations > 0 and globalParser then
local global = globalParser.parse(globalAnnotations, table.concat(globalAnnotations, "\n")) local global = globalParser.parse(globalAnnotations, table.concat(globalAnnotations, "\n"))
ast.global = global ast.global = global
end end
@@ -211,25 +215,25 @@ function BasaltDoc.parse(content)
elseif #annotationBuffer > 0 then elseif #annotationBuffer > 0 then
local nextLine = lines[i] local nextLine = lines[i]
local skip = false local skip = false
if nextLine and nextLine:match("^function") and currentClass and ok2 then if nextLine and nextLine:match("^function") and currentClass and functionParser then
local fn = functionParser.parse(annotationBuffer, nextLine) local fn = functionParser.parse(annotationBuffer, nextLine)
if fn then if fn then
table.insert(currentClass.functions, fn) table.insert(currentClass.functions, fn)
end end
skip = true skip = true
elseif firstTag then elseif firstTag then
if firstTag == "@class" and ok1 then if firstTag == "@class" and classParser then
local class = classParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n")) local class = classParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n"))
if class and not class.skip then if class and not class.skip then
table.insert(ast.classes, class) table.insert(ast.classes, class)
currentClass = class currentClass = class
end end
elseif firstTag == "@property" and currentClass and ok3 then elseif firstTag == "@property" and currentClass and propertyParser then
local prop = propertyParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n")) local prop = propertyParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n"))
if prop then if prop then
table.insert(currentClass.properties, prop) table.insert(currentClass.properties, prop)
end end
elseif firstTag == "@event" and currentClass and ok4 then elseif firstTag == "@event" and currentClass and eventParser then
local evt = eventParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n")) local evt = eventParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n"))
if evt then if evt then
table.insert(currentClass.events, evt) table.insert(currentClass.events, evt)
@@ -246,7 +250,7 @@ function BasaltDoc.parse(content)
end end
if #annotationBuffer > 0 and firstTag then if #annotationBuffer > 0 and firstTag then
if firstTag == "@class" and ok1 then if firstTag == "@class" and classParser then
local class = classParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n")) local class = classParser.parse(annotationBuffer, table.concat(annotationBuffer, "\n"))
if class and not class.skip then if class and not class.skip then
table.insert(ast.classes, class) table.insert(ast.classes, class)
@@ -259,7 +263,7 @@ function BasaltDoc.parse(content)
end end
function BasaltDoc.generateMarkdown(ast) function BasaltDoc.generateMarkdown(ast)
if ok5 then if markdownGenerator then
local result = markdownGenerator.generate(ast) local result = markdownGenerator.generate(ast)
return result return result
else else

View File

@@ -2,14 +2,14 @@ local helper = require("utils.helper")
local functionParser = {} local functionParser = {}
function functionParser.parse(annotations, line) function functionParser.parse(annotations, line)
local name = line:match("^function%s+([%w_%.]+[:.]?[%w_]+)") or line:match("^function%s+([%w_]+)") local funcName = line:match("function ([%w_%.]+:?[%w_]*)")
if not name then if not funcName then
print("Warning: Could not extract function name from line: " .. line) print("Warning: Could not extract function name from line: " .. line)
return nil return nil
end end
local f = { local f = {
type = "function", type = "function",
name = name, name = funcName,
description = nil, description = nil,
shortDescription = nil, shortDescription = nil,
params = {}, params = {},
@@ -21,17 +21,14 @@ function functionParser.parse(annotations, line)
helper.applyAnnotations(annotations, f, functionParser.handlers) helper.applyAnnotations(annotations, f, functionParser.handlers)
end end
local funcName = line:match("function ([%w_%.]+)") if funcName:find(":") then
if funcName then f.name = funcName:match(":([%w_]+)")
if funcName:find(":") then elseif funcName:find("%.") then
f.name = funcName:match(":([%w_]+)") f.name = funcName:match("%.([%w_]+)")
elseif funcName:find("%.") then else
f.name = funcName:match("%.([%w_]+)") f.name = funcName
else
f.name = funcName
end
end end
if line:match("function [%w_%.]+:") then if line:match("function [%w_%.]+:") then
f.static = false f.static = false
else else

View File

@@ -1,6 +1,3 @@
-- generate-docs.lua
-- Argumente
local arg = arg or {...} local arg = arg or {...}
local SRC_DIR = arg[1] or "src" local SRC_DIR = arg[1] or "src"
local OUT_DIR = arg[2] or "build_docs/docs/references" local OUT_DIR = arg[2] or "build_docs/docs/references"
@@ -9,10 +6,6 @@ package.path = package.path .. ";./tools/?.lua"
local BasaltDoc = require("tools/BasaltDoc") local BasaltDoc = require("tools/BasaltDoc")
--------------------------------------------------------
-- Filesystem Abstraction
--------------------------------------------------------
local fileSystem local fileSystem
if fs then if fs then
@@ -25,7 +18,9 @@ if fs then
open = function(path, mode) return fs.open(path, mode) end, open = function(path, mode) return fs.open(path, mode) end,
getDir = fs.getDir, getDir = fs.getDir,
readAll = function(file) return file.readAll() end, readAll = function(file) return file.readAll() end,
write = function(file, data) file.write(data) end, write = function(file, data)
file.write(data)
end,
close = function(file) file.close() end close = function(file) file.close() end
} }
else else
@@ -90,52 +85,35 @@ else
} }
end end
--------------------------------------------------------
-- Main
--------------------------------------------------------
print("Starting documentation generation...") print("Starting documentation generation...")
print("Source directory: " .. SRC_DIR)
print("Output directory: " .. OUT_DIR)
if not fileSystem.exists(OUT_DIR) then if not fileSystem.exists(OUT_DIR) then
print("Output directory does not exist, creating it...")
fileSystem.makeDir(OUT_DIR) fileSystem.makeDir(OUT_DIR)
else
print("Output directory already exists")
end end
local function getLuaFiles(dir) local function getLuaFiles(dir)
print("Scanning directory: " .. dir)
if not fileSystem.exists(dir) then if not fileSystem.exists(dir) then
print("Directory does not exist: " .. dir)
return {} return {}
end end
local files = {} local files = {}
local list = fileSystem.list(dir) local list = fileSystem.list(dir)
print("Found " .. #list .. " items in " .. dir)
for _, item in ipairs(list) do for _, item in ipairs(list) do
local path = fileSystem.combine(dir, item) local path = fileSystem.combine(dir, item)
if fileSystem.isDir(path) then if fileSystem.isDir(path) then
print(" -> Directory, scanning recursively: " .. path)
local subFiles = getLuaFiles(path) local subFiles = getLuaFiles(path)
for _, subFile in ipairs(subFiles) do for _, subFile in ipairs(subFiles) do
table.insert(files, subFile) table.insert(files, subFile)
end end
elseif item:match("%.lua$") then elseif item:match("%.lua$") then
print(" -> Lua file found: " .. path)
table.insert(files, path) table.insert(files, path)
else
print(" -> Skipping: " .. item)
end end
end end
return files return files
end end
local luaFiles = getLuaFiles(SRC_DIR) local luaFiles = getLuaFiles(SRC_DIR)
print("Found " .. #luaFiles .. " Lua files to process")
for _, filePath in ipairs(luaFiles) do for _, filePath in ipairs(luaFiles) do
local file = fileSystem.open(filePath, "r") local file = fileSystem.open(filePath, "r")
@@ -143,12 +121,17 @@ for _, filePath in ipairs(luaFiles) do
local content = fileSystem.readAll(file) local content = fileSystem.readAll(file)
fileSystem.close(file) fileSystem.close(file)
local ast = BasaltDoc.parse(content) if not content or #content == 0 then
local markdown = BasaltDoc.generateMarkdown(ast) print("Warning: Empty file:", filePath)
end
local relativePath = filePath:gsub("^" .. SRC_DIR .. "/", ""):gsub("%.lua$", ".md") local relativePath = filePath:gsub("^" .. SRC_DIR .. "/?", ""):gsub("%.lua$", ".md")
local outPath = fileSystem.combine(OUT_DIR, relativePath) local outPath = fileSystem.combine(OUT_DIR, relativePath)
local ast = BasaltDoc.parse(content)
local markdown = BasaltDoc.generateMarkdown(ast)
local outDir = fileSystem.getDir(outPath) local outDir = fileSystem.getDir(outPath)
if outDir and outDir ~= "" and not fileSystem.exists(outDir) then if outDir and outDir ~= "" and not fileSystem.exists(outDir) then
fileSystem.makeDir(outDir) fileSystem.makeDir(outDir)
@@ -156,15 +139,25 @@ for _, filePath in ipairs(luaFiles) do
local outFile = fileSystem.open(outPath, "w") local outFile = fileSystem.open(outPath, "w")
if outFile then if outFile then
fileSystem.write(outFile, table.concat(markdown, "\n")) if not markdown or (type(markdown) == "table" and #markdown == 0) or (type(markdown) == "string" and #markdown == 0) then
print("Warning: Empty markdown for", filePath)
fileSystem.write(outFile, "!! EMPTY MARKDOWN GENERATED !!\n")
else
if type(markdown) == "table" then
fileSystem.write(outFile, table.concat(markdown, "\n"))
else
fileSystem.write(outFile, tostring(markdown))
end
end
fileSystem.close(outFile) fileSystem.close(outFile)
print("Generated: " .. outPath) print("Generated: " .. outPath)
else else
print("Error writing: " .. outPath) print("Error writing: " .. outPath)
end end
else else
print("Error reading: " .. filePath) print("Error reading: " .. filePath)
end end
end end
print("Documentation generation complete.") print("Documentation generation complete.")