feat(generator): initial
ci/woodpecker/push/lint Pipeline was successful Details

This commit is contained in:
Aire-One 2022-08-15 15:44:00 +02:00
parent 48c1a6df85
commit 165bd7277c
9 changed files with 171 additions and 2 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
lua_modules
generated

View File

@ -11,14 +11,17 @@
"awesomewm",
"getcontent",
"htmlparser",
"isdir",
"justfile",
"lldebugger",
"Luacheck",
"luacheckrc",
"lualogging",
"Luarocks",
"mkdir",
"setopt",
"Stylua",
"tmpl",
"wibox",
"writefunction"
],

View File

@ -15,6 +15,7 @@ dependencies = {
"lua-curl 0.3.13",
"htmlparser 0.3.9",
"web_sanitize 1.3.0",
"penlight 1.13.1",
}
build = {
type = "builtin",

View File

@ -0,0 +1,72 @@
local file = require "pl.file"
local template = require "pl.template"
local path = require "pl.path"
local log = require "logger"
local utils = require "utils"
local snippets = require "generator.snippets"
local tmpl = (function(mod)
local package_path = utils.do_or_fail(path.package_path, mod)
local package_dir = path.dirname(package_path)
return utils.do_or_fail(file.read, package_dir .. "/template.tl.tmpl", false)
end)(...)
local generator = {}
function generator.generate_teal(data)
-- TODO : add the required modules to the generated code
-- TODO : replace this with a proper way to get the module name (will also probably need the module path)
local module_data = { name = "module_name" }
for _, item in ipairs(data) do
if item.section == "Static functions" then
-- TODO
module_data.static_functions = item.items
elseif item.section == "Constructors" then
-- TODO
module_data.constructors = item.items
elseif item.section == "Object properties" then
-- TODO
module_data.properties = item.items
elseif item.section == "Object methods" then
module_data.methods = {}
-- TODO : add the self parameter
-- TODO : define overrides to use the signal type in connect/emit functions
for _, i in ipairs(item.items) do
table.insert(
module_data.methods,
i.name .. ": " .. snippets.anonymous_function(i)
)
end
elseif item.section == "Signals" then
module_data.signals = item.items
end
end
local env = {
ipairs = ipairs,
module = module_data,
}
return utils.do_or_fail(template.substitute, tmpl, env)
end
function generator.write(file_content, file_path)
-- Make sure the directory we want to write the file to exists
local directory = path.dirname(file_path)
if not path.isdir(directory) then
path.mkdir(directory)
end
local success, error = file.write(file_path, file_content, false)
if not success then
log:error {
"generator.write error",
error = error,
}
return
end
log:info { "Successfully wrote file", file = file_path }
end
return generator

View File

@ -0,0 +1,52 @@
local utils = require "utils"
local template = require "pl.template"
-- Refactor scraper code to use pl.List objects
local function join(arr, delim)
local ret = ""
for i, type in ipairs(arr) do
ret = ret .. type
if i < #arr then
ret = ret .. delim
end
end
return ret
end
local snippets = {}
function snippets.types_list(types)
return join(types, ", ")
end
function snippets.anonymous_function(item)
local parameters_string = ""
if item.parameters then
for i, param in ipairs(item.parameters) do
parameters_string = parameters_string .. param.name
local types = "any"
if param.type then
types = snippets.types_list(param.type)
end
parameters_string = parameters_string .. ": " .. types
if i < #item.parameters then
parameters_string = parameters_string .. ", "
end
end
end
local returns_string = ""
if item.returns then
returns_string = snippets.types_list(item.returns)
end
local tmpl =
[[function($(parameters_string))$(#returns_string > 0 and (": " .. returns_string))]]
return utils.do_or_fail(template.substitute, tmpl, {
parameters_string = parameters_string,
returns_string = returns_string,
})
end
return snippets

View File

@ -0,0 +1,20 @@
-- Auto generated file (Do not manually edit this file!)
# if module.signals then
local enum signals
# for _, signal in ipairs(module.signals) do
"$(signal.name)"
# end
end
# end -- /signals
local record $(module.name)
# if module.methods then
-- Object methods
# for _, method in ipairs(module.methods) do
$(method)
# end
# end -- /methods
end
return $(module.name)

View File

@ -3,6 +3,7 @@ local inspect = require "inspect"
local log = require "logger"
local properties = require "properties"
local scraper = require "scraper"
local generator = require "generator"
log:info(
inspect { message = "Start extraction", base_url = properties.base_url }
@ -25,6 +26,11 @@ log:info(
-- end
local page =
crawler.fetch(properties.base_url .. "/widgets/awful.widget.button.html")
crawler.fetch(properties.base_url .. "/widgets/wibox.widget.imagebox.html")
local items = scraper.get_doc_from_page(page)
log:info(inspect { items })
-- log:info(inspect { items })
generator.write(
generator.generate_teal(items),
properties.out_directory .. "/test.tl"
)

View File

@ -5,6 +5,8 @@ properties.base_url = "file:///usr/share/doc/awesome/doc"
properties.index_uri = "/index.html"
properties.out_directory = "generated"
--- Pages from the navigation menu to ignore.
-- Sets to ignore documentations and sample file. I also added libraries with
-- low quality API documentation, I'll probably work on them later, lets start

View File

@ -55,4 +55,16 @@ function utils.trim(string)
return string:match "^%s*(.-)%s*$"
end
function utils.do_or_fail(func, ...)
local log = require "logger"
local res, err = func(...)
if not res then
log:error { "do_or_fail failed!", error = err }
error(err)
end
return res
end
return utils